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

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

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:

void Start() {
  Cursor.lockState = CursorLockMode.Locked;
}

What platform are you talking about? What lock mode are you setting?

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

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?

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.

Bruh, well now I know at least lol, Thank you

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.