- Home /
Changing animation update mode through code
I want to change the Update Mode of my animation during runtime. I created a couple functions to switch between the two. The functions are running but the modes aren't changing. Was wondering if there's anything I'm missing here?
Here are the functions I'm using.
public void PlayerAnimatorNormalTime()
{
AnimatorUpdateMode animatorUpdateMode = _animator.updateMode;
animatorUpdateMode = AnimatorUpdateMode.Normal;
Debug.Log("<color=orange>PLAYER ANIMATOR NORMAL TIME</color>");
}
public void PlayerAnimatorUnscaledTime()
{
AnimatorUpdateMode animatorUpdateMode = _animator.updateMode;
animatorUpdateMode = AnimatorUpdateMode.UnscaledTime;
Debug.Log("<color=teal>PLAYER ANIMATOR UNSCALED TIME</color>");
}
Answer by Ljaljevic1120 · Mar 05, 2020 at 10:00 AM
I know this question is old, but:
public Animator myAnimator;
myAnimator.updateMode = AnimatorUpdateMode.Normal;
// or
myAnimator.updateMode = AnimatorUpdateMode.AnimatePhysics;
// or
myAnimator.updateMode = AnimatorUpdateMode.UnscaledTime;
Answer by RedBambooLeaf · Mar 05, 2020 at 03:52 AM
You are storing a copy of _animator.updateMode, then you change that copy (but not the actual field of _animator).
Just add this line inside both of your methods:
_animator.updateMode = animatorUpdateMode;
or, alternatively, simply set the animator mode directly by changing your methods to:
public void PlayerAnimatorNormalTime()
{
_animator.updateMode = AnimatorUpdateMode.Normal;
Debug.Log("<color=orange>PLAYER ANIMATOR NORMAL TIME</color>");
}
public void PlayerAnimatorUnscaledTime()
{
_animator.updateMode = AnimatorUpdateMode.UnscaledTime;
Debug.Log("<color=teal>PLAYER ANIMATOR UNSCALED TIME</color>");
}
or, even better, define a single method to change your _animator.updateMode to any AnimatorUpdateMode you want:
public void ChangePlayerAnimatorMode(AnimatorUpdateMode newMode) {
_animator.updateMode = newMode;
}
Hope it helps!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
two animations interacting smoothly 0 Answers
How can I get the legnth of an animation? (Mechanim Animator/C#) 2 Answers
Enemy AI Script Glitch 0 Answers
Distribute terrain in zones 3 Answers