- Home /
Question by
KillerCat · Jul 28, 2017 at 01:36 AM ·
collisioncoroutines
How Can I get this Coroutine to Work with my OnCollisionEnter?
I want this to wait the 5 seconds then lower the speed. I have tried putting it into a whole coroutine but the collision variable from the OnCollisionEnter doesn't carry on.
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "PowerUp" && CanPowerUp == true)
{
CanPowerUp = false;
collision.transform.position -= new Vector3 (0, 100, 0);
print ("You have increased your speed");
jumpSpeed += 5f;
StartCoroutine (Waiter);
jumpSpeed -= 5f;
Debug.Log ("Speed has decreased");
collision.transform.position += new Vector3 (0, 100, 0);
CanPowerUp = true;
}
}
IEnumerator Waiter()
{
yield return new WaitForSeconds (5f);
}
Comment
Answer by bobisgod234 · Jul 28, 2017 at 02:36 AM
StartCoroutine (Waiter());
Will start the Coroutine (note the ()'s). In the Coroutine Waiter, anything after the yield will execute 5 seconds later, so move code that you want to execute 5 seconds later into the Coroutine.
IEnumerator Waiter()
{
yield return new WaitForSeconds (5f);
jumpSpeed -= 5f;
}