- Home /
How do I limit cursor movement?
On a first person controller, how do limit the movement of my cursor to the middle two thirds of my screen. This is so that the cursor can be more useful in selecting game objects quickly. The first person will continue to rotate but the mouse will not pass the 2/3 screen position.
I don't want to lock the cursor completely or hide it. I just want to restrain it to the middle 2/3 of my screen.
Sorry I have no sample script to start with. I couldn't find a starting point for this one.
Thanks for any help.
Answer by Owen-Reynolds · Apr 15, 2013 at 01:40 PM
Turn off the real cursor. Create a GUITexture of crosshairs or something that looks like a cursor. Use mouse motion to place and move that on the screen, but kept it in range. Ex (not real code, do not use):
float cursorX=0.5f; // middle of screen. Using 0-1
float xMv = (Input.mousePosition.x - oldMouse.x)/Screen.width;
// NOTE: translating 10 pixel mouse move into 0.03% screen move
cursorX += xMv;
if(cursorX<0.33f) cursorX=0.33f; // middle 2/3s
....
crosshairs.position = new Vector3(cursorX, cursorY, 0);
What is "old$$anonymous$$ouse" ? Also will the new cursor retain its ability to click on things (eg "On$$anonymous$$ouseDown")? How does the GUITexture get the cursor properties (eg click)?
On$$anonymous$$ouse's are only for the "real" mouse. Would use Input.
to check for clicks. Then screen ray casts to check what you were over. Even with a real mouse, you need a screenCast to, say, target a spot on the ground.
old$$anonymous$$ouse is just program$$anonymous$$g. If you can't directly check mouse movement, store the last mouse position yourself and compute the movement.
If this seems tricky, consider not doing it. $$anonymous$$ost people are used to the mouse going anywhere. A restricted mouse might freak people out.