- Home /
Tracking the mouse off-window
I would like to know whether or not it's possible to have the Standalone be able to continue tracking the mouse even after it leaves the window, because I'm having issues where the application I'm building is still noticing Mouse Scrollwheel
axis movement and acting accordingly.
The Editor seems to take care of this - it continues tracking the mouse position even if the Editor doesn't have Windows's focus. Is there any way I can put this on the finished build as well?
Answer by The Stick · Jun 26, 2015 at 03:57 PM
I managed to come up with a workaround until the people at Unity Technologies let us get the mouse position. A MouseBlocker GameObject with a few simple components:
An
Image
with no sprite, and a transparent tint (akaalpha = 0
)A
CanvasGroup
A
MonoBehaviour
that goes like:public Camera referenceCamera; private CanvasGroup group; public Vector2 lastMousePos; public Vector2 mouseDelta; public static bool BlockMouseInput = false; void Update() { Vector2 currMousePos = Input.mousePosition; Vector3 newpos; RectTransformUtility.ScreenPointToWorldPointInRectangle(((RectTransform)this.transform), currMousePos, referenceCamera, out newpos); transform.position = newpos; mouseDelta += new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); if((Vector3)lastMousePos != Input.mousePosition) { mouseDelta = Vector2.zero; lastMousePos = Input.mousePosition; } if(group == null) group = GetComponent<CanvasGroup>(); group.blocksRaycasts = BlockMouseInput = (mouseDelta.sqrMagnitude > 0.15f); }
It takes care if pretty much everything under the uGUI with the CanvasGroup
blocking the Raycasting, but anything referencing Input.GetAxis
on any of the mouse axes should check BlockMouseInput
first.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get mouse position outside of the main window 0 Answers
Mouse plane does not detect height 1 Answer
Mouse position OTG Android 1 Answer
How to replace the mouse with a 3D model? 1 Answer
Mouse follow on network 0 Answers