Flip back a frame

Hello!

This one is probably easy but I am not sure how to frame the question so I’m not finding the answer. Basically, I am trying to make animation poses advance forward one frame or reverse back one frame depending on which button is touched. I have forward down easy, basically it’s state++; if (state == 1){ animation.Play("1"); } if (state == 2){ animation.CrossFade("2"); copy pasta copy pasta.
Now, how do I create a state-- if you will? I thought that would work but Unity just whistles and smirks at me. I’ve tried ±1 but this gets me that expressions error. I’ve tried looking it up but haven’t gotten very far. A little nudge in the right direction would be most helpful! Thanks!

Take a look at the animation's play speed property, maybe you could set it to -1/1 for a single frame and back to 0? http://unity3d.com/support/documentation/ScriptReference/AnimationState-speed.html

1 Answer

1

hey I know this is a super old question, but I just faced a similar problem and here’s the solution I came up with-- hope it helps someone! The short answer is: use the += and -= expressions to change the value of ‘state’.

 
var state = 0;
function Update () {
	if (Input.GetKeyDown(KeyCode.RightArrow)) {
	state += 1; 
	Debug.Log(state);
	}
	if (Input.GetKeyDown(KeyCode.LeftArrow)) {
	state -= 1; 
	Debug.Log(state);
	}
if (state==1) {
	animation.CrossFade("1",.2);
	animation["1"].speed = 0;
	animation["1"].weight = 1.0;
}
if (state==2) {
	animation.CrossFade("2",.2);
	animation["2"].speed = 0;
	animation["2"].weight = 1.0;
}
if (state==3) {
	animation.CrossFade("3",.2);
	animation["3"].speed = 0;
	animation["3"].weight = 1.0;
}
etc. etc.