Camera Lock/freeze on keypress
I'm making a game and I want to add a pause menu, but I need to freeze/lock the camera when I press Escape aka opening the pause menu. I'm using the first person controller from the standard assets asset and using c#
Answer by doublelift41 · Sep 20, 2020 at 08:40 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testCamera : MonoBehaviour //Camera Script
{
private testInput _testInputSc;
void Start()
{
_testInputSc = FindObjectOfType<testInput>();
}
void Update()
{
Move();
}
void Move() //Move function
{
if (!_testInputSc._gameIsPaused)
{
transform.Translate(new Vector3(1,0,0));
}
}
}
CameraScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testInput : MonoBehaviour //The input script
{
public bool _gameIsPaused = false;
void Update()
{
_gamePause();
}
private void _gamePause()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
_gameIsPaused = true;
}
else if (Input.GetKeyDown(KeyCode.Space))
{
_gameIsPaused = false;
}
}
}
Input script. You can do it with such a simple method.
Or can you use just this function.But this stopping all functions. private void _gamePause() { if (Input.GetKeyDown(KeyCode.Escape)) { Time.timeScale = 0; } else if (Input.GetKeyDown(KeyCode.Space)) { Time.timeScale =1; } } }
Your answer
Follow this Question
Related Questions
Camera Lock/freeze on keypress 0 Answers
How can I set the y position every frame update? (C#) 2 Answers
Enabling Freeze Rotation changes rotation axis 0 Answers
Is it possible to lock file in Unity ? 0 Answers
Code acts differently in new project than in old project; Cursor.lockState issue 1 Answer