- Home /
Lock camera when paused
When I pause my game I want the player camera to freeze, so when I move the cursor around to chose a menu option, the inGame camera doesn't move around. Here's my pause script. I added some code to try and fix this, and it looks right to me, but it doesn't work. I'd love some help with this problem. Tnx in advance
var paused : boolean = false;
var myCheck : boolean = false;
function Start(){
paused=false;
}
function Update () {
if(Input.GetButtonDown( "Pause" ) ) {
if(!paused) {
Time.timeScale = 0;
paused = true;
Screen.lockCursor = false;
firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
mainCamera = gameObject.Find("Main Camera").GetComponent("MouseLook");
firstPersonControllerCamera.enabled = false;
mainCamera.enabled = false;
}else{
Time.timeScale = 1;
paused = false;
Screen.lockCursor = true;
firstPersonControllerCamera.enabled = true;
mainCamera.enabled = true;
}
}
}
function OnGUI() {
if( paused ) {
if(GUI.Button (Rect ( 500, 90, 100, 30), "Restart" )) {
Time.timeScale = 1.0f;
Application.LoadLevel( "stage2" );
}
if(GUI.Button (Rect ( 500, 130, 100, 30), "Options" )) {
}
if(GUI.Button (Rect ( 500, 170, 100, 30), "Quit" )) {
}
}
}
Answer by robertbu · Mar 29, 2014 at 03:46 AM
To start with put at the top of the file:
#pragma strict
When you do that you will find that your code will not compile. The first problem is that you don't declare 'firstPersonControllerCamera' and 'main' camera. The second problem is that you use the string version of GetComponent(). The string version returns an object of type 'Component', which does not have an enabled flag. You need to use the non-string version. Change lines 15 and 16 to the following:
var firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent(MouseLook);
var mainCamera = gameObject.Find("Main Camera").GetComponent(MouseLook);
I appreciate your help ver much, but i solved the problem and now i feel like a complete idiot. It was actually as simple as this: I'd previously re-named "first person controller" in my project, I'd put "AA" in front of it, to put it at the top of my project view, but when i wrote the code I'd forgotten to use "gameobject.find("aa first person controller"), and only used the default name. Hope that can be a lesson for other people. The simplest things can bring you down. a single letter in a code is all it takes to ruin the whole thing. When i changed that, my code worked, but I will try out your's to see if It'll help me solve some another problem I'm having with my pause menu
Answer by dizzymediainc · Apr 19, 2014 at 02:37 AM
With the needed additions mentioned above it indeed works just fine :)
(additions: FPS and main camera var )