- Home /
Animation Played when key pressed Help
I want to make a horse play it's idle animation when no key is pressed, it's walking animation when W, A, S, or D is pressed, and its running animation when the left shift is pressed. I just want to know how my script should be changed in order for this to happen.
pragma strict
public var Run : AnimationClip;
public var Walk : AnimationClip;
public var Idle : AnimationClip;
function Update () {
GetComponent.<>(Idle).Play();
if( Input.GetKeyDown("W")){
GetComponent.<Animation>().Play();
}
if( Input.GetKeyDown("A")){
GetComponent.<Animation>().Play();
}
if( Input.GetKeyDown("S")){
GetComponent.<Animation>().Play();
}
if( Input.GetKeyDown("D")){
GetComponent.<Animation>().Play();
}
if( Input.GetKeyDown("LeftShift")){
GetComponent.<Animation>().Play();
}
}
Answer by crohr · Jun 08, 2015 at 07:16 AM
First off, I suggest caching the reference to the Animation component in Start. But to play the idle animation simply call animation.Play("idle"). So long as the Animation contains a clip with the name "idle" the animation with play.
I tried animation.PLay but it says that is is obsolete. It makes me use GetComponent.().Play("Idle"); ins$$anonymous$$d. But that doesnt work either. It also wont let me add in the Run, Walk, and Idle animations in the inspector
Does the animation you are trying to call contain all the animations? Because if you use did GetComponent().Play("idle") should work.
It doesnt, I am trying to call 3 separate animations. I tried to put all 3 animations in an animator but I cant add the animator in the by removing the three vars and adding a public animator var (public car animator : Animator;).