I have a class which is changing the player speed. Here is the code:
using UnityEngine;
using System.Collections;
public class MultiplySpeed : MonoBehaviour {
public int multiplier = 2;
public Controls player;
bool flag = false;
void OnTriggerExit(Collider c){
if(c.tag == "Player"){
player = c.gameObject.GetComponent<Controls>();
if(!flag){
multiplySpeed();
StartCoroutine(powerUp());
}
flag = true;
}
}
public IEnumerator powerUp(){
yield return new WaitForSeconds(10);
backToNormal ();
StopCoroutine(powerUp ());
}
public void multiplySpeed(){
player.speed = player.speed * multiplier;
}
public void backToNormal(){
player.speed = player.speed/ multiplier;
}
}
I have two problems, the first one is that after starting the coroutine and the flag varaible is set to true, the method OnTriggerExit is called once again, and the flag is false and everything is executed again. The second problem is that the method backToNormal seems like it's never executed, it simply multiplies the speed and never take it back to normal.
Any help is appreciated.