Animation control - script problem

Hi,

What could be wrong with this script?

function OnTriggerEnter (col : Collider) 
{	
	for (var state : AnimationState in animation){
	var resumeAnimation;
	resumeAnimation = state.time;
	animation.Stop();
	yield new WaitForSeconds (3);			
	animation.Play();
	state.time = resumeAnimation;			
	}
}

What I’m trying to accomplish:

The player drives a car around a race circuit.

I have other cars which drive around autonomously, they’re animated (not controlled by players).

The player can bump into the other cars and gravity will spin them around.

To achieve this, the animations must be stoppped at the moment of collision, because gravity only seems to work when no animation is running.

After the spin, I want the other car to resume his track (and continue with the animation at the point where he left).

Problem is: the car that gets bumped into, starts the animation from the beginning, not from the point where he left.

Is the script flawed, or is there another way of achieving this?

I’m a newbie, evaluating Unity, any help appreciated.

I’m not sure if this is your problem, but it looks odd that you stop one state, wait 3 seconds and resume it, then stop the next state, wait 3 seconds and resume, then the next etc. Are you sure you’d not rather first stop all the states, then wait 3 seconds, then resume all the states?

Rune

I’ve been using animations with rigidbodies and gravity just fine. What exactly is your problem in that regard? There should be no need to stop the animations to get physics working.

Also, it might be simpler to just set state.speed to 0. This pauses the animation and you can resume it by simply setting state.speed back to 1.

@Adrian: I also thought that was strange, but from my experience, as soon as I add animation to a rigidbody collider, the physics no longer work. You hit it, but it just continues it’s animation path and doesn’t move. Or am I missing something very obvious?

@runevision: even if there’s only one animation, the problems occurs. The problem seems to be that the current time in the animation doesn’t get passed to the variable.

It seems I solved the problem. Turns out that the trigger was called multiple times, and Unity executed the second and third trigger code, before the code in the first trigger event had finished. To prevent it, I had to add a little counter.

static var resumeAnimation; 
static var collisionCounter = 0;

function OnTriggerEnter (col : Collider){
	   
	collisionCounter = collisionCounter + 1;

//now make sure the code gets executed only once

	if (collisionCounter == 1) {

   audio.Play(); 
   var state : AnimationState ; 
   state = animation["AnimationTest"]; 
   
   
   resumeAnimation = state.time; 
   
   Debug.Log ("resumeAnimation = " + resumeAnimation);
   
   animation.Stop("AnimationTest"); 
   
   yield new WaitForSeconds (3); 
   
   animation.Play("AnimationTest"); 
   var newstate : AnimationState ; 
   newstate = animation["AnimationTest"]; 
   
    Debug.Log ("newstate.time = " + newstate.time);
 
    newstate.time = resumeAnimation; 
       Debug.Log ("resumeAnimation2 = " + newstate.time);

// reset the counter so that new events may happen

     collisionCounter = 0;
       
	}
}