- Home /
Play animation with key once
Hey everyone.
I have a cube with the trigger is checked and ik drawed this JS into this GameObject.
var DoorGameObject : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyDown("e") && !HasTriggerBeenUsed) {
DoorGameObject.animation.Play("HouseDoor1");
setTrigger = true;
}
}
And i made sure the object is selected with the variable. Everything works fine. But there is only one problem. Everytime i'm in the trigger, and i press the "E" key. The animation plays. But what i want is, if i pressed the "E" key and the animation has played. The animation cant play for the second time. Only once. So i thought maybe i can destrot the cube after the "E" key. so can someone help me with this how to do that?
Sorry for my English. I'm a MaroccanDutchman ;)
Hope to hear soon about you :D
Answer by Owen-Reynolds · Oct 23, 2011 at 03:20 AM
You're not setting the same var you check. Pick one and stick with it. You set setTrigger
to true, so change the if to use it: if(Input.GetKeyDown("e") && !setTrigger)
.