Hi, I’m pretty new to unity (6 months or so). I’m making a menu screen but I can still move and look when I’m paused. I want the player to stay still when paused. Please help
Here is my movement script -
using UnityEngine;
using Photon.Pun;
public class PlayerMovement : MonoBehaviourPunCallbacks
{
public CharacterController controller;
public float speed;
void Update()
{
if (photonView.IsMine)
{
Move();
}
}
void Move()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
}
here is my look script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class MouseLook : MonoBehaviourPunCallbacks
{
public float mouseSensitivity;
public Transform playerBody;
float xRotation;
public bool isPaused;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
if (!photonView.IsMine)
{
GetComponent<Camera>().enabled = false;
GetComponent<AudioListener>().enabled = false;
}
}
private void Update()
{
if (photonView.IsMine && !isPaused)
{
Look();
}
}
void Look()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -80, 80);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerBody.Rotate(Vector3.up * mouseX);
}
}
and here is my pause script -
sing UnityEngine;
using Photon.Pun;
public class PauseMenu : MonoBehaviourPunCallbacks
{
public int menuSceneIndex;
public GameObject pauseMenu;
private bool isPaused = false;
private void Update()
{
Pause();
}
void Pause()
{
if (Input.GetKeyDown(KeyCode.Escape) && !isPaused)
{
isPaused = true;
}
else if (Input.GetKeyDown(KeyCode.Escape) && isPaused)
{
isPaused = false;
}
if (isPaused)
{
pauseMenu.SetActive(true);
Cursor.lockState = CursorLockMode.None;
}
else
{
pauseMenu.SetActive(false);
Cursor.lockState = CursorLockMode.Locked;
}
}
public void OnResumeButtonClicked()
{
pauseMenu.SetActive(false);
isPaused = false;
}
public void OnSettingsButtonClicked()
{
}
public void OnQuitButtonClicked()
{
PhotonNetwork.LeaveRoom();
PhotonNetwork.LoadLevel(menuSceneIndex);
}
}
I know my code isn’t best but I did the pause script myself and I’m just trying to get better.