- Home /
Question by
AmazingDeveloper2 · Jan 03, 2015 at 10:26 PM ·
ontriggerstay
Can't catch event of the pressing the button
When my player stay inside the trigger and I press Space, I want to disable my GO and enable another GO. When I press Space again, I want to undo that action.
The problem is OnTriggerStay calls many times per frame, so sometimes I need to press Space more than one time to see result. I tried to fix that using coroutines, but it didn't helped.
public GameObject player;
public GameObject sleepingPlayer;
bool isSleeping;
void OnTriggerStay()
{
StartCoroutine ("PlayerSleeping");
}
IEnumerator PlayerSleeping()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(!isSleeping)
{
sleepingPlayer.SetActive(true);
player.GetComponent<MeshRenderer>().enabled = false;
player.GetComponent<PlayerMovement>().enabled = false;
isSleeping = true;
}
else
{
sleepingPlayer.SetActive(false);
player.GetComponent<MeshRenderer>().enabled = true;
player.GetComponent<PlayerMovement>().enabled = true;
isSleeping = false;
}
}
yield return new WaitForSeconds (0.5f);
}
Comment
Best Answer
Answer by DirusCanis · Jan 04, 2015 at 04:21 AM
try changing GetKeyDown to GetKeyUp and see what happens