- Home /
Input.GetKey
I have a Sword in my game with an animation right now and I want it so that when I press x, It plays the animation. I have wrote this
function Update () { if (Input.GetKey("x")) GetComponent(Animation).enabled = true; }
and it doesnt give me errors so what did I do wrong?
And is it possible to have two animations for the same gameobject but different keys?
you could use animation events and play the animation at the events, or you can just call the separate animations using animation references check the link,
Answer by reptilebeats · Aug 04, 2012 at 05:50 PM
you need to use animation.Play();
http://docs.unity3d.com/Documentation/ScriptReference/Animation.Play.html
So it would be
function Update () {
if (Input.Get$$anonymous$$ey("x"))
animation.Play("sword");
}
if I had the animation named sword?
I need to add to this solution.
The Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown, will make you play the animation over and over without waiting for the animation to end. The simple solution for this is as such:
if(Input.Get$$anonymous$$ey("x") && !animation.IsPlaying("sword"))
It will only play the animation again after it is over. Also, you may want to try using animation.CrossFade("sword"); for smoother results.
yh there is a lot of things you can do with animation check the functions to the right of the link i gave.