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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

(solved) Mouse Cursor Lock and Unlock issues Especially unlock.

Discussion in 'Scripting' started by SomeVVhIteGuy, Apr 2, 2018.

  1. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    Hi guys

    today I'm trying to make my in game menus actually usable and am having difficulty.

    The goal is to have (C) open up a canvas, make the cursor visible and unlock it. Ideally this would be done without stopping other inputs ( Like being able to still run forward with that canvas open, which it already does but I felt I should point out I don't want to lose that functionality).

    Additionally, I need to know how to make it so if I click within that canvas, it wont re-lock and hide the cursor (which it does now)

    Right now, my entire script is as follows, Being used solely by the Canvas in the inspector (should I maybe try putting the cursor related code in the Character Controller instead?)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. public class CharacterInfoScript : MonoBehaviour
    8. {
    9.  
    10.  
    11.  
    12.     public Canvas canvas2;
    13.     private bool menuOpen;
    14.  
    15.     void Start()
    16.     {
    17.         Cursor.visible = (false);
    18.         canvas2.GetComponent<Canvas>().enabled = false;
    19.         menuOpen = false;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.C))
    26.         {
    27.             if (menuOpen == false)
    28.             {
    29.                 canvas2.GetComponent<Canvas>().enabled = true;
    30.                 menuOpen = true;
    31.                 Cursor.visible = (true);
    32.                 Cursor.lockState = CursorLockMode.None;
    33.             }
    34.             else
    35.             {
    36.                 canvas2.GetComponent<Canvas>().enabled = false;
    37.                 menuOpen = false;
    38.  
    39.             }
    40.  
    41.  
    42.         }
    43.     }
    44.  
    45.  
    46. }
    Right now, the canvas does open/close correctly, and is intractable (if I use Escape to unlock the cursor) but C doesn't unlock anything.


    I've tried a couple different solutions I found online but nothing has worked, please tell me If you see any redundant code I forgot to delete.

    thanks for any help, you guys surprise me with extreme helpfulness every time.
     
  2. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    I left out the call to re-lock the cursor for now, I figure I will add that once I actually get it to unlock.

    Also, I test everything through unity's play button. Not sure if the cursor lock is just always overridden in the tester or something like that.
     
  3. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Make menuOpen public so you can see what’s its doing. Make it private once it works. Also a throw a debug log into there to check what events and methods are being called.
     
    SomeVVhIteGuy likes this.
  4. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Cursor management works in the editor as in the build.
     
    SomeVVhIteGuy likes this.
  5. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    made public for now
    okay i added
    Debug.Log("Menu Called to open: Cursor state is" + Cursor.lockState.ToString()); to the if (menuopen ==false)

    tested and returned
    Menu Called to open:Cursor state is None
    UnityEngine.Debug:Log(Object)

    so its saying its not locked but it is.

    is it possible the character controller or the editor is locking the the cursor as part of an update()? aka the code is working but then canceled out the next frame?
     
  6. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    so is that to say the editor itself isnt stopping the code from working?
     
  7. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    The editor should behave the same as the build. So if it isn’t working in the editor, it probably won’t work in the build.
     
    SomeVVhIteGuy likes this.
  8. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    thankyou for that. well it's not working in the editor.
     
  9. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    I’m not sure if it’s being locked elsewhere. Maybe do a search across all your scripts for lockstate.
     
    SomeVVhIteGuy likes this.
  10. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    SOLVED: the code was working, thankyou for suggesting the simple debug check.

    Under the FPS Controller inspector, under the standard script<mouse look the was a check to always mouse lock. just had to unclick it. sorry im such a scrub
     
    brickshot and NicBischoff like this.
  11. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Great. No problem. Go wild with debugging states because you can always take them out when you don’t need them any more. You could also step through the code but I like to watch the variables with public and log them until the code is working properly.
     
    SomeVVhIteGuy likes this.
  12. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    good advice thankyou again

    everything is working great and now I can start initializing my menus.