- Home /
Animator switching between floats
Hello guys,
in my script, im trying to animate a moving character. I calculate the speed with the following script:
float currentSpeed = (transform.position - lastPosition).magnitude * 10; lastPosition = transform.position;
Then i access the Animator and change the float parameter:
animator.SetFloat ("speed", currentSpeed);
Now my problem is, that the "speed" Parameter in the animator is switching between 0 and the real speed multiple times in a second. This results in the animation states switching between Walk and Run and sometimes over to Idle.
My transition conditions are:
Idle - Walk: speed 0.1 Walk - Run: speed 0.5
I have set the animations to loop, i have set them to loop pose, i tried the FixedUpdate function, i tried InvokeRepeating the speed calculation every 0.1 seconds to avoid too many changes in the speed parameter but nothing helps.
Thank you, i hope someone has an idea
Answer by Kashyk · Dec 15, 2016 at 09:44 AM
i dont know why, but it works now... this is the code for everyone who needs it:
void Update () { speed (); }
void speed(){
float currentSpeed = (transform.position - lastPosition).magnitude / Time.deltaTime;
lastPosition = transform.position;
animator.SetFloat ("speed", currentSpeed);
}