Using root motion scripting for specific animations

I have various animations from the asset store which I intend to use within my game, currently I am looking to introduce some combat stance animations. All my current general movement animations are within an animator using Mecanim and they all have root motion in-built, i.e. they have velocity and the animation causes the character to move across the map.

I have imported some new combat based animations which were originally set to Legacy, I changed them to Humanoid for use in the animator but they have no average velocity, i.e. they are in-place animations and don’t make the character move. I understand that to make these move the character I need to attach a root motion script and call OnAnimatorMove to set the characters transform based on the animator parameters.

The problem I’m facing is that this affects all my animations, is there a way to only use this script for the specific in-place animations but use default root motion for all the others that work fine?

Thanks in advance.

One way to do this is to implement your own Root Motion handling script.

Start by figuring out which animations you want to use the built in root motion, and which you’d like scripted motion. You may wish to add curves to the animations you’d like scripted, to provide animation-matched data for use in your script, but this isn’t always necessary.

Then implement an OnAnimatorMove() method in a component script with access to your Animator. Note: Once you do your object’s Animator component will print “Handled by Script” in the Apply Root Motion field rather than allow you to toggle it via checkbox. Within the method, you can implement any scripted motion you like. For any built in root motion you’ll need to grab it from the Animator (deltaPosition, and deltaRotation will be useful) and apply them directly to the root (Transform) of your animated object.

You can mix scripted and built-in root motion, but it may look very weird if you alter the interpretation of the built-in motion curves.