Movement Animation Looping when it Shouldn't
I am currently working on the walking animations for a 2D top-down rpg using tile-based movement similar to movement in the 2D Final Fantasies and Pokemon. Every animation consists of the character taking a step on frame one, and the character idle on frame twelve. Every time the player takes a step, it should play that animation once, then stop. However, the animation will replay occasionally. What seems to be causing this is the script that moves the character. I'm assuming this is the offending code:
protected IEnumerator Move(bool run) {
//Find a new position proportionally closer to the end, based on the moveTime
Vector3 newPostion = Vector3.MoveTowards(rb2d.position, target, (!run ? MoveSpeed : RunSpeed) * Time.deltaTime);
//Call MovePosition on attached Rigidbody2D and move it to the calculated position.
rb2d.MovePosition(newPostion);
//Recalculate the remaining distance after moving.
sqrRemainingDistance = (target - transform.position).magnitude;
//Return and loop until sqrRemainingDistance is close enough to zero to end the function
yield return null;
}
It will, on average, take 12 frames to complete. Every now and then, it will take 13.2 frames to complete. This code is being called in FixedUpdate so I don't know why there's a discrepancy of 1.2 frames occasionally, unless I'm misunderstanding FixedUpdate. This is the entire script I'm using to move the character.