- Home /
How do I make animation go to end frame, and back to start?
Okay, so... 'Detailed' Question.
I'm trying to make Animation go to end frame (40) and when UI button is pressed, go back to start (0).
Now, I'm trying to use
GetComponent.<Animation>()["Animation"].time >= 1.5|| GetComponent.<Animation>()["Animation"].time <=0.0)
{
GetComponent.<Animation>()["Animation"].speed = 0;
}
Its in FixedUpdate BTW
The problem is that if time goes more than 1.5, or below 0.0 too much. Making the animation speed 1 again doesn't work, because it is stopped instantly by this.
I'll show it by video. It happens around 01:15 minutes
It may not seem that big a problem, but it happens randomly, and may be more common on slower PC's.
Help?
Seems like I have to just post the whole code. Its easier for you to understand.
#pragma strict
private var backward: int = 0;
var MainCam: Animation;
var Mainmenu: Animation;
var NewGameMenu: Animation;
function Play()
{
MainCam.GetComponent.<Animation>()["MainMenuCamRotate"].speed = 1;
Mainmenu.GetComponent.<Animation>()["MainMenu1"].speed = 1;
NewGameMenu.GetComponent.<Animation>()["NewGameMenu1"].speed = 1;
MainCam.GetComponent.<Animation>().Play();
Mainmenu.GetComponent.<Animation>().Play();
NewGameMenu.GetComponent.<Animation>().Play();
}
function Stop()
{
backward = 1;
}
function Update(){
if(backward==1)
{
MainCam.GetComponent.<Animation>()["MainMenuCamRotate"].speed = -1;
Mainmenu.GetComponent.<Animation>()["MainMenu1"].speed = -1;
NewGameMenu.GetComponent.<Animation>()["NewGameMenu1"].speed = -1;
backward = 0;
}
}
function FixedUpdate()
{
if(MainCam.GetComponent.<Animation>()["MainMenuCamRotate"].time >= 1.5 || MainCam.GetComponent.<Animation>()["MainMenuCamRotate"].time <=-0.1)
{
MainCam.GetComponent.<Animation>()["MainMenuCamRotate"].speed = 0;
Mainmenu.GetComponent.<Animation>()["MainMenu1"].speed = 0;
NewGameMenu.GetComponent.<Animation>()["NewGameMenu1"].speed = 0;
}
}
For the Legacy system this is a common question. Try Ping-Pong. You can also change 1 to -1 speed.
I'll explain again, this time I'll try.
I'm making $$anonymous$$ain $$anonymous$$enu, and when you click New Game ui button, 3 animations happen, 1. Camera rotates 180 degrees and shows planet. 2. $$anonymous$$ain $$anonymous$$enu slides to the right and disappears. 3. New Game options come from the right.
The New Game options has back button, as you may saw on the video. When you press the Back button, it rewinds, plays the animations back to start.
I hope that is more clear than first, haven't done English in a while.
Answer by Lilbob · Mar 14, 2015 at 03:47 PM
Try AnimationState.normalizedTime = 1; 0 will be the first frame, 1 will be the last, 0.5 the middle. Then use AnimationState.time = 0 to reset the animation.
Cheers
Thanks for answers Lilbob and meat5000, they didn't work really but gave me idea. Thank you.
Nothing seems to work. I'll just add button to reset animation if it breaks for some reason.
Thanks for answers meat5000 and Lilbob, they gave me idea how else I could do this.
Answer by meat5000 · Mar 15, 2015 at 09:14 PM
I still dont understand how you cant get this to work ;)
You can also change 1 to -1 speed.
http://docs.unity3d.com/ScriptReference/AnimationState-speed.html
(Or to rewind without playing, this)