- Home /
Trying to manage an Animation. AnimationState.time doesn't adjust the animation
I have a sphere moving from left to right by use of an animation. To the interface, I've added a button that will advance the animation by a set interval called delta
.
Below is my moveObject script which is attached to the Sphere.
var objectAnimation:Animation;
function Movement(delta:float){
animation[objectAnimation.clip.name].time += delta;
}
This function is called from the button within it's button press method as below:
moveTarget.GetComponent(MoveObject).SendMessage("Movement", 1.0);
A Debug.Log
of animation[objectAnimation.clip.name].time
shows that it is indeed changing every time the button is pressed. Say for instance, the delta is 1, in my tests, every time the button is pressed, the Debug.Log returns 1, then 2, then 3 etc. However, the animation itself does not advance, the Sphere stays in the same place all the time.
What am I doing wrong here?
Answer by Bunny83 · Jun 20, 2012 at 02:23 PM
The AnimationState have to be enabled and need to have some weight set. The weights of all active animation states are combined and normalized. If you have two animations with a weight of 1.0 each, it will result in each animation get's 0.5 weight.
If you call Play() it actually does this for you. It enables the state and set it's weight to 1.0. CrossFade will enable it and slowly increase the weight and decrease the weight of the old one.
The time usually advances automatically, so if you want to set the time manually, you might want to set the speed to 0.0
You can also use Animation.Sample to sample the animation manually at a certain point.