- Home /
Scriptable root motion?
Is it possible to affect root motion's animation curves from script?
For example: I have a jump animation that moves the character up 1 unit along the Y axis. I'd like to make it "parameterized" in a sense that I can specify from script that I want the jump height to be 0.5, 1.0 or some other arbitrary value depending on some factors.
I know I can do it the "brute-force" way, by which I mean just disabling root motion for the animation and moving the character on Y axis in update/coroutine by updating character's transform position. The problems with this approach are:
It'd complicate the code
It'd be hard to synchronize the update to gameobject's position with what's going on in the animation. For example: there's some stop-and-go motion in the animation. It'd be hard to tell in the script if the animation is at the point at which character should be stationary or not and if I should update the position in script or not.
Answer by DKaras · Dec 10, 2019 at 10:22 PM
I came up with a bit of a "hack" to solve this. This perhaps won't apply to many use-cases but it may save someone some time.
Hard-code the value that you are interested in, for example translation along Y axis due to root motion
Calculate actual distance that you want to move - in this case on Y axis
Calculate scaling factor which is real_distance/hard_coded_root_motion_distance.
Override OnStateMove (or OnAnimatorMove - whichever is better for your case), and in there get animator.deltaPosition and multiply it by the scale factor from step 3. This way - if your "real distance" is smaller than distance from root motion, you'll move by analogously smaller value while still keeping overall "pace" of the move.
This allows for many other solutions, such as clamping position to some value (ignore root motion if it exceeds some threshold) etc.