- Home /
Trying to play an animation using the 'E' key.
I'm trying to animate a tree I have using the 'E' key. I have 3 separate animations for it; Idle, Fall, and Down (Down meaning it stays down).
This is the code I have:
using UnityEngine; using System.Collections;
public class TreeFall : MonoBehaviour {
public GUIText textHints;
bool Fall = false;
void Start () {
}
void OnTriggerEnter(Collider col){
textHints.SendMessage ("ShowHint", "\n\n\n\n\n That tree looks like it can be pulled down.");
if(col.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E)){
print ("player has pressed e");
animation.Play("Fall");
}
}
void Update () {
}
}
The box collider I'm using works and everything, I've used 'print' to help me ensure this, it's just that whenever I enter the triggerzone, and hit the 'e' key, nothing happens, and my animation doesn't play. What am I doing wrong?
Answer by ExtremePowers · Nov 05, 2014 at 04:26 PM
OnTriggerEnter is only called once upon each contact point. So what your code is doing is checking if the User press the key at the same time of a collision with the target. Use OnTriggerStay instead.
Thank you. Ironically however, I figured it out. Turns out I was putting Input.Get$$anonymous$$eyDown in the wrong spot. Ins$$anonymous$$d of putting it in the OnTriggerEnter, I only had to put it in the Void Update.