- Home /
How do I lock the mouse in an area?
The problem I have right now is that I can lock my mouse texture to an rect using Mathf.clamp, but now the actual mouse.
Is there anyway to clip the actual mouse/cursor in a similar fashion as my mouse texture?
Comment
Hiding it doesn't do what I want as the mouse can still move out of the area.
Answer by chemicalvamp · Nov 15, 2011 at 09:33 AM
The mouse position is not at all related to unity, Thats the job of user32.dll. I suppose you could read up on it, but its going to be whole lot more complicated than:
Vector2 mousePos;
float leftBound;
float rightBound;
float bottomBound;
float topBound;
mousePos.x = Mathf.Clamp(Input.mousePosition.x, leftBound, rightBound);
mousePos.y = Mathf.Clamp(Input.mousePosition.y, bottomBound, topBound);
Input.mousePosition = mousePos; // too bad its read only.
Your answer
Follow this Question
Related Questions
Cant unlock cursor with FPS prefab 2 Answers
Cursor lock not working after alt tab 2 Answers
Lock mouse position with a Rect, possible? 1 Answer
Locking the Cursor at the center. 4 Answers
How do I do this? 2 Answers