Idle Animation Not Playing

Hi all,
I am currently testing 2D game development. After finally getting my follower script to work and have a sprite follow me, I am attempting to animate this follower too. I’m sure you can already tell I’m new to Unity and games development in general so please point out anything wrong!

My problem is that my animations are playing for my actual movements (walking up, down, left and right), but whenever I stop my idle animations are not playing even though my isMoving boolean is false, and the moving animations continue to play even when the follower is not moving. Here is my code:

    public Transform target;
    public float speed;
    public float stopDistance = 1f;
    Animator anim;
    Rigidbody2D rbody;

    void Start()
    {
        anim = GetComponent<Animator>();
        rbody = GetComponent<Rigidbody2D>();
        anim.speed = 0.65f;
    }
    

    void Update()
    {
        //follow player
        var distance = Vector2.Distance(transform.position, target.position);
        float step = speed * Time.deltaTime;

        if (distance > stopDistance)
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, step);
        }


        //getting direction of movement and therefore animating it
        var heading = target.position - transform.position;
        var moveDistance = heading.magnitude;
        Vector2 moveDirection;
        // Get the direction of the player
        moveDirection = heading / moveDistance;
        // Make this vector planar (xz-plane)

        if (moveDirection != Vector2.zero)
        {
            anim.SetBool("isMoving", true);
            anim.SetFloat("moving_x", moveDirection.x);
            anim.SetFloat("moving_y", moveDirection.y);
        }
        else
        {
            anim.SetBool("isMoving", false);
        }
    }

From testing around with the code I think the problem is to do with the var heading = target.position - transform.position;, because with my stopping distance preventing the follower getting too close, it will always leave a gap distance between the follower and player, and I feel that this gap is always setting the value of ‘heading’ to be > 0.

I think this is the problem because when I remove the stopping distance all together and disable the sprite renderer, I can see that the follower does not move (/is idle when it is directly on top of the player). As well as the ‘moving_x’ and ‘moving_y’ parameters being always above/below 0 and never actually 0 in the animation pane while moving. As stated before I feel like this is caused by the gap inbetween the follower and player caused by the stopping distance, however I cannot work out any fix for this. So any solution would be greatly appreciated!

Hi Peakz. I think the main thing you’re doing wrong is checking if moveDirection is zero. As you said that distance won’t ever really be zero.

However you already know everything you need. The line near the start that goes “if (distance > stopDistance)” looks like it does the check you need. What about some code like this?

if (distance > stopDistance)
         {
             anim.SetBool("isMoving", true);
             anim.SetFloat("moving_x", moveDirection.x);
             anim.SetFloat("moving_y", moveDirection.y);
         }
         else
         {
             anim.SetBool("isMoving", false);
         }