- Home /
Proper way to play non looping animations
I animated a character driving a boat with an idle, drive, throttle down animation. The throttle down animation should play once when the s key is pressed, then play the drive animation once. What's happening is the throttle down keeps playing in a loop while the s key is down. I chose Once for the Wrapmode on throttle down, and loop for drive. Below is my code:
function Update () {
if(Input.GetKey(KeyCode.W)){
animation.Play("drive");
}else if(Input.GetKey(KeyCode.S) ){
animation.Play("throttledown");
}
if(!Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical")){
animation.CrossFade("idle");}
}
Answer by Owen-Reynolds · Mar 18, 2012 at 02:32 AM
GetKey is the "key is being held down" check, so this code should play throttleDown over and over. Play is smart enough not to restart, but as soon as it ends, the key being held starts it again. GetKeyDown is the "once/press" check.
Also, Play usually gives some jerks at the start/stop. Often CrossFade("throttleDown",0.05);
looks nicer (fades in/out over 1/20th of a second, which is about 3 frames.)
A trick for "idle" is to put in on a lower layer and leave it always playing. That way you can ignore it, and have it automatically be playing if nothing else is.
Answer by raoz · Mar 18, 2012 at 04:58 AM
function Update () {
if(Input.GetKey(KeyCode.W)){
animation.Play("drive");
}else if(Input.GetKeyDown(KeyCode.S) ){
animation.Play("throttledown");
}
if(!Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical")){
animation.CrossFade("idle");}
}
Your answer
Follow this Question
Related Questions
Why cant i use animations on my character? 1 Answer
animation play in game over 1 Answer
How to set legacy on an animation? 1 Answer
Directing character's fist to a specific position 1 Answer
Character Animation Import Problem 0 Answers