# Unity2D: Returning back to idle state animation problem

**URL:** <https://forums.kodeco.com/t/unity2d-returning-back-to-idle-state-animation-problem/17517>\
**Category:** Game Tech\
**Created:** [July 19, 2016, 3:46pm UTC](https://forums.kodeco.com/t/unity2d-returning-back-to-idle-state-animation-problem/17517 "2016-07-19T15:46:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xxdivaxx1](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/xxdivaxx1/32/37247_2.png) [@xxdivaxx1](https://forums.kodeco.com/u/xxdivaxx1)\
**Post date:** [July 19, 2016, 3:46pm UTC](https://forums.kodeco.com/t/unity2d-returning-back-to-idle-state-animation-problem/17517/1 "2016-07-19T15:46:35Z")

</div>

Since, I am a new user, I can only put two up-links, so if you could click [here](https://stackoverflow.com/questions/38412266/unity3d-returning-back-to-idle-state-animation) to read the full information, thank you 🙂. So can anyone help me with both of my problems, which is:

1. Returning back to idle animation once my player reaches it’s destination.
2. And making sure that my player face the correct direction as it is suppose to. My player’s sprite [image](http://i.imgur.com/Hz28tgl.png)

Here is my code:

```
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;

void Start () 
{
    target = transform.position;
    anim = GetComponent<Animator> ();
}

void Update () 
{

if(transform.position == target)
{
    anim.SetBool ("walking", false);
}
    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;
            Vector3 animDirection = Vector3.zero;
            // Use >= to default to horizontal on both being equal

        if (movementDirection.x > movementDirection.y) 
            animDirection.x = 1;
        else
            animDirection.y = 1;

        anim.SetBool ("walking", true);
        anim.SetFloat ("SpeedX", movementDirection.x);
        anim.SetFloat ("SpeedY", movementDirection.y);

        Debug.LogFormat ("X: {0}, Y: {1}", movementDirection.x, movementDirection.y);

        if (movementDirection.x > 0)
        {
            anim.SetFloat ("LastMoveX", 1f);
        }
        else if (movementDirection.x < 0) 
        {
            anim.SetFloat ("LastMoveX", -1f);
        } 
        else 
        {
            if (movementDirection.y > 0)
            {
                anim.SetFloat ("LastMoveY", 1f);
            } 
            else if (movementDirection.y < 0)
            {
                anim.SetFloat ("LastMoveY", -1f);
            } 
            else 
            {
                anim.SetFloat ("LastMoveY", 0f);
            } 
        }   
    } 
    else 
    {
            transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
    }
}

```
