Boost over time

Hi,

I want to have an item, if the player collects this item he should get a boost for 5 seconds. My Script so far:

var SpeedIncrease : int;

function OnTriggerEnter (collisionInfo : Collider){
// If Player Collides with State Blue barrier gets destroyed
if (collisionInfo.gameObject.tag == "Player" && CharacterScript.PlayerState == 1)
    {
        MoveLevelScript.speed += SpeedIncrease;
        Destroy(gameObject);
    } 
}

The script works, so it increases the speed but how do i tell it to set it back to normal after 5 seconds ?

2 Answers

2

MoveLevelScript.Invoke( “BackToNormal”, 5 );

I learn something new everytime i get on here XD I've never heard of Invoke. Thank you for introducing that to me

I learned about it here too :p

this might work:

yield WaitForSeconds (5);
MoveLevelScript.speed -= SpeedIncrease;

if not, you might want to go and make a seperate function that is called when you get the item, like

if (collisionInfo.gameObject.tag == "Player" && CharacterScript.PlayerState == 1)
    {
        SpeedIncreaseTemp(SpeedIncrease)
        Destroy(gameObject);
    } 
}
function SpeedIncreaseTemp(SpeedIncrease){
    MoveLevelScript.speed += SpeedIncrease;
    yield WaitForSeconds (5);
    MoveLevelScript.speed -= SpeedIncrease;
}

you might have to put that function in a seperate script and call it.

thx for the answers, unfortunately the decreasing of the speed does not really work :(