I dont understand why this doesn’t work. All i am trying to do is pause the game for one sec OnTriggerEnter:
using UnityEngine;
using System.Collections;
public class LevelInfo : MonoBehaviour {
public bool pause;
void Update()
{
if (pause){
Time.timeScale = 0;
}
if (!pause){
Time.timeScale = 1;
}
}
void OnTriggerEnter (Collider col){
if (col.gameObject.tag == "Player"){
StartCoroutine(pauseTime());
print ("Pause in action");
}
}
IEnumerator pauseTime(){
pause = true;
yield return new WaitForSeconds(1f);
pause = false;
}
}
Thank you
WaitForSeconds
runs at Time.timeScale
.
As you set timeScale
to 0 it will never stop waiting.
More detail and a couple of solutions can be found here.
Here is an implementation that monitors Time.realtimeSinceStartup
:
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour
{
private float pauseUntil;
private bool paused;
void Update() {
if (paused) CheckPause();
}
void OnTriggerEnter(Collider col) {
if (col.gameObject.tag == "Player") {
PauseForDuration(1.0f);
}
}
void PauseForDuration(float duration) {
paused = true;
pauseUntil = Time.realtimeSinceStartup + duration;
Time.timeScale = 0.0f;
}
void CheckPause() {
if (Time.realtimeSinceStartup >= pauseUntil) {
Time.timeScale = 1.0f;
paused=false;
}
}
}