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. :slight_smile:

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 to set the mouse position

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 believe using the “new” UnityEngine.InputSystem solves this problem with WarpCursorPosition. This also works on a variety of platforms. Example:

private void SetMousePosition(Vector3 worldPosition)
{
    Vector3 screenPoint = Camera.main.WorldToScreenPoint(worldPosition);
    Mouse.current.WarpCursorPosition(screenPoint);
}

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

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.

There is also a paid solution in the asset store that works for Window only: Set Cursor Position + Mouse Clicks API | Input Management | Unity Asset Store

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.