2D Character Animation - even starting if I'm not pressing A or D

Hey Guys,

thanks for your time. I’m new to Unity and I have a problem with my walking animation. The problem is that my animator should switch from idle to walk animation if the speed is greater than 0.01 and go back if it’s less than 0.01. But if I start my game, the speed automaticly goes to 5 all the time, even if im not pressing the walk buttons.

Thanks for your help


Well, basically you are not passing the actual speed of your character to the animator. You are passing the speed value at which your character will move, in other words your Math.Abs( moveSpeed ) would be the same as doing Math.Abs( 5 ) (in your case), and that won’t do, right? What you could do is store the value you offset your character by and pass its magnitude to the animator.

example code:

Vector3 movement = new Vector3( Input.GetAxis("Horizontal"), 0 );
// store the offset
Vector3 offset = moveSpeed * Time.deltaTime * movement;
transform.position += offset; // translate
animator.SetFloat( "Speed", offset.magnitude ); // and pass in the magnitude

If something is unclear or you have more questions, I’ll be happy to help.

Thanks for your help. I will try it out later and then give you an update.