- Home /
Prevent the mouse from moving out of the webplayer, but still having the cursor movable
I have a game called Galactic Neon, an Asteroids-like twin-stick shooter that uses the cursor as an aiming reticle. One annoying problem I have with the linked webplayer is that I accidentally click outside of the webplayer quite frequently. Is there a way to prevent this from happening? From what I understood, Screen.lockCursor
only locks the mouse to the center of the webplayer, which is not what I want.
try this:
Screen.showCursor = false;
it wont lock the mouse, but just hide in in the game view. if it helps please mark as correct for future reference of others:) Good luck
EDIT: sorry i read it a bit wrong. doing Screen.lockCursor = true;
should only be put in your Start function and then if you wish to change it, change it within the Update function.
this does not answer the question
that uses the cursor as an ai$$anonymous$$g reticle
so the cursor must be able to move freely.
From what I understood, Screen.lockCursor only locks the mouse to the center of the webplayer, which is not what I want.
as the OP states, this is not what they want.
Converting answer to comment.
The OP wants to have the cursor move freely within the game window, but remain confined to the game window.
In short: No, there isn't any way that I'm aware of to stop the mouse cursor leaving the bounds of the webplayer.
Browsers do not let you control the mouse position I'm afraid (as this would allow websites to mess with your mouse etc).
You could hide the cursor, but that isn't really going to solve your problem.
One thing you might be able to do is detect clicks which aren't inside of the web player so you could still shoot when outside of the game, but this probably isn't worth the effort..
Anyways, sorry but locking the cursor to only move within the bounds isn't really possible due to browser restrictions :(
Best option is to play the game fullscreen, btw cool game!
Answer by japtar10101 · Nov 01, 2014 at 07:04 PM
Thanks to @merry_christmas, I now know it's not possible. Darn.
Edit: I believe starting with Unity 5, it is now possible to confine the mouse to the window of the application.
The behavior is not consistent across platforms, but at least for Windows 10, the follow code works wonders:
Cursor.lockState = CursorLockMode.Confined;
I am not certain this works with WebGL yet.
Answer by Bunny83 · Sep 13, 2016 at 09:50 PM
The usual solution in such a case is to use a virtual mouse cursor. So you simply lock and hide the cursor (this also works in WebGL) and simply use your own graphic / image as cursor. This you can move around by reading the mouse deltas with the Input class
var delta = new Vector3(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
yourCursorObject.transform.position += delta;
Of course you have to clamp the object position manually so it stays within your desired area. You can also make it wraparound if you like. It's a virtual cursor so you can do what you want. Of course you would need to use the object position as mouse position and not Input.mousePosition. There are many ways how to display such a cursor. The usual way is probably to use Unity's GUI system but a GUITexture would work as well.
Many games use virtual mouse cursors but only for visual reasons. They also hide the hardware cursor and simply display a virtual cursor at the actual cursor position. However using a pure virtual cursor has many advantages. You can set the position of the cursor to any point inside your game, even in WebGL builds. You also can easily freeze the cursor by not applying the delta each frame.
Just have a look at the official $$anonymous$$ozilla pointer lock API. It's the standard that a browser, if it even supports locking the mouse, should offer.
Also have a look at the official pointer lock demo. It's basically exactly what a virtual mouse cursor is. Once the mouse is locked you directly move the circle around just like it is the mouse.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
JavaScript: _player.cursor(280) 1 Answer
Following the mouse Cursor does not work correctly after Flip() Function 0 Answers
Custom mouse icon 1 Answer
mouse cursor is not changing in browser 0 Answers
Can't See Mouse Cursor In Game. 1 Answer