- Home /
Setting the mouse position to specific coordinates
Hi,
I am making a game that involves the OnMouseOver() function, and I would like the game to start with the player's cursor already positioned at specific coordinates, but with the player will still being able to move his cursor after that. Is this possible? I know how to lock the cursor in the middle of the screen, but how could I manually set its position at the beginning of the game?
Thanks in advance. :)
You can use the GUI to create a mouse cursor, similar to creating a crosshair and manipulate that image.
You cannot however move the cursor itself unless you include windows.form.dll.
Basically the cursor is a windows operating system object that Unity reads information from. It doesn't write to it though, its not a unity object. You'd need to basically include microsoft's code for the mouse to move the mouse, hence windows.form.dll
Answer by skalev · Oct 10, 2012 at 09:14 PM
You can't. This is controlled by the operating system. (as far as I know). How do you lock the cursor in the middle of the screen? if you set it somehow, just set it to the right coordinates.
What you can do though, is HIDE the cursor and then write your own class that shows a different cursor (using an Image you created) and moves along with the mouse. However, this isn't a great solution, since you will always have an offset between your mouse and the real mouse, which means yo cannot use any of the built in On$$anonymous$$ouse functions, and will have to implement those yourself. Probably not worth the trouble...
Answer by zachwuzhere · Mar 03, 2018 at 09:47 PM
Anyone who still needs to move the move position, here is one method...
//C#
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
int xPos = 30, yPos = 1000;
SetCursorPos(xPos,yPos);//Call this when you want to set the mouse position
don't know why this doesn't have more upvotes since it seems to solves the problem perfectly.. does this work accross diferent platforms somehow?
It doesn't. user32.dll is a windows library, so this fix is windows-only.
Other OSes probably have similar system calls to do this sort of thing though.
Now if only we can get this to work for mac, linux (and webGL)
I saw someone else on another post using Hardware.mouseposition so maybe that would work on other platforms.
Answer by elmckdaddy · Dec 05, 2020 at 07:17 AM
I know this is super old, but I found this question tonight, so maybe someone else will land here as well. If you're looking for how to set the cursor position using the new input system, check out this answer for a cross platform solution.
Answer by sparkzbarca · Oct 10, 2012 at 09:34 PM
You can use the GUI to create a mouse cursor, similar to creating a crosshair and manipulate that image.
You cannot however move the cursor itself unless you include windows.form.dll.
Basically the cursor is a windows operating system object that Unity reads information from. It doesn't write to it though, its not a unity object. You'd need to basically include microsoft's code for the mouse to move the mouse, hence windows.form.dll
Answer by NightmarexGR · Apr 30, 2014 at 11:15 AM
Hello guys it seems this problem is still active, for this reason i created a unity API that lets you set the cursor's position on screen "SetCursorPosition(x : int,y : int)"
http://forum.unity3d.com/threads/242832-Official-Set-Cursor-Position?p=1606714#post1606714
I know this thread is 3 years old, but I've seen you paste this comment on a bunch of related threads. Your workaround is for Windows only and uses js ins$$anonymous$$d of c#. Both of which are inadvisable. Even if it were multiplat, one would first be faced with translating to c#.
5 years later, the asset is a paid solution for windows only but has great support and works with c# just fine https://assetstore.unity.com/packages/tools/input-management/set-cursor-position-mouse-clicks-api-17177?aid=1101liJWIxCr
Your answer
Follow this Question
Related Questions
Locking cursor without changing position 1 Answer
custom mouse changing when hovering over objects 1 Answer
Disable/enable script and animation when you move your mouse cursor 1 Answer
Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer
get the mouse cursor position x and y and make gui follow on trigger -1 Answers