I am trying to figure out the speed of a rigidbody2D (it’s velocity) (It’s movement speed) so that I can animate a character. I need the character to stop the walking animation when it hits a wall (stops moving) The problem is that when my character walks into a wall, if the key is still held, I am still adding to the velocity. Therefore my code still thinks that it should try to play the walking animation.
Is there any code to determine the actual speed at which a rigid body is moving and not just being set to?
Is there any better way of telling the animator to stop the animation when the player isn’t moving?
I need a quickly updating solution.
I have tried:
if (GetComponent<Rigidbody2D>().isSleeping())
and
if (gameObject.isStatic)
but they either don’t work or are too slow at recognising that the player has stopped.
Please help me solve this problem.
My current solution is:
if (rb.velocity == 0) {
anim.SetFloat ("Speed", 1);
} else {
anim.SetFloat ("Speed", 0);
}