Search Unity

Inability to constrain mouse to game while testing it is really abnoxious.

Discussion in 'Editor & General Support' started by SilverDaller, Apr 29, 2021.

  1. SilverDaller

    SilverDaller

    Joined:
    Apr 25, 2021
    Posts:
    5
    I have tried constraining the cursor using Cursor.lockState, but it just does not work. I put that into a script in the start method, and attached the script to an empty in my scene. Before that, I had it attached to the player movement script. Regardless of where I put it, IT DOES NOT WORK. Please let me know if i'm doing something wrong, because after closing my game for the 10th time while playing because the mouse keeps moving outside the game window, Its getting really annoying.

    Details:
    HDRP project in Unity 2021.1.4f1 Personal
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Well you haven't really shared what you've actually tried code-wise so it's hard to say. This is more or less what your code should look like:
    Code (CSharp):
    1. void Start() {
    2.   Cursor.lockState = CursorLockMode.Locked;
    3. }
     
    Last edited: Apr 29, 2021
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    PraetorBlue likes this.
  4. SilverDaller

    SilverDaller

    Joined:
    Apr 25, 2021
    Posts:
    5
    Sorry I didn't post a snippet, but its pretty much what you provided. Here's my code. I have this attached to an empty in my scene. I also tried having Confined mode on instead of locked, but same issue persists. The target platform is just PC
    upload_2021-4-29_11-29-46.png

    EDIT: I don't know how to send a video here, but I recorded it to show you. Basically when I press the play button, the mouse isn't automatically locked. So while I move my fps camera around, my mouse can land on the X button for unity causing me to close my project while testing. I also accidentally opened eclipse in the video while also playing lol. I noticed something new though, when I manage to click back in the game window after the mouse clicks out of the editor, that's when it actually gets locked to my game. Do you have an idea of why that's happening?
     

    Attached Files:

    Last edited: Apr 29, 2021
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Oh you're talking about just in the Editor. I believe this behavior is as designed. You'll also notice you can unlock the mouse by hitting the ESC key as well. Just click on the Game View window after you hit the play button if you want the mouse locked.
     
  6. SilverDaller

    SilverDaller

    Joined:
    Apr 25, 2021
    Posts:
    5
    Bruh, well now I know at least lol, Thank you
     
  7. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,119
    I have a few assets in my project that lock the mouse as you have requested in the game window, I don't know how they do it but this is possible to have what you asking.

    Once you have hit play and then you click once on the game window you will not interact with the unity interface anymore.

    Then if you hit escape, the editor keep the game playing but you can interact with the interface, change option, and other, then you click in the game window and the mouse is locked to the game area.

    so to end the gameplay you need to hit the escape twice, or hit escape once to get back to interface and click the play button at the top.