- Home /
Animation replaying every frame...
This is my code to play animation on keypress (hold):
if(Input.GetKey("w") && Input.GetKey("left shift")){ GetComponent.().Play("runfront"); }
It is working but not correctly. It stays like this:
Plays one frame... I think it is from getkey codes but I tried getbutton, getbuttondown, getkeydown combinations. Only getkey plays animation. Others don't.
Answer by Nick4 · Jun 26, 2016 at 01:23 PM
That's because every frame while you hold down w and shift buttons your animation starts to play. I don't remember the exact variables for animation class but it should look something like this :
void Start() {
anim = GetComponent<Animation>();
}
void Update() {
if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode)) {
if(anim.isPlaying == false) {
anim.Play();
}
}
}
That way your animation won't try to play again while it's playing.
The name 'anim' does not exist in the current context. Change: The classic error, unexpected token: ).
Nick4