Input script
I am trying to set up so that when I walk into a trigger, you press "e" key to enable an object and it's not working.
What exactly is wrong with my code.
#pragma strict
var obj : GameObject;
function Start () {
obj.gameObject.SetActive(false);
}
function OnTriggerEnter (player : Collider)
{
//gameObject.SetActive(true);
if (Input.GetKey("e")){
obj.gameObject.SetActive (true);
Destroy(gameObject);
}
}
Answer by hexagonius · Oct 26, 2016 at 06:58 AM
Try holding e while walking into the trigger and it will work. The problem is, that OnTriggerEnter is executed once and that's when you need the key also down. If you want e to work as long as you "stay" inside the trigger, use OnTriggerStay and GetKeyDown. This way the code gets constantly executed and just listens for a key press. GetKey would just spam each frame the key is down, and that's a lot during the time a user just presses the key, considering the high framerate.
Can it be done where it only accepts input when in the trigger?
well, that's how it works now. OnTriggerStay is called as long as in the trigger. Thats when it works. Or what do you mean?
Your answer
Follow this Question
Related Questions
Specific key combination not working 0 Answers
Differentiate between types of key press 1 Answer
Dance pad button input stays false 0 Answers
Why unity not working after launching .exe but works when build and run? 0 Answers
How to fix the problem 0 Answers