freeze
Pozastavení hry
void Start()
{
StartCoroutine(Freeze(3));
}
IEnumerator Freeze( float time)
{
float timer = 0f;
// float time = 5f;
while (timer < time)
{
timer += Time.deltaTime;
yield return null;
}
//po pauze další kód
}
Třída na freeze hry
using System.Collections;
using UnityEngine;
public class Freeze : MonoBehaviour
{
[Range(0, 1.5f)]
public float duration = 1f;
private bool isFreeze = false;
private float timeFreeze = 0f;
void Update()
{
if (timeFreeze > 0 && !isFreeze)
StartCoroutine(DoFreeze());
}
public void StartFreeze()
{
timeFreeze = duration;
}
IEnumerator DoFreeze()
{
isFreeze = true;
var original = Time.timeScale;
//Time.timeScale = 0.000001f;
//yield return new WaitForSeconds(2.0f * Time.timeScale);
Time.timeScale = 0f;
yield return new WaitForSecondsRealtime(2.0f);
Time.timeScale = original;
timeFreeze = 0;
isFreeze = false;
}
}
Menu + pauze hry
//menu ve hře
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Time.timeScale == 0)
{
Time.timeScale = 1;
Menu.SetActive(false);
pauze = false;
}
else
{
Time.timeScale = 0;
Menu.SetActive(true);
pauze = true; //zablokování kláves ovládání a střelby
}
}