Home > Blockchain >  How to freeze camera when the game is paused in Unity?
How to freeze camera when the game is paused in Unity?

Time:10-22

So recently I started coding my first FPS game. I experienced a problem with my pause menu. The problem is when I have my game paused my mouse is still controling the camera and when I want to press some buttons in menu camera keeps following my mouse. I searched for solution to this problem on web, but I haven't found the solution (even my code is similar to some I've found).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;



public class PauseMenu : MonoBehaviour
{
    public static bool gameIsPaused;
    public GameObject pauseMenuUI;
    

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


    }

    public void Resume()
    {
        Cursor.lockState = CursorLockMode.Locked;
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;

        gameIsPaused = false;
    }
    void Pause()
    {
        Cursor.lockState = CursorLockMode.None;
        pauseMenuUI.SetActive(true);
        gameIsPaused=true;
        
        Time.timeScale = 0f;
    }

    public void LoadMenu()

    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("Menu");
    }

    public void QuitGame()
    {
        Debug.Log("Quitting game...");
        Application.Quit();
    }
}

CodePudding user response:

So i've just copied my entire CameraRot script here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotation : MonoBehaviour
{
    public float sensX;
    public float sensY;
    public Transform orientation;

    float xRotation;
    float yRotation;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    void Update()
    {
        
        float mouseX = Input.GetAxis("Mouse X") * Time.fixedDeltaTime * sensX;
        float mouseY = Input.GetAxis("Mouse Y") * Time.fixedDeltaTime * sensY;

        yRotation  = mouseX;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        
        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
 

    }
}

CodePudding user response:

What I would do is to update the camera according to the pause conditon. like so:

public class CameraRotation : MonoBehaviour
{
    public isGamePaused; // changed from outside when you pause/unpause the game
    void Update()
    {
        if (isGamePaused) {
            ...
        }
    }
}

The problem with gameIsPaused (usual code convention naming to state is a bool would be isGamePaused :)) is that until you set it to true in the menu the camera will keep moving, so you may need to set the boolean to true at the time the menu pops up.

Even its not the suited case for static variables, if you want to check the pause state of your game from the camera script, you can do so like this:

public class CameraRotation : MonoBehaviour
{
    public isGamePaused; // changed from outside when you pause/unpause the game
    void Update()
    {
        if (PauseMenu.gameIsPaused) {
            ...
        }
    }
}

static stands for static in memory, so the variable value can be checked anytime from anywhere with ClassName.staticVariableName. With this I mean that as long as you set the PauseMenu.gameIsPaused variable at the times in the code where you´d like you should be able to make it work, by working I mean freeze/unfreeze the camera at the exact moment you want.

  • Related