- Home /
Get mouse's pixel position on the monitor, independent of the game window's position or size.
I need to to simply know the mouse's position, where 0,0 is at the bottom left corner of the whole monitor, and not the bottom left corner of the game window.
I could calculate the position of the mouse by checking the Input.MousePosition, plus some calculations that factor in the screen resolution and the game window's x/y position on the screen...
However that is particularly tricky in my case, because I'm also moving my whole game window based on the cursor's movement. This creates a whip-lash effect since the game window is moving based on the cursor's position, while simultaniously I'm trying to calculate the cursor's position based on the game window's position! You see the problem.
I've actually managed to fix the whip lash by predicting the whiplash and compensating for it, but I'd still like to know why the bloody heck it isn't possible to simply get the mouse's X and Y position on the whole monitor? Or is it possible though a windows DLL call or something?
Answer by Vollmondum · Mar 21, 2019 at 07:42 AM
Game window is simply a preview of what you get in actual game. Whole monitor = game window when the game is built and running.
Umm... Whole monitor is not the same as game window. The game can run in a window, which isn't necessarily the size of the whole monitor.
Yeah, that's called outsmarting :) I answered whatever I suppose he needed. Well, at least people with such questions don't go that deep into coding as windowed games.
Answer by Casiell · Mar 22, 2019 at 08:11 PM
Input gives you mouse position in screen coordinates. Unity Camera has two methods that will do what you want ScreenToWorldPoint and ScreenToViewportPoint
You are wrong. None of these things do anything remotely close to giving the position of the mouse relative to the corner of the whole monitor, where the position isn't affected by the position of the game window.
Answer by $$anonymous$$ · Mar 25, 2019 at 10:58 PM
I discovered that GetCursorPos (which is a user32.dll call) will actually give correct mouse position... but only if you (a) have windows mouse acceleration turned off (which requires you to edit the registry), and (b) have window's text and application scaling set to exactly 100% in control panel, and (c) have windows pointer speed set to exactly 50% in the window's mouse settings, and (d) have the game window set to scaling of exactly 1.0x (if you are running it in the unity editor mode).... so yea.. it works but sure has a lot of conditions. I'd still like to just have the mouse position without all those conditions, but I guess if it was easy then Unity would have already been giving it to us through Input. I just wish I knew how AutoHotKey gets the mouse position so accurately and easily, there must be a way, Unity dev's must just not know how to do it I guess.
Your answer