Search Unity

Move/Set mouse cursor position

Discussion in 'Scripting' started by magomedmirz, Jun 9, 2019.

  1. magomedmirz

    magomedmirz

    Joined:
    Jun 9, 2019
    Posts:
    9
    Hello,

    Im trying to write a script where i move/set the mouse cursor position to a specific destination (for example the position of an gameobject) in 2D.

    Recently i tried it with:

    Code (CSharp):
    1. [DllImport("user32.dll")]
    2.     public static extern bool SetCursorPos(int X, int Y);
    it does change the position of the mouse, but always to an position i didnt intend to. So as i converted the mouseposition (or mouseposition world) x and y to an Int and tried the function for testing, it always sends the mouse somewhere else. Shouldnt it stay on the same position?
    My tought is that im not using the correct position parameters.

    Does someone know how i can move/set the mouse cursor position to the position of an specific gameobject? With the function above or an different approach (without an plugin).
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  3. magomedmirz

    magomedmirz

    Joined:
    Jun 9, 2019
    Posts:
    9
    I tryed, but WorldToScreenPoint always gives me values around (40000, 30000, 10).

    While
    Code (CSharp):
    1. Input.mousePosition
    return values between -500 and 2000. Also the only positions which doesnt move the cursor with the SetCursorPos function to the edge of the screen. It just deviate always from the position i actually want.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    ok, I have tested it and seems to work only in the fullscreen (in the build)
    at first, setcursorpos is counting from the top left (worldtoscreen from the bottom left)
    at second, it working ok in the fullscreen, maybe it can be forced to work in the editor but I have no idea, how to do this :p

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.InteropServices;
    4. using UnityEngine;
    5.  
    6. public class MousePos : MonoBehaviour
    7. {
    8.     [DllImport("user32.dll")]
    9.     public static extern bool SetCursorPos(int X, int Y);
    10.  
    11.     public GameObject somethingToTest;
    12.  
    13.     private void Start()
    14.     {
    15.         Vector3 tmpScreenPos = Camera.main.WorldToScreenPoint(somethingToTest.transform.position);
    16.         SetCursorPos((int) tmpScreenPos.x, Screen.height - (int) tmpScreenPos.y);
    17.     }
    18. }
     
    cosmochristo and magomedmirz like this.
  5. magomedmirz

    magomedmirz

    Joined:
    Jun 9, 2019
    Posts:
    9

    It also works for me if i use fullscreen :). If it stays consistant in the game later on, then i got what i needed.

    Thank you for your help
     
  6. ziyadedris

    ziyadedris

    Joined:
    Mar 11, 2021
    Posts:
    8
    I know this is old, but can someone please explain this, because I don't understand how to use it ╯︿╰
     
    Lenny991 likes this.
  7. ziyadedris

    ziyadedris

    Joined:
    Mar 11, 2021
    Posts:
    8
    omg finally, I got it to work, here is what I understand
    so at the top of the script, you have to write:
    using System.Runtime.InteropServices;

    otherwise, it will tell you that nothing is called DllImport

    then write this:

    [DllImport("user32.dll")]
    public static extern bool SetCursorPos(int X, int Y);
    public GameObject somethingToTest;


    for the somethingToTest, I put the main camera

    then write this in the void Start:

    tmpScreenPos = Camera.main.WorldToScreenPoint(somethingToTest.transform.position);
    SetCursorPos((int)tmpScreenPos.x + 175, (int)tmpScreenPos.y + 140);


    TO EXIT PLAY MODE PRESS: Ctrl + P

    the 175 and 140 are what made the mouse go to the middle
    then beside the 175, add or subtract whatever you want to move the mouse in the X-axis
    and beside the 140, add or subtract whatever you want to move the mouse in the Y-axis

    now you can move the mouse wherever you want here is what I did to move the mouse

    void Update()
    {
    OneAdder -= 100 * Time.deltaTime;
    if(OneAdder <= 0){
    OneAdder = 1;
    Xpos += 1;
    SetCursorPos((int)tmpScreenPos.x + 175 + Xpos, (int)tmpScreenPos.y + 140);
    }
    }

    the Xpos is = 0
    and you can change the 100 to make the tick or update faster or slower
    or change the Xpos += 1 to make it move faster or slower, I'm bad at explaining ) :

    also, do this to make it so you can move your mouse

    if(Input.GetKey("s")){
    Destroy(gameObject.GetComponent<MouseManager>());
    }
     
    Last edited: Jun 6, 2021
    cosmochristo likes this.
  8. ziyadedris

    ziyadedris

    Joined:
    Mar 11, 2021
    Posts:
    8
    But I still don't know what is the reason of the something to test gameObject
     
  9. cosmochristo

    cosmochristo

    Joined:
    Sep 24, 2018
    Posts:
    250
    I get a not found error relating to the dll, perhaps because it is PC specific? (using a mac).
    Is there not a more general solution supported by Unity?
     
    EaglemanGameDev likes this.
  10. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Yes the DLL is Windows specific.

    You can set the mouse position cross platform using the new Input system with.
    Code (CSharp):
    1. UnityEngine.InputSystem.Mouse.current.WarpCursorPosition(pos);
    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.4/manual/Mouse.html
     
    kayy, ToshoDaimos and cosmochristo like this.
  11. cosmochristo

    cosmochristo

    Joined:
    Sep 24, 2018
    Posts:
    250
    thatnk you! That was not revealed in my searches! I have just moved to the new input system so good timing!
     
  12. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    This may be useful on systems you control, such as during a testing phase, but I would not recommend it in gameplay on the user's machine. Different mouse-like devices handle the mouse differently and probably have odd interactions with this. Window capturing pointer code, and mouse "warping" code, often accomplish nothing to touchscreens or absolute touchpads or pen inputs, for example.
     
    cosmochristo likes this.