How to pause camera movement?
I have two scripts currently, my FPS_Player Script and my Pause_Menu Script, currently I'm trying to make it so that when the player hits the pause button (Escape) it will enable the mouse cursor and restrict it to the game screen whilst pausing the camera but I can't seem to call the function properly and have tried multiple methods of doing so. Can someone please help? Preferably in an example, I struggle with standard code lingo since I'm still learning :)
FPS_Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class FPS_Player : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
public float Health = 100f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
public bool Pause = false;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
Pause_Menu:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pause_Menu : MonoBehaviour
{
[HideInInspector]
public bool Pause = false;
// Start is called before the first frame update
void Start()
{
if (Input.GetButton("Pause"))
{
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = true;
Pause = true;
}
}
// Update is called once per frame
void Update()
{
if (Pause == true)
{
GetComponent<MouseLook>().enabled = false;
}
else (Pause == false);
{
GetComponent<MouseLook>().enabled = true;
}
}
}
Your answer
Follow this Question
Related Questions
Simple Camera Switch Using C# 3 Answers
How to get photo image and work with it 0 Answers
C# Script for 3rd Person Camera? 1 Answer
Using Camera.main.ViewportToWorldPoint to limit player movement in the Y direction 0 Answers
Received an error: RenderTexture's anti-aliasing changed from 2 to 1 due hardware limitations. 0 Answers