Locking/unlocking cursor

is it possible to disable the mouse look script applied to a camera?

The problems is that I have several GUI buttons that I need to use, but I'm using a FPS camera with the mouse look and cursor lock scripts applied to it.

I want to be able to unlock the cursor, use the GUI buttons and then lock the cursor again. While the cursor is unlocked I want the camera to stop moving with the movement of the mouse.

Any thoughts?

Since you are using the `First Person Controller` prefab, then you don't have the `MouseLook` component only on the `Main Camera` but also on the Player (to be able to turn). So you want to disable both of them in order stop the movement.

Here is an example on how you can disable/enable `Mouse Look` components of your player. Create a new Javascript, copy/paste this code to it and attach it to the `First Person Controller`.

private var mouseLook: MouseLook[];
private var canLook: boolean = true;

function Start() {
    mouseLook = gameObject.GetComponentsInChildren(MouseLook);
    Screen.lockCursor = true;
}

function Update () {
    if (Input.GetKeyDown(KeyCode.Escape)) {
        if (canLook) {
            disableMouseLook();
        } else {
            enableMouseLook();            
        }
    }
}

function enableMouseLook() {
    canLook = true;
    Screen.lockCursor = true;
    for (var look in mouseLook) {
        look.enabled = true;
    }
}

function disableMouseLook() {
    canLook = false;
    Screen.lockCursor = false;
    for (var look in mouseLook) {
        look.enabled = false;
    }
}

The above example disables and enables the `Mouse Look` when you are hitting the `Esc` key. Feel free to ask for more details in the comments if necessary.

Hi guys,

I found your solution and it helps me, but I still encounter an issue. I first had to change the beginning part of the script to :

private var mouseLook: Array = new Array();
private var canLook: boolean = true;

function Start() {

    for(var mouseScriptFound : MouseLook in gameObject.GetComponentsInChildren(MouseLook) ) {
        mouseLook.Add( mouseScriptFound );
    }

    Screen.lockCursor = true;
}

Then everything is ok, BUT, I have the same issue than when I tried my own script. One I hit Escape, the cursor unlocks and shows off, but if I hit ESC again, even if the mouseLook scripts are activated again, the mouse cursor still shows off.

I'm using Unity 3.3. Does anyone one know what's happening? Thanks for unlightning me ;)

@Simonced Maybe you have have find, but it can be useful for others…
Try with another key than escape, for me, that works :slight_smile:

in your Player Script:

public GameObject menu;
    public static bool menuActive;
    void Update()
    {        if (Input.GetKeyDown(KeyCode.Escape))
        {
            menu.SetActive(!(Screen.lockCursor = !(menuActive = !menuActive)));
            return;
        }
}
    void Start()
    {
        Screen.lockCursor = true;
    }

In your Camera Script:

void Update()
{
    if (!Player.menuActive)
    {

//your stuff here

}
}