Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do I stop the flickering that occurs when the character stops moving?

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 699
    Answer it

    I followed this tutorial on youtube: Unity3D. Top-down 8 directions movement - YouTube[^]. It took me a while but I've almost converted from movement by arrows to mouse touch . but now I have this problem, there's flickering that occurs when the character stops moving. I looked at the link he sent me but it doesn't work. Please look at this video to show you what I am talking about: My unity problem - YouTube[^]. Does anyone knows how to stop the flickering that occurs when the character stops moving? This is my code:

       

    private Animator anim;
        public float speed = 15f;
        private Vector3 target;
        private bool touched;
        
        
        void Start () {
            target = transform.position;
            anim = GetComponent<Animator> ();
        }
        
        void Update () {
            touched = true;
            if (Input.GetMouseButtonDown (0)) {
                Vector3 mousePosition = Input.mousePosition;
                mousePosition.z = 10; // distance from the camera
                target = Camera.main.ScreenToWorldPoint(mousePosition);
                target.z = transform.position.z;
            }
    
    
                
                var movementDirection = (target - transform.position).normalized;
                
                if (movementDirection.x != 0 || movementDirection.y != 0) {
                    anim.SetBool("walking" , true);
                    anim.SetFloat("SpeedX" , movementDirection.x);
                    anim.SetFloat("SpeedY" , movementDirection.y);
                    
                    Vector2 movement = new Vector2(
                        speed * movementDirection.x ,
                        speed * movementDirection.y);
                    movement *= Time.deltaTime;
                    transform.Translate(movement);
                    
                    if (movementDirection.x < 0) {
                        anim.SetFloat("LastMoveX" , -1f);
                    }
                    else if (movementDirection.x > 0) {
                        anim.SetFloat("LastMoveX" , 1f);
                    }
                    else {
                        anim.SetFloat("LastMoveX" , 0f);
                    }
                    if (movementDirection.y > 0) {
                        anim.SetFloat("LastMoveY" , 1f);
                    }
                    else if (movementDirection.y < 0) {
                        anim.SetFloat("LastMoveY" , -1f);
                    }
                    else {
                        anim.SetFloat("LastMoveY" , 0f);
                    }
                } else {
                touched = false;
                anim.SetBool("walking" , false);
            }
        }

    What I have tried:

    I have tried the following: 
    https://josejimenez.info/blog/unity3d-top-down-8-direction-movement.html

    I've Tried checking out what happens if I add something like

     

    if (movementDirection.x != 0 || movementDirection.y != 0) {
    anim.SetFloat("SpeedX", movementDirection.x);
    anim.SetFloat("SpeedY", movementDirection.y);
    }
    
    and
    
    if (movementDirection.x != 0 || movementDirection.y != 0) {
    float rot_z = Mathf.Atan2 (movementDirection.y, movementDirection.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler (0f, 0f, rot_z);
    }
    
    as well as 
    transform.rotation = Quaternion.Euler (0f, 0f, rot_z - 90)

     

 2 Answer(s)

  • For sure your code is not reaching to the end so that it can set the "walking" parameter false despite of the player reaching to the destination position.

    So for the change don't run that part of code in "else" part inspite check it seperately.

  •     void Update () {
        touched = true;
        if (Input.GetMouseButtonDown (0)) {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = 10; // distance from the camera
            target = Camera.main.ScreenToWorldPoint(mousePosition);
            target.z = transform.position.z;
        }
        var movementDirection = (target - transform.position).normalized;
        if (movementDirection.x != 0 || movementDirection.y != 0) {
            anim.SetBool("walking" , true);
            anim.SetFloat("SpeedX" , movementDirection.x);
            anim.SetFloat("SpeedY" , movementDirection.y);
            Vector2 movement = new Vector2(
                speed * movementDirection.x ,
                speed * movementDirection.y);
            movement *= Time.deltaTime;
            transform.Translate(movement);
            if (movementDirection.x < 0) {
                anim.SetFloat("LastMoveX" , -1f);
            }
            else if (movementDirection.x > 0) {
                anim.SetFloat("LastMoveX" , 1f);
            }
            else {
                anim.SetFloat("LastMoveX" , 0f);
            }
            if (movementDirection.y > 0) {
                anim.SetFloat("LastMoveY" , 1f);
            }
            else if (movementDirection.y < 0) {
                anim.SetFloat("LastMoveY" , -1f);
            }
            else {
                anim.SetFloat("LastMoveY" , 0f);
            }
        } 
            if(movementDirection.x == 0 && movementDirection.y == 0)//Try to check this condition weather it's getting to the target position or not.
            {
            touched = false;
            anim.SetBool("walking" , false);
        }
    }

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: