Search Unity

Cursor Lock (but only for in-game scenes)

Discussion in 'Community Learning & Teaching' started by mojokage, Sep 1, 2014.

  1. mojokage

    mojokage

    Joined:
    Feb 15, 2014
    Posts:
    8
    Hi guys, so I want the cursor to be locked to the center of the screen where the crosshair is. If the player gets killed it will take them to a replay/exit scene, the problem is that when they're taken to this scene (which the script isn't in, so it should no longer be active) the cursor is still locked, this means that the player can't make the choice weather to Replay or Exit and they're stuck, any suggestions?

    Cursor Lock Code:

    // lock at beggining
    function Start ()
    {
    Screen.lockCursor = true;
    }
    //unlock in mid game

    function Update()
    {
    Screen.showCursor = false;

    if (Input.GetKeyDown(KeyCode.P))
    if (Screen.lockCursor)
    Screen.lockCursor = false;

    else{
    Screen.lockCursor = true;
    }
    }
     
  2. OutSpoken_Gaming

    OutSpoken_Gaming

    Joined:
    Oct 14, 2013
    Posts:
    90
    You could try putting Screen.lockCursor = false; in the start method of the scene you have switched to. Or unlock the cursor before switching scenes.