- Home /
creating a Temporary powerup
I'm new to coding in unity and I created a simple powerup that increases the characters speed when picked up and it works but I don't know how to remove the effects after a certain amount of time. Any help is appreciated.
Answer by Anonymous620 · Jul 08, 2021 at 11:52 PM
IEnumerator Deactivate()
{
playerScript.jumpspeed += increase;
yield return new WaitForSeconds(5);
playerScript.jumpspeed -= increase;
}
Call this function when your player picks up the power up. change the 5 in waitforseconds to the time you want the powerup to last Edit: you will need to call the function in a start couritine
I've placed the code you provided inside my code and don't receive any errors which is good. it doesn't have the timer working yet but it still increases the jump speed. I am however uncertain how to call the function for start couritine everything I've tried gives errors as a beginner I'm not sure how to use it.
Sorry, i should have explained better, the IEnumerator gets placed like a new void like this
void Update()
{
StartCoroutine(Deactivate());
}
IEnumerator Deactivate()
{
playerScript.jumpspeed += increase;
yield return new WaitForSeconds(5);
playerScript.jumpspeed -= increase;
}
you start a couritine like this:
StartCoroutine(Deactivate());
the code works great thank you, I've been trying to get this to work for some time.