Advancing animations by X amount of seconds is quite a common occurrence for me in Unity3D. It can be done with the built in AnimationState class.
In the game I’m currently developing, I needed to develop a method of managing these animations. In my case, I have a ‘lever’ that I’d like to ‘flip’ in increments instead of just complete from one end to the other.
You could do this quite simply by micromanaging the animation. The animation of the lever would move the main lever stick from left to right, and then it’s position in the animation could be controlled through Javascript.
This can be done quite simply with the use of the AnimationState.
Unity3D’s AnimationState
Here’s a quote from the Unity Script Reference for AnimationState –
The AnimationState interface allows you to modify speed, weight, time and layers while any animation is playing. You can also setup animation mixing and wrapMode.
Utilising the AnimationState can be a little tricky for those who haven’t used it before, so hopefully I’ll be able to provide some insight.
First I opened my objectAnimation up to the Inspector. This way, I can reference the animation in the code quite easily.
Then I created a function called Movement, which takes ‘interval’ as an argument. This will be called from a controller script that I have on my button.
|
1 2 3 4 5 |
var objectAnimation:AnimationClip; function Movement(interval:float){ } |
Now to populate the Movement function.
I need to firstly initialise my AnimationState with regard to my objectAnimation. This AnimationState takes the animation’s name as a slicer, so I can pass the name of my object animation to it. Before you can use the AnimationState, you must enable it, and give it a weight.
|
1 2 |
animation[objectAnimation.name].enabled = true; animation[objectAnimation.name].weight = 1.0; |
Weight is used for cross fading animations i.e. fading one animation into another (it takes values between 1.0 and 0.0), but I’m not using that method here, so I just need to explicitly set it as 1.0.
Now for the main action code. I need to advance the AnimationState’s time by the predefined interval.
|
1 |
animation[objectAnimation.name].time += interval |
This Movement function, as I mentioned can be called from any button press script like so:
|
1 |
object.GetComponent(MovementScriptComponentName).SendMessage("Movement",0.25); |
Now you have the base knowledge needed to work with simple cases of AnimationState.
Further Reading
AnimationState has a wrapMode setting that controls how the animation reacts to a value that’s over it’s bounds, i.e. 4 seconds for a 2 second animation, so you can adjust this setting to manage how you’d like this to react.
I can also clamp the time setting of the AnimationState to avoid it breaking it’s bounds entirely:
|
1 2 3 4 5 6 7 8 9 10 11 |
var length:float = objectAnimation.length; if(animation[objectAnimation.name].time>length){ animation[objectAnimation.name].time=length; } else if (animation[objectAnimation.name].time<=0){ animation[objectAnimation.name].time=0; } |
Comments
Powered by Facebook Comments