I have successfully created a slow motion power up and i thought this script would allow me to set the time scale at any speed I wanted, but if I set the timescale at 1.5 the timescale never returns back to 1. If I set the timescale to 0.5 it works fine. I also tried dividing the duration variable (in the slomoroutine) like so: duration=time/speed. Still nothing. What could be causing this? Help would appreciated.
Here's my code:
using UnityEngine;
using System.Collections;
public class TimeChangingPowerUp : MonoBehaviour {
public GameObject particle;
public AudioClip powerUpSound;
public float time;
public float speed;
//private bool Active=false;
//private bool playerEntered;
/*public void ActivatePowerUpSlowMotion(float time, float speed)
{
StartCoroutine (SlowMoRoutine (time, speed));
}*/
/*void Update(){
if (Active) {
Destroy (gameObject, 3);
}
}*/
IEnumerator SlowMoRoutine(float time, float speed)
{
//Active = true;
//time = Time.unscaledTime;
//Destroy(gameObject );
//float duration = time / speed;
float duration = time * speed;
Time.timeScale = speed;
yield return new WaitForSeconds (duration);
Time.timeScale = 1f;
Destroy (gameObject);
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
//Active = true;
GameObject clonedParticle;
clonedParticle =Instantiate (particle, transform.position, transform.rotation) as GameObject ;
GameObject sound = new GameObject ();
sound.transform.position = transform.position;
AudioSource audioSource = sound.AddComponent ();
audioSource.clip = powerUpSound;
audioSource.Play ();
Destroy (clonedParticle, 1.2f);
Destroy (sound, powerUpSound.length);
gameObject.GetComponent ().enabled = false;
StartCoroutine (SlowMoRoutine (time, speed));
}
}
}
↧