- Home /
Pause Menu Wont Disable Mouse,Pause menu wont disable Player Controls
Hi, im trying to make a pause menu in which the user can select Menu, Quit and Resume. When i press escape (the menu button) it brings up the menu but when i try to select any of the options, instead of clicking the buttons it goes back into the game, rehiding the mouse (goes back into controlling the Player's camera i think). Also included in my game is a Ship controller which uses the mouse also. Ive tried other suggestions but none seem to work.
Pause Menu script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour {
public static bool GameIsPaused = false;
public GameObject pauseMenUI;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
pauseMenUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause()
{
pauseMenUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void LoadMenu()
{
SceneManager.LoadScene(0);
}
public void QuitGame()
{
Application.Quit();
}
}
Player Controller Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : GravityObject {
public LayerMask walkableMask;
public float maxAcceleration;
public Transform feet;
public float walkSpeed = 8;
public float runSpeed = 20;
public float jumpForce = 20;
public float vSmoothTime = 0.1f;
public float airSmoothTime = 0.5f;
public float stickToGroundForce = 8;
public bool lockCursor;
public float mass = 70;
Rigidbody rb;
Ship spaceship;
public float mouseSensitivity = 4;
public Vector2 pitchMinMax = new Vector2 (-40, 85);
public float rotationSmoothTime = 0.1f;
public float yaw;
public float pitch;
float smoothYaw;
float smoothPitch;
float yawSmoothV;
float pitchSmoothV;
public Vector3 targetVelocity;
Vector3 cameraLocalPos;
Vector3 smoothVelocity;
Vector3 smoothVRef;
CelestialBody referenceBody;
Camera cam;
bool readyToFlyShip;
public Vector3 delta;
void Awake () {
cam = GetComponentInChildren<Camera> ();
cameraLocalPos = cam.transform.localPosition;
spaceship = FindObjectOfType<Ship> ();
InitRigidbody ();
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void InitRigidbody () {
rb = GetComponent<Rigidbody> ();
rb.interpolation = RigidbodyInterpolation.Interpolate;
rb.useGravity = false;
rb.isKinematic = false;
rb.mass = mass;
}
void Update () {
HandleMovement ();
}
void HandleMovement () {
DebugHelper.HandleEditorInput (lockCursor);
// Look input
yaw += Input.GetAxisRaw ("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxisRaw ("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp (pitch - Input.GetAxisRaw ("Mouse Y") * mouseSensitivity, pitchMinMax.x, pitchMinMax.y);
smoothPitch = Mathf.SmoothDampAngle (smoothPitch, pitch, ref pitchSmoothV, rotationSmoothTime);
float smoothYawOld = smoothYaw;
smoothYaw = Mathf.SmoothDampAngle (smoothYaw, yaw, ref yawSmoothV, rotationSmoothTime);
cam.transform.localEulerAngles = Vector3.right * smoothPitch;
transform.Rotate (Vector3.up * Mathf.DeltaAngle (smoothYawOld, smoothYaw), Space.Self);
// Movement
bool isGrounded = IsGrounded ();
Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
float currentSpeed = Input.GetKey (KeyCode.LeftShift) ? runSpeed : walkSpeed;
targetVelocity = transform.TransformDirection (input.normalized) * currentSpeed;
smoothVelocity = Vector3.SmoothDamp (smoothVelocity, targetVelocity, ref smoothVRef, (isGrounded) ? vSmoothTime : airSmoothTime);
if (isGrounded) {
if (Input.GetKeyDown (KeyCode.Space)) {
rb.AddForce (transform.up * jumpForce, ForceMode.VelocityChange);
isGrounded = false;
} else {
// Apply small downward force to prevent player from bouncing when going down slopes
rb.AddForce (-transform.up * stickToGroundForce, ForceMode.VelocityChange);
}
}
}
bool IsGrounded () {
// Sphere must not overlay terrain at origin otherwise no collision will be detected
// so rayRadius should not be larger than controller's capsule collider radius
const float rayRadius = .3f;
const float groundedRayDst = .2f;
bool grounded = false;
if (referenceBody) {
var relativeVelocity = rb.velocity - referenceBody.velocity;
// Don't cast ray down if player is jumping up from surface
if (relativeVelocity.y <= jumpForce * .5f) {
RaycastHit hit;
Vector3 offsetToFeet = (feet.position - transform.position);
Vector3 rayOrigin = rb.position + offsetToFeet + transform.up * rayRadius;
Vector3 rayDir = -transform.up;
grounded = Physics.SphereCast (rayOrigin, rayRadius, rayDir, out hit, groundedRayDst, walkableMask);
}
}
return grounded;
}
void FixedUpdate () {
CelestialBody[] bodies = NBodySimulation.Bodies;
Vector3 strongestGravitionalPull = Vector3.zero;
// Gravity
foreach (CelestialBody body in bodies) {
float sqrDst = (body.Position - rb.position).sqrMagnitude;
Vector3 forceDir = (body.Position - rb.position).normalized;
Vector3 acceleration = forceDir * Universe.gravitationalConstant * body.mass / sqrDst;
rb.AddForce (acceleration, ForceMode.Acceleration);
// Find body with strongest gravitational pull
if (acceleration.sqrMagnitude > strongestGravitionalPull.sqrMagnitude) {
strongestGravitionalPull = acceleration;
referenceBody = body;
}
}
// Rotate to align with gravity up
Vector3 gravityUp = -strongestGravitionalPull.normalized;
rb.rotation = Quaternion.FromToRotation (transform.up, gravityUp) * rb.rotation;
// Move
rb.MovePosition (rb.position + smoothVelocity * Time.fixedDeltaTime);
}
public void SetVelocity (Vector3 velocity) {
rb.velocity = velocity;
}
public void ExitFromSpaceship () {
cam.transform.parent = transform;
cam.transform.localPosition = cameraLocalPos;
smoothYaw = 0;
yaw = 0;
smoothPitch = cam.transform.localEulerAngles.x;
pitch = smoothPitch;
}
public Camera Camera {
get {
return cam;
}
}
public Rigidbody Rigidbody {
get {
return rb;
}
}
}
Ship Controller Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ship : GravityObject {
public Transform hatch;
public float hatchAngle;
public Transform camViewPoint;
public Transform pilotSeatPoint;
public LayerMask groundedMask;
[Header ("Handling")]
public float thrustStrength = 50;
public float rotSpeed = 5;
public float rollSpeed = 30;
public float rotSmoothSpeed = 10;
public bool lockCursor;
[Header ("Interact")]
public Interactable flightControls;
Rigidbody rb;
Quaternion targetRot;
Quaternion smoothedRot;
Vector3 thrusterInput;
PlayerController pilot;
bool shipIsPiloted;
int numCollisionTouches;
bool hatchOpen;
KeyCode ascendKey = KeyCode.Space;
KeyCode descendKey = KeyCode.LeftShift;
KeyCode rollCounterKey = KeyCode.Q;
KeyCode rollClockwiseKey = KeyCode.E;
KeyCode forwardKey = KeyCode.W;
KeyCode backwardKey = KeyCode.S;
KeyCode leftKey = KeyCode.A;
KeyCode rightKey = KeyCode.D;
void Awake () {
InitRigidbody ();
targetRot = transform.rotation;
smoothedRot = transform.rotation;
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update () {
if (shipIsPiloted) {
HandleMovement ();
}
// Animate hatch
float hatchTargetAngle = (hatchOpen) ? hatchAngle : 0;
hatch.localEulerAngles = Vector3.right * Mathf.LerpAngle (hatch.localEulerAngles.x, hatchTargetAngle, Time.deltaTime);
}
void HandleMovement () {
DebugHelper.HandleEditorInput (lockCursor);
// Thruster input
int thrustInputX = GetInputAxis (leftKey, rightKey);
int thrustInputY = GetInputAxis (descendKey, ascendKey);
int thrustInputZ = GetInputAxis (backwardKey, forwardKey);
thrusterInput = new Vector3 (thrustInputX, thrustInputY, thrustInputZ);
// Rotation input
float yawInput = Input.GetAxisRaw ("Mouse X") * rotSpeed;
float pitchInput = Input.GetAxisRaw ("Mouse Y") * rotSpeed;
float rollInput = GetInputAxis (rollCounterKey, rollClockwiseKey) * rollSpeed * Time.deltaTime;
// Calculate rotation
if (numCollisionTouches == 0) {
var yaw = Quaternion.AngleAxis (yawInput, transform.up);
var pitch = Quaternion.AngleAxis (-pitchInput, transform.right);
var roll = Quaternion.AngleAxis (-rollInput, transform.forward);
targetRot = yaw * pitch * roll * targetRot;
smoothedRot = Quaternion.Slerp (transform.rotation, targetRot, Time.deltaTime * rotSmoothSpeed);
} else {
targetRot = transform.rotation;
smoothedRot = transform.rotation;
}
}
void FixedUpdate () {
// Gravity
Vector3 gravity = NBodySimulation.CalculateAcceleration (rb.position);
rb.AddForce (gravity, ForceMode.Acceleration);
// Thrusters
Vector3 thrustDir = transform.TransformVector (thrusterInput);
rb.AddForce (thrustDir * thrustStrength, ForceMode.Acceleration);
if (numCollisionTouches == 0) {
rb.MoveRotation (smoothedRot);
}
}
int GetInputAxis (KeyCode negativeAxis, KeyCode positiveAxis) {
int axis = 0;
if (Input.GetKey (positiveAxis)) {
axis++;
}
if (Input.GetKey (negativeAxis)) {
axis--;
}
return axis;
}
void InitRigidbody () {
rb = GetComponent<Rigidbody> ();
rb.interpolation = RigidbodyInterpolation.Interpolate;
rb.useGravity = false;
rb.isKinematic = false;
rb.centerOfMass = Vector3.zero;
rb.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
}
public void ToggleHatch () {
hatchOpen = !hatchOpen;
}
public void TogglePiloting () {
if (shipIsPiloted) {
StopPilotingShip ();
} else {
PilotShip ();
}
}
public void PilotShip () {
pilot = FindObjectOfType<PlayerController> ();
shipIsPiloted = true;
pilot.Camera.transform.parent = camViewPoint;
pilot.Camera.transform.localPosition = Vector3.zero;
pilot.Camera.transform.localRotation = Quaternion.identity;
pilot.gameObject.SetActive (false);
hatchOpen = false;
}
void StopPilotingShip () {
shipIsPiloted = false;
pilot.transform.position = pilotSeatPoint.position;
pilot.transform.rotation = pilotSeatPoint.rotation;
pilot.Rigidbody.velocity = rb.velocity;
pilot.gameObject.SetActive (true);
pilot.ExitFromSpaceship ();
}
void OnCollisionEnter (Collision other) {
if (groundedMask == (groundedMask | (1 << other.gameObject.layer))) {
numCollisionTouches++;
}
}
void OnCollisionExit (Collision other) {
if (groundedMask == (groundedMask | (1 << other.gameObject.layer))) {
numCollisionTouches--;
}
}
public void SetVelocity (Vector3 velocity) {
rb.velocity = velocity;
}
public bool ShowHUD {
get {
return shipIsPiloted;
}
}
public bool HatchOpen {
get {
return hatchOpen;
}
}
public bool IsPiloted {
get {
return shipIsPiloted;
}
}
public Rigidbody Rigidbody {
get {
return rb;
}
}
}
Your answer
Follow this Question
Related Questions
Controlling MouseLook script with keys/joystick 1 Answer
input system InvalidOperationException when unplugging the controller 0 Answers
New input system controller stops detecting on fully pressed trigger? 0 Answers
Using a PS Vita as a controller? 1 Answer
How do I have multiple controls for the same function? (Controller and Keyboard) 1 Answer