- Home /
Creating a play/pause button for animations.
Hey guys, might be a bit of an odd request here. I'm working on creating an application that is going to serve as a showcase for 3D animations. The user can turn the camera around the character and the showcase contains basic functions that most media players have as well.
One of the first and most important of these functions is a button that can pause the current animation when pressed, and resume it when pressed again. At the moment I'm having a bit of trouble with this one. I can't seem to get the player to resume the animation after having paused it. A crucial element of the program is that the user can slow down or speed up the animation by pressing corresponding buttons. This means that when the user pauses the player at 0.5 animation speed, it should resume at that same speed. Right now I'm doing this by saving the animation speed to a float variable, tmpSpeed, on the moment the user presses the pause button. Then when the player presses the button again to resume the animation, it calls the saved animation speed again and resumes.
Below is the code that I have so far:
function PausePlayAnimation(anim: Animation){
var isPlaying: boolean = true;
var tmpSpeed: float;
for(var state: AnimationState in anim){
// Pause the animation
if(isPlaying == true){
tmpSpeed = state.speed;
state.speed = 4;
isPlaying = false;
Debug.Log("Pausing animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
}
//Play the animation
else if(isPlaying == false){
state.speed = tmpSpeed;
isPlaying = true;
Debug.Log("Resuming animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
}
}
}
Right now the function is being called when the space bar is pressed. But as soon as I press it, it seems to run through the loop three times before finishing. On top of that, the animation only pauses when the space bar is pressed TWICE. When reading the Console for my debug logs it says:
First Press
Pausing animation, state.speed is: 4. tmpSpeed is 1
Resuming animation, state.speed is: 1. tmpSpeed is 1
Pausing animation, state.speed is: 4. tmpSpeed is 1
Second press
Pausing animation, state.speed is: 4. tmpSpeed is 4
Resuming animation, state.speed is: 4. tmpSpeed is 4
Pausing animation, state.speed is: 4. tmpSpeed is 4
I was wondering if this was a problem because of the fact that I'm using a for loop to check the animation states? I don't really know any other method to check and/or change the animation speed, so I decided on using this.
If you guys need any more information, let me know. I hope you guys can help me out with this, because I have no clue.
Cheers!
Hey guys, small update for anyone trying to help me out here..
I discovered the pretty obvious fact that the the function being ran three times is, of course, because of the fact that the for-loop is dependent of the amount of animations I had on my gameObject. At the time, I had three animations in the list, and the for-loop just executed the function for each animation in there.
Now I know the specific problem, I'm stuck on finding a solution. I don't know of any different way to call the animationState of my current animation, without using the for-loop that I'm using now. Does anyone know of any alternate ways of calling my animationState?
Cheers!