I cannot figure out how to make my timer work! If the player collides with the slowdown powerup (slowing all objects on the screen) the slowdown continues forever even though I have a timer setup. Do I have the timer in the wrong spot in Update? Does this have something to do with how I have variables setup and Time.timescale? I have looked on unity answers, youtube, and google for days, but there is nothing. This cannot be that difficult and I am so close! This is driving me crazy!
Here is my code:
Code 1:
using UnityEngine;
using System.Collections;
public class PowerUpManager : MonoBehaviour {
private bool doublePoints;
private bool slowMo;
private bool fastMo;
private bool powerUpActive;
private float powerUpLengthCounter;
private ScoreManager theScoreManager;
private float score;
private float slowDown = Time.timeScale ;
//private float slowDown = Time.unscaledDeltaTime ;
// Use this for initialization
void Start () {
theScoreManager = FindObjectOfType <ScoreManager > ();
}
// Update is called once per frame
void Update () {
if (powerUpActive) {
powerUpLengthCounter -= Time.deltaTime;
if (slowMo) {
//Time.unscaledDeltaTime = slowDown;
Time.timeScale = slowDown;
slowDown = 0.5f;
}
}
if (powerUpLengthCounter <= 0) {
//Time.unscaledDeltaTime = slowDown;
Time.timeScale = slowDown;
slowDown = 1f;
powerUpActive = false;
}
}
public void ActivatePowerUpSlowMotion(bool slow, float time, float effect){
slowMo = slow;
powerUpLengthCounter = time ;
slowDown = effect;
powerUpActive = true;
}
}
Code 2:
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour {
public bool doublePoints;
public bool slow;
public bool fast;
public float speed=Time .timeScale ;
public float powerUpLength;
private PowerUpManager thePowerUpManager;
// Use this for initialization
void Start () {
thePowerUpManager = FindObjectOfType <PowerUpManager > ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
thePowerUpManager.ActivatePowerUpSlowMotion (slow, powerUpLength,speed );
}
gameObject.SetActive (false);
Destroy(gameObject);
}
}