- Home /
How do I play an animation with the 'E' key when I enter a trigger
Hi i'm making a horror game an I download the free Locker asset from the asset store. It comes with an Open and Close animation. I made a box collider trigger so when the player enters the trigger, if the E key is pressed the animation will play. The script I have is not working please help! Iv put the script on the trigger, and added the locker to the GameObject variable, and the open animation to the AnimationClip variable.
var open: AnimationClip;
var door: GameObject;
function OnTriggerEnter (){
if (Input.GetKeyDown("e"))
{
animation.Play("open");
}
}
have you tried OnTriggerStay()? I believe OnTriggerEnter() is only called on the frame when the two colliders touch rather than during the time they are touching.
Scribe
hmm okay I think that might work, I tried it but I got this after testing it:
$$anonymous$$issingComponentException: There is no 'Animation' attached to the "LockerTrigger" game object, but a script is trying to access it. You probably need to add a Animation to the game object "LockerTrigger". Or your script needs to check if the component is attached before using it.
Have you selected an animation for your 'open' variable in the hierachy?
The game object containing this script must have an animation called "open". your variables "open" and "door" are useless here.
Answer by KiraSensei · Jun 08, 2013 at 12:26 PM
So this game object is useless.
Attach your box collider, your script and the animation to the game object that needs to be animated.
Answer by Cence99 · Jun 09, 2013 at 03:18 PM
I think the problem is, that the AnimationClip variable is senseless (you're not using it anywhere) and you should simply drag the clip "open" into the Animation component of your Door. Then you should be able to call "animation.Play("open");" or "animation.PlayOneShot("open");"
Also note that one of the GameObjects needs a Rigidbody to make OnTrigger-functions work.
Your answer
Follow this Question
Related Questions
Open door with key 2 Answers
Starting animation with collision 0 Answers
play animation when press key 2 Answers
Trigger enabling keys 1 Answer
Seven key press 3 Answers