vendredi 13 novembre 2020

Execute if-statement after fixed amount of time

I don't know if this is an easy question or not.

I'm trying to write a single function that slows down the player and returns them to normal speed after a certain amount of time. I founds some answers online but they all use a second function to set a timer, but I'm trying to contain the whole thing in one function.

I've already tried this:

public void slowDown(float slowAmount, float durationSlowed)
{
        var playerInfo = player.GetComponent<FPSMovement>();
        playerInfo.speed *= slowAmount;

        float lengthSlowed = Time.time + durationSlowed;

        if(Time.time > lengthSlowed)
        {
                playerInfo.speed /= slowAmount;
        }
}

and calling it with:

slowDown (0.5f, 2f)

It manages to slow down the player, but doesn't return their speed to normal. What am I missing here?

Aucun commentaire:

Enregistrer un commentaire