Using animationState for animation controls

Hey everyone,

I was hoping to get some help with a problem that I’ve been stuck on for the past week and a half. I am pretty sure that this might end up being a really easy fix, but I’ve had no luck so far.

I’m working on a project that requires me to create a sort of a “Media Player” setup in Unity3D, specialized in showcasing animation. I’ve been able to use the AnimationState to create a simple increase/decrease play speed which works at the moment, but I’m pretty much stuck on every single other function.

Now, I have a pretty good idea on what the problem is. So far I’ve been using AnimationState in the same way that it’s described in the examples on the Unity website, which looks like this:

for (var state : AnimationState in animation)
{state.speed = 0.5;}

The piece of code that I’ve used for my function looks like this

The function itself:

function increaseAnimSpeed(anim:Animation)
{	
	for(var state: AnimationState in anim)
	{
		state.speed = state.speed * speedChanger;
		Debug.Log("changing to " + state.speed);
	}
}

Calling the function when a button is pressed:

if(GUI.Button(Rect(width * 0.45, height * 0.835, 30, 30), "P"))
{
	PausePlayAnimation(this.gameObject.animation);
	Debug.Log("Play!");
}

I used this example in my increase/decrease function, but every time that I call the function, it is executed several times. To be precise, I think it executes as many times as there are animations on the gameObject to which the script is attached and once on top of that. Which I imagine is pretty logical, because of the for-loop. For the speed controls this doesn’t seem to be that big of a deal. It changes the speed from 1 to 2. but executes the loop 5 times if there are 4 animations on the gameobject. It does the job, no problem, it just runs a bunch of extra unnesscesary loops.

It doesn’t work at all for the other functions though, when I try to create the pause/play function, for example, switches between pause and play several times each time I click the button. The code for the function is pasted below:

function PausePlayAnimation(anim: Animation)
{
	var isPlaying: boolean = true;
	var tmpSpeed: float;
	
	for(var state: AnimationState in animation)
	{

	// Pause the animation
		if(isPlaying == true)
		{
			tmpSpeed = state.speed;
			state.speed = 0;
			isPlaying = false;
			Debug.Log("Pausing animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
			yield new WaitForSeconds(buttonDelay);
		}
	
	//Play the animation
		else
		{
			state.speed = tmpSpeed;
			isPlaying = true;
			Debug.Log("Resuming animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
			yield new WaitForSeconds(buttonDelay);
		}
	}
}

If I call this function, the outpit prints the Debug 5 times and I just have NO idea on how to fix this. I’ve been looking for a way to use AnimationState without the for-loop, of course. But unfortunatly, I’m not that great of a programmer and I always seem to get null reference errors when I try to just declare it as a variable.

Sorry for the incredibly long slab of text, I’m hoping you guys can help me out with this.

TL;DR
Any way to use animationstate without a for-loop, or any alternative way to create a play/pause button?

Cheers!

your Debug.Log is executed 5 times because of the for loop…

try this:

 function PausePlayAnimation(anim: Animation)
 {
     var isPlaying: boolean = true;
     var tmpSpeed: float;
     var debug: bool = true;
     
     for(var state: AnimationState in animation)
     {
 
     // Pause the animation
         if(isPlaying == true)
         {
             tmpSpeed = state.speed;
             state.speed = 0;
             isPlaying = false;
             if (debug) {
                  Debug.Log("Pausing animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
                  debug = false;
             }
             yield new WaitForSeconds(buttonDelay);
         }
     
     //Play the animation
         else
         {
             state.speed = tmpSpeed;
             isPlaying = true;
             if (debug) {
                  Debug.Log("Resuming animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
                  debug = false;
             }
             yield new WaitForSeconds(buttonDelay);
         }
     }
 }

i did not test it but i hope it will help

the way i handle speed in the animations is creating a speed parameter in the animator controllerand on the animation state’s properties enable the multiplier under speed and set it to the animator controller’s speed value.


then it is pretty easy to set this value with animation.setfloat from the script.

to pause, i would try setting the speed to 0