- Home /
Animation play on input, else play idle animation.
function Update () {
if (Input.GetKeyDown("d")) {
animation.Play("Walking");
}
else animation.Play("Standing");
}
}
This is the code i´ve come up with(i´m very new to scripting). It doesn´t play the Walking animation and apparently can´t find the Standing animation, but plays it anyway. Any help is appreciated, thanks in advance! :)
Answer by DESTRUKTORR · Aug 15, 2012 at 03:07 PM
For the walking animation:
Use `Input.GetKey("d")`. `Input.GetKeyDown("d")` would only execute once, as the key is detected as being pressed down, whereas `Input.GetKey("d")` will return true for every update that the key is down.
As for the "Standing" animation, make sure the name is correct, and the animation is accessible to the component.
[EDIT] You also may want to add
if(Input.GetKeyUp("d"))
{
animation.Stop("Walking");
}
Before switching animations.
If i want it to activate on my "input" called Horizontal (that has a negative and positive pushforce for movement) ?
To use buttons, just change it to
if(Input.GetButton("buttonName")
and
if(Input.GetButtonUp("buttonName")
If I'm reading this correctly, you would just replace "buttonName" with "Horizontal"
[EDIT] And by buttons, I mean named inputs, from the input panel (they're called "buttons" for some reason...).
Answer by AMDAndrew · Aug 15, 2012 at 03:46 PM
var walking : boolean = false;
..
if(input.GetButtonDown("w")) {
walking = true;
if(walkin == true){
animation.CrossFade("walking");
animation.Play();
}
else if (walking == false){
animation.CrossFade("idle");
animation.Play();
}
}
Your answer
Follow this Question
Related Questions
Punch Animation Doesn't Work And Others Do... 1 Answer
Animation play when dead 2 Answers
Java Script: Clock script help. 1 Answer
Animation Script Error 1 Answer
Animation Script not working. 1 Answer