Standalone problem with cursor

I'm using this script so my cursor will be hidden and locked - except when I click escape, then my cursor will be unlocked and visible... but when I create the game in standalone windows the escape button doesn't work. Anyone know why?


using UnityEngine; using System.Collections;

public class LockCursor : MonoBehaviour {

void Start () {
    // Start out in paused mode in web player
    if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer) {
        SetPause(true);
    } else {
        SetPause(false);
        Screen.lockCursor = true;
    }
}

void OnApplicationQuit () {
    Time.timeScale = 1;
}

void SetPause (bool pause) {
    Input.ResetInputAxes();
    DidPause(pause);

    transform.position = Vector3.zero;

    if (pause) {
        Time.timeScale = 0;
        transform.position = new Vector3 (.5f, .5f, 0);
        guiText.anchor = TextAnchor.MiddleCenter;
    } else {
        guiText.anchor = TextAnchor.UpperLeft;
        transform.position = new Vector3(0, 1, 0);
        Time.timeScale = 1;
    }
}

void DidPause (bool pause) {
    if (pause) {
        // Show the button again
        guiText.enabled = true;
        guiText.text = "Click to start playing";
    } else {
        // Disable the button
        guiText.enabled = true;
        guiText.text = "Escape to show the cursor";
    }
}

void OnMouseDown () {
    // Lock the cursor
    Screen.lockCursor = true;
}

private bool wasLocked = false;

void Update () {
    if (Input.GetMouseButton(0))
        Screen.lockCursor = true;

    // Did we lose cursor locking?
    // eg. because the user pressed escape
    // or because he switched to another application
    // or because some script set Screen.lockCursor = false;
    if (!Screen.lockCursor && wasLocked) {
        wasLocked = false;
        SetPause(true);
    // Did we gain cursor locking?
    } else if (Screen.lockCursor && !wasLocked) {
        wasLocked = true;
        SetPause(false);
    }
}

}

You don't have anything in your script that checks if you hit escape and acts on it. Use

if(Input.GetKeyDown(KeyCode.Escape)) {

    Screen.lockCursor = true;

}

This happens automatically as a savety in the webplayer as not to trap players in an application without giving them a literal and figurative way to escape. In the editor it's also done automatically because you might forget to put it in and be.. well in trouble :p..

For all other platforms you'll have to put it in yourself though.


A small script that at hides the mouse unless you press P or Escape:

To help you understand : at start you pretend you come out of a pause, so you say shouldPause is true and isPaused is false. The rest of the code is just if I should be paused but am not I'll pause, if I shouldn't be paused but I am I'll unpause, if I press Esc or P should become shouldn't and vice versa and if you should not be paused but the mouse got unlocked (for instance if someone presses the windows icon button, or right clicks in webbrowser) you pause the game. Hope I made no mistakes, it's untested.

private var shouldPause : boolean = false;
private var isPaused : boolean = true;

function Update () {

    if(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P)) {
        shouldPause = !shouldPause ;
    }

    if(!shouldPause && isPaused ) {
        Screen.lockCursor = true;
        Time.timeScale = 1;
        isPaused = true;
    }
    else if(shouldPause && !isPaused) {
        Screen.lockCursor = false;
        Time.timeScale = 0;
        isPaused = false;
    }
    else if(!Screen.lockCursor && !shouldPause) {
        shouldsPause = true;
    }
}