In most games, it is desirable to interrupt the game at some point in order to do something else, such as taking a break or changing options.
Steps
- Create script Pause.cs
using UnityEngine;
public class Pause : MonoBehaviour
{
[SerializeField] private KeyCode _key = KeyCode.Escape;
private bool _isPause;
private void Update()
{
if (Input.GetKeyDown(_key))
{
Time.timeScale = _isPause ? 1.0f : 0.0f;
_isPause = !_isPause;
}
}
}
- Create canvas in it create empty object add text.
- Add script Pause.cs to Pause obejct.
- Select key to pause, default key is escape.