- Home /
Having problems with GetButtonDown missing button presses
Here's the code I have on the doors in my level with each door containing a collision trigger:
function Update () {
if(Input.GetButtonDown("Fire1"))
doorButtonPressed=true;
else
doorButtonPressed=false;
}
function OnTriggerEnter (collisionInfo: Collider)
{
if (collisionInfo.tag == "Player")
{
doorPromptRef.renderer.enabled = true;
doorPromptText.text = "[X] ENTER";
}
}
function OnTriggerStay (collisionInfo: Collider)
{
if (collisionInfo.tag == "Player")
{
if(doorButtonPressed)
{
GoToRoom();
}
}
}
This works 90% of the time, but occasionally it will miss a button press (I test by rapidly going back and fourth between two doors).
I suspect this is occurring because Unity Script reference states "OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger." But I don't know of a more proper way to go about handling this and don't want to make a hacky workaround yet.
I humored changing GetButtonDown to just GetButton, but if the player holds down the button they will warp back and fourth between doors too rapidly.
On a random side note that may or may not be relevant: I'm using ex2D.
Answer by gregzo · Apr 18, 2012 at 09:42 AM
What about doing it the other way around? OnTriggerEnter, boolean inFrontOfDoor becomes true, OnTriggerExit it reverts to false. Then, check this bool when Fire1 is pressed... That way, you wouldn't even need OnTriggerStay.
Well damn, that works perfectly. Thanks! I feel sorta silly now.
Your answer
Follow this Question
Related Questions
OnTriggerStay simple question 1 Answer
Detect 2 colliders by trigger 1 Answer
OnTriggerStay to detect objects inside of another 2 Answers
Problem with OnTriggerStay2D function 2 Answers