I am new to Unity and have no idea how to solve this.
I tried to make an speed up game object for 5 seconds after touching a note. I tried both function but nothing seems to work.
Everything posted here is in MonoBehavior.
private bool isSpeedUp = false;
private float timeSpeedUp = 1f;
public float defaultTimeSpeedUp = 5f;
private float DefaultMoveSpeed;
private float DefaultTurnSpeed;
private float starttimer = 0f;
public void ZombieTouch ()
{
Transform goldnote = transform.GetChild (0);
goldnote.collider2D.enabled = false;
goldnote.GetComponent<Animator> ().SetBool ("CollectNote", true);
isSpeedUp = true;
DefaultMoveSpeed = Common.ZombiemoveSpeed;
DefaultTurnSpeed = Common.ZombieturnSpeed;
Debug.Log ("Zombie Speed -1 " + Common.ZombiemoveSpeed);
StartCoroutine (SpeedUp ());
SpeedUp2 ();
}
IEnumerator SpeedUp()
{
Debug.Log ("SpeedUp coroutine run");
Debug.Log ("Zombie Speed CR 0 " + Common.ZombiemoveSpeed);
Common.ZombiemoveSpeed = DefaultMoveSpeed + 2;
Common.ZombieturnSpeed = DefaultTurnSpeed + 2;
Debug.Log ("Zombie Speed CR 1 " + Common.ZombiemoveSpeed);
yield return new WaitForSeconds(5);
Debug.Log ("Waiting...");
SpeedDown();
}
void SpeedUp2 ()
{
Debug.Log ("Zombie Speed IV 0 " + Common.ZombiemoveSpeed);
Common.ZombiemoveSpeed = DefaultMoveSpeed + 2;
Common.ZombieturnSpeed = DefaultTurnSpeed + 2;
Debug.Log ("Zombie Speed IV 1 " + Common.ZombiemoveSpeed);
Invoke ("SpeedDown", timeSpeedUp);
Debug.Log ("Invoke striked");
}
void SpeedDown ()
{
Debug.Log ("Speed down called");
Common.ZombiemoveSpeed = Common.defaultMoveSpeed;
Common.ZombieturnSpeed = Common.defaultTurnSpeed;
Debug.Log ("Zombie Speed2 " + Common.ZombiemoveSpeed);
isSpeedUp = false;
}
I posted both methods. Both of which does not call SpeedDown function. When the game is run:
-
the log doesn’t show “Waiting…” that I place after WaitforSeconds method
-
the log does show “Invoke striked”
-
speed of object increase as intended, it should because SpeedUp is called properly.
-
I tried to use only one at a time but the result is the same as described above.
Please help, I am at my wits end.