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);
}