Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Unity3D: How do I stop the flickering that occurs when the character stops moving?

BakausagichanBakausagichan hayesMember Posts: 4

I followed this tutorial on 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 (). 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);
    }
}
Tagged:
Sign In or Register to comment.