[Not Solvable]Lock Mouse in area?

Hello community,

I want to allow the mouse cursor to just move in a specific area on the screen.
Of course I could check “if mouse x/y is bigger then allowed, reset position”. But this doesnt sounds like having a good performance.

Is there a better way doing this?


Unity doesn’t provide this as a built-in feature. Unity only provides a way to lock the cursor at the screen center. Unity doesn’t provide any function to set the cursor to a specific position. However there are multiple possibilities. I guess we talk about one of those platforms: Windows, Mac, Linux or Webbuild since those actually have a mouse, however not all solutions are possible on all those platforms. It also depends on if you have Unity pro or not.

In a standalone build (PC / Mac) you can restrict the hardware mouse cursor with the operating system API as long as you have Unity pro which is required for using native-code plugins. For windows you could use ClipCursor. I don’t develop for other platforms, but i’m sure they have a similar function.

In a Webbuild you can’t use native code plugins so there’s no way to influence the hardware mouse cursor except which the functions Unity provides.

That’s why the best solution is to simple hide and lock the hardware cursor in Unity and display a software-cursor. This can be moved with Input.GetAxis or Event.delta inside OnGUI. This way you can clamp the cursors position as you wish. However, keep in mind that the Unity GUI won’t directly work with the software cursor. So all interactions with this cursor has to be processed by yourself.

Just hiding the the hardware cursor is the worst approach because when the hardware cursor leaves the allowed rect the user don’t know where the mouse cursor is. The User also accidentally can leave the whole application window and might unfocus the application. Hiding the cursor makes only sense with a non restricted software cursor.

An edited compilation of comments made on the question:

This is a good question because, whilst the performance of a few simple ‘if’ statements is too negligible to be concerned with (I can’t think of a simpler and more efficient way to do this and it really won’t affect performance), what IS wrong with this approach is that because the cursor is hardware driven and not software driven, creating software constraints should result in cursor jittering if the user attempts to push the cursor outside the confining area.

All possible methods that are implemented and performed as part of your unity program will still suffer from the fact that they are software driven and thus don’t get updated as frequently as the hardware cursor. You could avoid the jittering look by hiding the hardware cursor and drawing a custom cursor at the hardware cursor’s position each frame; effectively making the cursor software driven which will prevent it from moving inbetween our constraining repositionings. The obvious downside is that a software cursor wont be updated as frequently as a hardware cursor and thus will appear to lag behind where a moving cursor should be, and if the frame rate of the program drops, so too will the cursor movements (it will appear to ‘skip’).

I’m sorry to say that I think you’ll have to find a work around of some sort, perhaps throw up exactly what you’re trying to do in a new question and try and find a suitable solution to your particular circumstance (that doesn’t involve constraining the cursor, unless you’re okay with a software cursor or jitter)?

Besides, people hate having their cursor restrained :stuck_out_tongue: