Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cursor Lock and Hide ...

Discussion in 'Getting Started' started by boshko, Mar 15, 2015.

  1. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20
    Well im pretty new in Unity so,, this thing: http://docs.unity3d.com/ScriptReference/Cursor-visible.html
    and http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html somehow does not work for me. What i want? Simply to hide cursor (so it wont be visible but will still be in the middle of the screen) so i can put some mine cursor on ,,, let me say, UI ,,,, , In Unity 4.something i was use:

    Code (JavaScript):
    1. function Update()
    2. {
    3. Cursor.visible = false;
    4. Screen.lockCursor = true;
    5. Screen.lockCursor = false;
    6. }
    And everything worked fine till Unity 5 where i get warning (and also this old script does not work anymore)
    Assets/SCRIPTS/CenteredShowCursor.js(5,8): BCW0012: WARNING: 'UnityEngine.Screen.lockCursor' is obsolete. Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible instead..
    Is it any simply code to HIDE and Lock game cursor but still be able to use it in game ("to click on some objects inside the game")
     
  2. Creator126

    Creator126

    Joined:
    Jul 17, 2014
    Posts:
    4
    What do you mean by click on objects? Also, calling that in Update() will make your cursor rapidly lock and unlock. If you write an if statement or function to unlock and show the cursor, you could make your cursor reappear.
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
  4. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20

    This is what i get: -Assets/SCRIPTS/SetCursorState.js(7,49): BCE0051: Operator '!=' cannot be used with a left hand side of type 'UnityEngine.CursorLockMode' and a right hand side of type 'Object'.-

    ??? Have no clue what that mean!
     
  5. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20
    I mean: the cursor will still be in game, so you can interact with game objects with cursor but you,or me, will not see the Unity cursor, Instead we will see custom cursor.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,949
    The following code will hide the cursor and keep it centrally locked.
    Code (csharp):
    1. function Update () {
    2.    Cursor.visible = false;
    3.    Cursor.lockState = CursorLockMode.Locked;
    4. }
    If you simply want a custom cursor, you can use Cursor.SetCursor.
     
    Last edited: Mar 15, 2015
  7. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20
    Thank you, but i dont get anything to work with that code. Probably there need to be something else, Am I right?
     
    SarfaraazAlladin likes this.
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,949
    There is one thing I noticed when trying out the newer ways of working with the cursor. The editor appears to override hiding the mouse cursor. I don't know if it is supposed to or not, but that's what it does for me. When I build and run the project's executable though I get the desired result of a hidden cursor.

    Additionally when you mentioned a problem you encountered I decided to sit down and experiment with the example given for SetCursor. It isn't exactly the best example as it states it sets the cursor, but it doesn't state that OnMouseEnter and OnMouseExit are used to detect if a cursor is hovering over an object or not. Moving the SetCursor command to a different location (such as Start, or Update) fixes that.

    If you encounter a problem with the texture appearing corrupted, that's because hardware mode on some platforms expects a very specific cursor format. Setting it to ForceSoftware (or Force Software in the drop down box) fixes this issue. I'd actually recommend using software mode from the beginning.

    Here's the updated code I used for SetCursor.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var cursorTexture : Texture2D;
    4. var hotSpot : Vector2 = Vector2.zero;
    5.  
    6. function Start () {
    7.    Cursor.SetCursor(cursorTexture, hotSpot, CursorMode.ForceSoftware);
    8. }
    9.  
    10. function Update () {
    11.  
    12. }
     
    Last edited: Mar 15, 2015
    boshko likes this.
  9. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20
    Well this is what i got on the end, so C#,,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HideLockCursor : MonoBehaviour
    5. {
    6.     void Update ()
    7.     {
    8.          Cursor.visible = false;
    9.          Cursor.lockState = CursorLockMode.Locked;
    10.     }
    11. }
    After that "some" cursor was still visible in game (Looks like that was windows cursor) and after I added my mouse cursor in the: Edit - ProjectSettings - Player -Default Cursor (so -Select- and choose some .png file 32x32pix (Texture Type = Cursor) , Well now all works well.
     
  10. boshko

    boshko

    Joined:
    Jan 22, 2015
    Posts:
    20
    Ah now i understand where i was misunderstand ya,,, you wrote code in java script and in that grey window say its c# and i wonder about that -function- ( coz i really dont know anything about coding ), so all went wrong on my side. But all is good now, hopefully someone will have some benefit of this.
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,949
    I saw the original code snippet in JavaScript so I assumed you wanted that language specifically. Normally I use C#.
     
    boshko likes this.
  12. thedarcsage

    thedarcsage

    Joined:
    Nov 2, 2016
    Posts:
    23
    I want to confirm that adding that script to the First Person Controller with the Camera in Unity 5.4.1 worked like a charm. Thanks :)
     
  13. Bullin

    Bullin

    Joined:
    Aug 23, 2015
    Posts:
    2
    void LateUpdate()
    {
    if(isCursorIsLocked){
    Cursor.visible=false;
    }else
    {
    Cursor.visible=true;
    }
    }

    you have to run it in a LateUpdate() .

    Sorry its buggy when you run it this way.Must be a Work around tho?
     
    Last edited: Dec 18, 2016
  14. Bullin

    Bullin

    Joined:
    Aug 23, 2015
    Posts:
    2
    kk just realized that you can use it in Update() ,but you have to disable Cursor Lock in the inspector <FirstPeronController><MouseLook>.Cursor Lock .It Keeps overriding it if you don't .
     
  15. Avaneesh_Naik

    Avaneesh_Naik

    Joined:
    Nov 20, 2020
    Posts:
    1
    this is my code, this should help i think

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.     private void Start()
    7.     {
    8.         Cursor.visible = false;
    9.         Cursor.lockState = CursorLockMode.Locked;
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKey(KeyCode.Escape))
    15.             if (Cursor.visible == false)
    16.             {
    17.                 Cursor.lockState = CursorLockMode.None;
    18.                 Cursor.visible = true;
    19.             } else
    20.             {
    21.                 print("Cursor is not locked!");
    22.             }
    23.            
    24.     }
    25. }
    26.  
     
    MuratP likes this.
  16. carlogio

    carlogio

    Joined:
    Apr 10, 2021
    Posts:
    2
    The script in your final reply worked perfectly for me, thank you Avaneesh_Naik