当游戏在Unity暂停时,如何冻结相机



所以最近我开始编写我的第一款FPS游戏。我的暂停菜单出现问题。问题是,当我暂停游戏时,我的鼠标仍然控制着相机,当我想按下菜单中的一些按钮时,相机一直跟着我的鼠标。我在网上搜索这个问题的解决方案,但我没有找到解决方案(甚至我的代码与我找到的一些代码相似(。

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();
}
}

我要做的是根据暂停条件更新相机。像这样:

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

gameIsPaused的问题(通常的代码约定命名为bool将是isGamePaused:(是,直到您在菜单中将其设置为true,相机才会继续移动,因此您可能需要在菜单弹出时将布尔值设置为true。

即使它不适合静态变量,如果你想从相机脚本中检查游戏的暂停状态,你可以这样做:

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

static代表内存中的静态,因此可以使用ClassName.staticVariableName随时随地检查变量值。我的意思是,只要你在代码中你想要的时间设置PauseMenu.gameIsPaused变量,你就应该能够使它工作,我所说的工作是指在你想要的确切时刻冻结/解冻相机。

您的静态PauseMenu.IsPaused很好,也是一种很好的方法,尽管我通常会在GameManager类上放这样的东西。你可以在网上找到一个Unity Singleton模式,这样你就可以开始使用PauseMenu.Instance.XXX,这样你可以很容易地访问任何你需要的东西。也就是说,你需要找到控制你的相机的脚本,并检查PauseMenu.IsPaused。很可能你的相机上有一个脚本,你通常需要做的就是找到Update((函数,然后执行:if(PauseMenu.IsPaused(return;阻止它工作。

最新更新