Unity 5.1.1 - Mouse issue

Greetings,

I created a FPS shooter game for a school assignment. I have one issue I can’t really seem to fix. I have this issue for both Mac and Windows. Basically I can’t seem to find a way to clamp the mouse to the window. It keeps leaving the window especially when your shooting the gun. This really kind of sucks now that we’ve added health to the mobs and we are no longer just destroying the prefabs. I’ve tried the following while it will hide the mouse until it leaves the screen. It doesn’t seem to help much with keeping the mouse in the window.

Cursor.visible = false;
Cursor.lockState = CursorLockMode.Confined;

you need to set it every frame. (at least that’s what I’ve found) Try putting it in update.

Thanks, I will try that now.

That worked for me. Only every great once in a while the mouse makes it outside the window. I can live with that. Thank you.

The mouse should never make it outside the window once it has focus unless you hit escape. I do the following in my code as well:
if (Input.GetMouseButtonDown(1))
{
Cursor.lockState = CursorLockMode.Locked;
}