The question is answered, right answer was accepted
C# Disabling Camera moving while in Pause Menu
I have searched through other questions people have asked but it hasn't helped. Basically when the player pauses the game I don't want the camera in game moving around when the player is selecting options in the pause menu.
For my FPS Controller I am using the standard assets. Below is my Pause script: ( I have it on an empty object called ButtonManager)
public Transform canvas;
public Transform Player;
//Pause menu
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Pause();
}
}
public void Pause()
{
if (canvas.gameObject.activeInHierarchy == false)
{
//so player can have mouse
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
canvas.gameObject.SetActive(true);
Time.timeScale = 0;
Player.GetComponent<CharacterController>().enabled = false;
AudioListener.volume = 0;
}
else
{
canvas.gameObject.SetActive(false);
Time.timeScale = 1;
Player.GetComponent<CharacterController>().enabled = true;
AudioListener.volume = 1;
}
}
//End Pause Menu
//function for New Game button that loads next scene
//make sure to have next scene in build settings
public void NewGameBtn(string newGameLevel)
{
SceneManager.LoadScene(newGameLevel);
}
//function to quit the game once the user presses exit game button public void ExitGameBtn() {
Application.Quit();
}
}
I have tried adding GetComponent().enabled = false; in the if statement and then make it true in else statement but it throws an error for enabled. Says something along the lines of "mouselook" doesnt have definition of enabled.
Thanks in advance!
Answer by UnityCoach · Feb 17, 2017 at 04:39 PM
I would actually recommend using Time.deltaTime as a multiplier in your Camera moving component. 1st, it'll make it frame rate independent, 2nd, it'll make it stop moving when you set the timescale to 0.
When using Time.deltaTime I am getting an error that says "The time in seconds it took to complete the last frame (Read Only). Property or indexer "Time.deltaTime"cannot be assigned to --read only"
You cannot assign Time.deltaTime, you simply use it when you move things around. Like :
void Update ()
{
transform.Translate (speedX, speedY, 0); // framerate dependant and will never pause
transform.Translate (speedX*Time.deltaTime, speedY*Time.deltaTime, 0); // framerate independant and will pause when Time.timeScale is set to 0
}
I apologize I'm still learning how to code. Is that something I would implement in my Pause method or update in my if statement?
Thanks for your help! I was able to fix it another way by adding GetComponent().enabled = false; ins$$anonymous$$d of using CharacterController but now I know in the future about Time.deltaTime thank you!
Follow this Question
Related Questions
Unity's FPC apply camera deadzone for mouse 0 Answers
Settings menu of main menu not updating in settings menu of pause menu. Unity C#, 0 Answers
Moving Camera without Changing Object Rotation 1 Answer
When I open my Ingame Menu My mouse appears Then when I click it disappears 0 Answers
Unity Crashs When Time.timescale = 0 0 Answers