Hello,
I am a 3D artist with very basic coding skills. I have a FPS character controller, and would like to pause the controller and camera while the TAB key is pressed so they are frozen while I navigate to menus.
Thank you for your help,
Mike
Hello,
I am a 3D artist with very basic coding skills. I have a FPS character controller, and would like to pause the controller and camera while the TAB key is pressed so they are frozen while I navigate to menus.
Thank you for your help,
Mike
hey Mike. Are you using unity input system package or legacy input?
For legacy input you could do something like below
if (Input.GetKeyUp(KeyCode.Tab))
{
GetComponen<FPSController>().enabled = false;
}
where FPSController if obviously name of the actual controller.
Thank you IndieForger!
I will try the code out this morning. I don’t want the character controller to disappear, just freeze translations and rotations while the key is pressed. I’m not sure which system I’m using, I will post the code below.
using UnityEngine;
public class FPSCharacter : MonoBehaviour {
[Header("Camera")]
public Transform cam;
public bool lockCursor;
[Range(0.1f, 10)] public float lookSensitivity;
public float maxUpRotation;
public float maxDownRotation;
private float xRotation = 0;
[Header("Movement")]
public CharacterController controller;
// Speed of forwards and backwards movement
[Range(0.5f, 20)] public float walkSpeed;
// Speed of sideways (left and right) movement
[Range(0.5f, 15)] public float strafeSpeed;
public KeyCode sprintKey;
// How many times faster movement along the X and Z axes
// is when sprinting
[Range(1, 3)] public float sprintFactor;
[Range(0.5f, 10)] public float jumpHeight;
public int maxJumps;
private Vector3 velocity = Vector3.zero;
private int jumpsSinceLastLand = 0;
void Start() {
if(lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update() {
transform.Rotate(0, Input.GetAxis("Mouse X") * lookSensitivity, 0);
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -maxUpRotation, maxDownRotation);
cam.localRotation = Quaternion.Euler(xRotation, 0, 0);
velocity.z = Input.GetAxis("Vertical") * walkSpeed;
velocity.x = Input.GetAxis("Horizontal") * strafeSpeed;
velocity = transform.TransformDirection(velocity);
if(Input.GetKey(sprintKey)) { Sprint(); }
// Apply manual gravity
velocity.y += Physics.gravity.y * Time.deltaTime;
if(controller.isGrounded && velocity.y < 0) { Land(); }
if(Input.GetButtonDown("Jump")) {
if(controller.isGrounded) {
Jump();
} else if(jumpsSinceLastLand < maxJumps) {
Jump();
}
}
controller.Move(velocity * Time.deltaTime);
}
private void Sprint() {
velocity.z *= sprintFactor;
velocity.x *= sprintFactor;
}
private void Jump() {
velocity.y = Mathf.Sqrt(jumpHeight * -2 * Physics.gravity.y);
jumpsSinceLastLand++;
}
private void Land() {
velocity.y = 0;
jumpsSinceLastLand = 0;
}
}
If you want to pause the game when the key is held down, you should be able to do this (put in update method):
if (Input.GetKey(KeyCode.Tab))
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
If you want to pause and unpause the game by pressing tab, do this:
bool paused = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
if (paused)
{
Time.timeScale = 1;
paused = false;
}
else
{
Time.timeScale = 0;
paused = true;
}
}
}
Thank you leskela,
The first bit of code works perfectly with the Character Controller, but the camera still moves. What I’d like to do is freeze both the controller and camera while TAB is down so the mouse can navigate to the menus.