// ---------------------------------------------------------------------------------- // Method: Start() // Description: Cache components // ---------------------------------------------------------------------------------- private void Start() { _playerWeaponScript = FindObjectOfType <FPSRigAnimator>(); PlayerStats.CanPauseGame = true; // make sure we re-enable the ability to pause the game if this object becomes active in a scene // This is because pausing the game during the scene load can lead to glitches like a black screen/things not loading correctly originalFOV = Camera.main.fieldOfView; }
// --------------------------------------------------------------------------------------------- // IEnumerator: PauseTheGame // Description: Pauses the game by incrementally slowing the timescale until it reaches epsiolon, // then activates pause canvas UI elements, also plays SFX. INcrementally decreases the main // camera's FOV to mimic a zooming in effect as the timescale is slowed // --------------------------------------------------------------------------------------------- public IEnumerator PauseTheGame() { // Boolean switch to prevent starting the coroutine if it is already running: if (isPausing) { yield return(null); } isPausing = true; _slowDownSoundEffect.PlayOneShot(_slowDownSoundEffect.clip); Camera cam = Camera.main; float targetFOV = cam.fieldOfView - 8.0f; // the target FOV to move towards float targetTimeScale = Mathf.Epsilon; // the target timescalse - I found issues were caused when setting it to 0, so I use epsilon // Tried a few different ways to facillitate a slow-down and zoom-in effect like lerping and smoothStepping // eventually settled on using a stepcounter loop with the Mathf.SmoothStep method, then snapping the value just in case: int stepCounter = 0; while (stepCounter != 6) { cam.fieldOfView = Mathf.SmoothStep(cam.fieldOfView, targetFOV, 0.25f); Time.timeScale = Mathf.SmoothStep(Time.timeScale, targetTimeScale, 0.25f); stepCounter += 1; yield return(new WaitForSeconds(0.01f)); } // snap the timescale and fov just in case Time.timeScale = Mathf.Epsilon; _enableSoundEffect.PlayOneShot(_enableSoundEffect.clip); // In the start area scene, the player game object is inactive when this script's Start() method is called // So perform a findobjectoftype call just in case we do not already have a reference to the player if (!_playerWeaponScript) { _playerWeaponScript = FindObjectOfType <FPSRigAnimator>(); // If we still cannot get a reference to the player something has gone wrong and we should return // It's highly unlikely we'lll enter into this conditional though if (!_playerWeaponScript) { yield return(null); } } _playerWeaponScript.CanFire = false; _pauseMenuActive = true; foreach (GameObject item in MenuItems) { item.SetActive(true); } Cursor.lockState = CursorLockMode.None; Cursor.visible = true; isPausing = false; // reset the boolean switch yield return(null); }