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

YoutubeVideo

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;
}
}

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

I still dont understand how you cant get this to work :wink:

You can also change 1 to -1 speed.

http://docs.unity3d.com/ScriptReference/AnimationState-speed.html

(Or to rewind without playing, this)