Easiest way to pause your Unity game | TimeScale | Pause | Resume | Unity | @Unity3DSchool


PauseManager.cs
using UnityEngine;

public class PauseManager : MonoBehaviour
{
    bool isPaused = false;
    public GameObject PauseMenu;
    float timeScale;

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            if(isPaused) Resume();
            else Pause();
        }
    }

    void Pause()
    {
        isPaused = true;
        PauseMenu.SetActive(true);
        timeScale = Time.timeScale;
        Time.timeScale = 0.0f;
    }

    void Resume()
    {
        isPaused = false;
        PauseMenu.SetActive(false);
        Time.timeScale = timeScale;
    }
}

GameManager.cs
using System.Collections;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(DebugCoroutine());
    }

    void Update()
    {
        if (Time.timeScale == 0) return;
        Debug.Log("Update is running...");
    }

    void FixedUpdate()
    {
        Debug.Log("FixedUpdate is running...");
    }

    IEnumerator DebugCoroutine()
    {
        while(true)
        {
            yield return new WaitWhile(() => Time.timeScale == 0);
            yield return new WaitForEndOfFrame();
            Debug.Log("coroutine is running...");
        }
    }
}

Understanding Mathf functions - Lerp, MoveTowards, PingPong | Diff | Mathf | Unity | @Unity3DSchool


TestScript.cs
using UnityEngine;
using UnityEngine.UI;

public class TestScript : MonoBehaviour
{
    public Slider lerpSlider;
    public Slider moveTowardsSlider;
    public Slider pingPongSlider;

    void Update()
    {
        //Lerp
        lerpSlider.value = Mathf.Lerp(lerpSlider.value, lerpSlider.maxValue, 0.5f * Time.deltaTime);
        //MoveTowards
        moveTowardsSlider.value = Mathf.MoveTowards(moveTowardsSlider.value, moveTowardsSlider.maxValue, 10f * Time.deltaTime);
        //PingPong
        pingPongSlider.value = Mathf.PingPong(20 * Time.time, pingPongSlider.maxValue);
    }
}

Custom Event Node - Global Broadcasting | Script Graph | Visual Scripting | Unity | @Unity3DSchool