- Home /
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
Answer by tyruji · Jul 22, 2021 at 01:30 PM
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.
Sorry, I had no time so far, but if I'll try it out, I'll let you know if it worked.
Answer by nniitchhandeleven · Jul 23, 2021 at 11:08 AM
Thanks for your help. I will try it out later and then give you an update.
Your answer
Follow this Question
Related Questions
how to make a irregular ground or slope,About irregular terrains and slopes 0 Answers
Attach a gravity automatically? 1 Answer
How can I have a CharacterController's height make it to where the feet are correctly on the ground? 0 Answers
Why are my animations not working correctly? Thanks! 0 Answers