- Home /
Automatically adjusting the speed of a walking animation to avoid foot-sliding?
My most recent attempt looks like this:
var animVel = Vector3.Project(m_Animator.velocity, m_Rigidbody.velocity.normalized);
Debug.DrawRay(transform.position, animVel * 50f);
float animSpeed = animVel.magnitude;
if (animSpeed > 0f) {
m_Animator.SetFloat("Forward", m_Rigidbody.velocity.magnitude / animSpeed);
}
else
{
m_Animator.SetFloat("Forward", 1f);
}
Which may well be nonsense, but it's been an improvement. :] "Forward" is used as a parameter for the speed multiplier in the animator controller.
Anyway, how would one go about doing this?
Also, what's the difference between animator.velocity and animator.deltaPosition?
Answer by Multithreaded_Games · Feb 16, 2017 at 01:58 PM
The easiest way to do this is to use root motion. Assuming you are using a humanoid rig, I'd have a look at and override the Animator.OnAnimatorMove function. This function will be called during each update frame or physics tick (depending on what your animation update mode is set to.)
Within this function, you'll want to read out either deltaPosition (this is the root-motion driven 'translation' of your character) or the velocity (this is pretty much the same as deltaPosition but it hasn't been multiplied by deltaTime). I would guess that the distinction is mostly for standard translation versus Rigidbody updates--for translation, deltaPosition is already what you want, but if you want to use it with Rigidbodies, you'll want to divide by deltaTime. Similarly, if you want to use velocity with translated bodies, you'll want to multiply by deltaTime. I can elaborate on this more if necessary
Basically you'll be using controller/keyboard/mouse or whatever input to set your animator parameter values. The deltaPosition returned within OnAnimatorMove is directly related to how much the root motion position changes (for humanoids, this is already defined for you as a special node) and this is related to what parameter values you set to 'drive' the animation.
One last caveat: for your particular walk/run animations, you'll want to NOT bake the XZ translation into the pose itself, otherwise OnAnimatorMove updates will return no velocity change for the deltaPosition/velocity. I would suggest setting up a test scene using the Unity standard ThirdPersonController (ethan model) to get all of this working correctly, and you might want to have a look at that code as well, although I'm not sure exactly if it uses OnAnimatorMove updates.
This is probably a lot of information, but let me know if anything is unclear and I'll try to help as best I can!
Your answer
Follow this Question
Related Questions
mecanim animation runs at random speed on iOS 0 Answers
Mecanim - changing animation clip speed, through script? 4 Answers
Mecanim problem with setting speed 0 Answers
Are mecanim blend trees linear? 2 Answers