Simple activate animation w/ button script

Hey
I’ve been working on a script which allows me to active an animation by interacting with a button. Ideally I’d like the player to use “E” to activate/press the button. (The characters )

I’ve created the button, added a box collider to detect the player and attached the animation… I’ve create the script which compiles with no errors… But nothing happens once I test/play the game… (execpt for the “OnTriggerExit” function)

Here’s my script so far:

#pragma strict

var clip : AnimationClip;
var clip2 : AnimationClip;

function Start () {

}

function OnTriggerStay (other : Collider) {
if (other.CompareTag (“Player”)){

if(Input.GetKeyDown(“E”))
animation.Play(clip.name);

}}

function OnTriggerExit () {

animation.Play(clip2.name);
}

Any Help would be greatly appreciated!

Away from computer so can’t trial anything but maybe one of the following could help (all just ideas)
1 change get key down to key up
2 keycode.e instead of “E”
3 try setting a variable to true in ontriggerenter then in the update frame you can use if(theVariable) then if (keycode.e) etc…

Will try have a better look when I get home!!

Thanks for the reply!
It got my creativity going!

This is what I have so far. I think, I’m close.. But I hit a wall… any ideas?


#pragma strict

var clip : AnimationClip;
var clip2 : AnimationClip;

var countdown : float;
var buttonPressed = false;

function Update () {

//If player is in button box collider
function OnTriggerStay (other : Collider)
{
if (other.CompareTag (“Player”))
{
if(Input.GetKeyDown(KeyCode.e))
{
countdown = 5.0;
buttonPressed = true;
}
}
if(buttonPressed)
{
animation.Play(clip.name);
if(countdown <= 0.0)
{
buttonPressed = false;
}
}
else
{
animation.Play(clip2.name);
}
}
}