Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Lock / Unlock Mouse Cursor when using the FPSController standard asset

Discussion in 'Editor & General Support' started by rob241abc, Nov 23, 2016.

  1. rob241abc

    rob241abc

    Joined:
    Dec 14, 2015
    Posts:
    19
    Hi,

    I'm currently developing a game where an inventory system is required. I have fully implemented the inventory system itself, however when the inventory is closed (by pressing the escape key), the mouse cursor is then visible and not locked. I am aware that the MouseLook script unlocks the cursor when the escape key is pressed and locks it when the player left-clicks; I have tried commenting out and deleting this part of the MouseLook script, however the cursor is still unlocked when the escape key is pressed.

    Is there a way to disable this part of the MouseLook script completely? Or is there a way to work around this to make sure that the cursor is locked to the center of the screen (not just hidden) even if the escape key is pressed.

    Thanks in advance!
     
  2. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    12
    Hey,

    Use this when opening and closing inventory.

    Show & Unlock cursor:

    Code (CSharp):
    1. Cursor.visible = true;
    2. Cursor.lockState = CursorLockMode.None;
    Hide & Lock cursor:

    Code (CSharp):
    1. Cursor.visible = false;
    2. Cursor.lockState = CursorLockMode.Locked;
     
  3. rob241abc

    rob241abc

    Joined:
    Dec 14, 2015
    Posts:
    19
    This is what I am using at the moment but the MouseLook script changes the cursor lock state to none when I press escape. Is there not a way to go around this?