Unlock Animation Secrets: Mastering Unity's Motion Time in Animator States
This page is dedicated to a little known, but very powerful feature of Animator Controller States: MotionTime
What Does It Do?
According to Unity, Motion Time is described as: "The time used to play the motion for this state. Enable Parameter to control the motion time with a custom value from a script."
....That is not a great description of what this is capable of.
Admittedly, this is a concept that is hard to put into words.
A Slightly Better Description
Simply put, this feature will allow code to directly manipulate what frame the Animation State is playing in the Animation Clip, assigned.
Put another way: We can bypass an Animation Clip's normal playback control, allowing code to take control of playback.
Put ANOTHER way: We can use code to scrub through an Animation Clip, just like you can drag the current frame around in an Animation tab.
Once setup, you can manipulate the Motion Time float in the Animator to advance, or even reverse, the Animation Clip in the State.
You can then think of the current time of the Animation Clip as a normalized float:
0.0 = The beginning of the clip
1.0 = The end of the clip
Negative number and values over 1.0 are also valid!
-0.25 = A quarter of the clip, but in reverse
-0.25 is identical to 0.75
1.25 = A quarter of the clip past the end, which then loops back around and becomes 0.25
This means you can have a lot of precise control of the playback of a clip, allowing Animation Curves, reversing the animation, and/or looping the animation a specific amount of times (fractions of a loop are valid).
Setup Steps
Find the Animator State with the Clip you want to control.
Enable the "Motion Time" parameter in the Inspector.
Give the parameter a name so code can find it.
We use "MotionTime" as a default.
If you are using this feature in multiple Animator States then you will need unique names for each, unless you want the same parameter to influence both states.
Make sure code is using the following commands to control Motion Time.
<your Animator ref>.SetFloat(<your parameter name>, <value set this frame>);
Example:
spinnerAnimator.SetFloat(MotionTime, motionTime);
MotionTime == "MotionTime"
motionTime == a value output from an AnimationCurve evaluation
Comments
Post a Comment