Rootmotion on slopes issue
Hi, thanks
I have a basic character that moves trough rootmotion. I want to adjust the movement on the Y axis but I am confused how to do so;
1-My preferred way would be to disable rootmotion, get the animation motion trough script and apply it in function to the ground normal to my rigidbody. I just cant find the motion as Animator.deltaPosition() only returns a value when root motion is on;
2-My second way would be done in the OnAnimatorMove() : get the rootmotion, calculate a Y offset relative to the ground normal and move my character on the Y axis accordingly; My worries is that the the movement step will be executed in two seperate steps (i have no clue when animator rootmotion is executed) and could lead to weird small collisions;
3-My last resort is to sort this out in mecanim / more animations / animation editing
please help
Answer by tcz8 · Dec 21, 2019 at 09:00 AM
It's easy to override and mold the root animation data from within a StateMachineBehavior.
In OnStateMove get the root motion data for this frame from the animation using Vector3 deltaPos = animator.deltaPosition;
then you can modify this "root motion" data before applying it manually as follow:
If using unity's character controller: character_controller.Move(deltaPosition);
Otherwise you can try applying it to the transform position directly. Note that OnStateMove is an override and you must also apply the root rotation otherwise your animations will no longer turn. You can use this: player.transform.rotation = animator.rootRotation;
You can also modify the rotation data if need be.
From what I have found out, the common fix to bouncing while going down a slope is to apply extra downward movement so before applyin deltaPos you want to modify it like so:
deltaPos.y = slopeDownForce * Time.deltaTime; // slopeDownForce must be negative
slopeDownForce being a public float you use to adjust the effect. with low values it should make smaller bounces and the higher you go it will eventually stop being bumpy.
One more thing, you should only apply this force when going downhilll. This is basicly an adaptation of this technique https://www.youtube.com/watch?v=b7bmNDdYPzU
Note that I haven't tested this code but it should work. If you have a rigidbody involved you may need to do extra work to get it working right.
Good luck.