dimanche 24 mai 2020

How to Check the time a player is below a y-Coordinate in C#

I want my script to check how long the player is below a given y coordinate. However, since I am checking for the information inside a FixedUpdate void I cannot directly add a while loop. therefore, I tried the following:

void FixedUpdate()
{
if(rb.position.y < 1f)
        {
            checkIfLost();
        }
}

IEnumerator checkIfLost()
    {
        while(rb.position.y < 1f)
        {
            float timeGiven = 5 - Time.deltaTime;

            if(timeGiven <= 0)
            {
                FindObjectOfType<GameManager>().EndGame();
            }

            yield return null;
        }
    }

This does not work. I am new to unity C#, and I have tried searching it online but could not find anything.

What is a better alternative for running the while loop and checking how long the player is below a y-coordinate?

Aucun commentaire:

Enregistrer un commentaire