Unity暂停菜单无法停止相机移动的技术求助
Hey there! Let's work through this pause menu issue you're facing. It's a super common gotcha when setting up your first pause system—you get time to stop, but camera movement keeps going because Time.timeScale = 0 only affects time-dependent functions (like those using Time.deltaTime or coroutines), not direct input handling. Here's how to fix it:
Step 1: Track Pause State Globally
First, update your PauseMenu.cs to expose a static boolean that other scripts can easily reference to check if the game is paused:
using UnityEngine; public class PauseMenu : MonoBehaviour { public static bool isPaused = false; // Static variable for global access [SerializeField] private GameObject pauseUI; void Update() { // Toggle pause with Escape key (adjust to your preferred input) if (Input.GetKeyDown(KeyCode.Escape)) { TogglePause(); } } public void TogglePause() { isPaused = !isPaused; pauseUI.SetActive(isPaused); if (isPaused) { Time.timeScale = 0f; // Unlock cursor to interact with pause menu Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } else { Time.timeScale = 1f; // Lock cursor back for gameplay Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } } }
Step 2: Block Input/Movement When Paused
In your PlayerMovement.cs (and any separate camera controller script you might have), add a check at the start of your Update() or LateUpdate() method to skip all input and movement logic when the game is paused. Using your Polish variable names, it might look like this:
using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float mouseSensitivity; // Your other variables (klawiatura, mysz, etc.)... void Update() { // Exit early if game is paused—don't process any input or movement if (PauseMenu.isPaused) { return; } // Your existing player movement logic float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // ... rest of your movement code // Your existing camera rotation logic float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; // ... rest of your camera rotation code } }
Why This Works
- The static
isPausedvariable lets every script handling input or movement quickly check the pause state without complex references. - By returning early in
Update(), you ensure none of the camera or player movement code runs while paused—even ifTime.timeScaleis 0. - Locking/unlocking the cursor prevents accidental camera movement from mouse clicks or drags while the pause menu is open.
Quick Troubleshooting If It Still Doesn't Work
- If your camera has its own separate script (like
CameraController.cs), add the samePauseMenu.isPausedcheck there too. - Double-check that you didn't miss adding the pause state check in any input-handling methods.
- Verify
Time.timeScaleis actually set to 0 when paused—you can check this in the Unity Editor's Inspector while the game is running.
内容的提问来源于stack exchange,提问作者mortis20

