Skip to content

Instantly share code, notes, and snippets.

@123tris
Created November 7, 2019 20:21
Show Gist options
  • Save 123tris/d660a1258248d2bc2e7d1a2a056e22a1 to your computer and use it in GitHub Desktop.
Save 123tris/d660a1258248d2bc2e7d1a2a056e22a1 to your computer and use it in GitHub Desktop.
A statemachinebehaviour to toggle rootmotion when entering or exiting animation states in Unity
using UnityEngine;
public class UpdateRootMotion : StateMachineBehaviour
{
public bool onStateEnter;
public bool applyRootMotionOnEnter;
public bool onStateExit;
public bool applyRootMotionOnExit;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (onStateEnter)
animator.applyRootMotion = applyRootMotionOnEnter;
}
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (onStateExit)
animator.applyRootMotion = applyRootMotionOnExit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment