jklw10
jklw10

Reputation: 117

Is there a way to lock the cursor on a specific screen position in unity?

Basically I want a click and drag camera but without teleporting the cursor to the center of the screen every time. What I have for camera movement so far is:

public class CameraController : MonoBehaviour
{
    Vector3 camRot = new Vector3(0, 0, 0);
    Vector3 camPosRot = new Vector3(0, 0, 0);


    public float VSpeed = 2.0f;
    public float HSpeed = 2.0f;
    public Vector3 CameraOffset = new Vector3(0,0,0);
    public Rigidbody Follow;
    public float Distance = 0.1f;

    Vector3 CursorBC;
    Vector3 CameraPos = new Vector3(0, 0, 0);
    // Start is called before the first frame update
    void Start()
    {
        //Follow = GetComponent<Rigidbody>();
        CameraPos = CameraOffset;
    }

    // Update is called once per frame

    void Update()
    {

        camRot += new Vector3(-Input.GetAxis("Mouse Y") * VSpeed, Input.GetAxis("Mouse X") * HSpeed, 0);
        if (Input.GetKey(KeyCode.Mouse1))
        {
            CursorBC = Input.mousePosition;
            Cursor.lockState = CursorLockMode.Locked;
            transform.eulerAngles = camRot;

            camPosRot = new Vector3(camRot.y,camRot.x);
            Vector3 RotationVector = new Vector3(Mathf.Sin(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad), Mathf.Sin(camPosRot.y * Mathf.Deg2Rad), Mathf.Cos(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad));
            CameraPos = new Vector3(CameraOffset.magnitude * -(RotationVector.x), CameraOffset.magnitude * (RotationVector.y), CameraOffset.magnitude * -(RotationVector.z));
        }
        else
        {

            Cursor.lockState = CursorLockMode.None;
        }
        transform.position = (Follow.transform.position + CameraPos);
    }
}

Only thing so far I've come across is to move it through .NET but that wouldn't really be multi-platform at all and also seems like a generally bad idea. Is my only option to make a "virtual mouse" as in a Vector3 position that I can control and wouldn't that not work with the GUI engine unity already has built in?

Upvotes: 1

Views: 3204

Answers (1)

rob1997
rob1997

Reputation: 176

There's a workaround

Whenever you unlock the cursor you can set its position to the position you want.

first add these using statments.

using System.Runtime.InteropServices;
using System.Drawing;

then add these lines in your class.

[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pos);
Point cursorPos = new Point();

then this one in your start function

void Start()
{
   GetCursorPos(out cursorPos);
}

then finally you can call the fucntion SetCursorPos(x, y); anywhere to set the cursor to any point on the screen x and y being coordinates on your screen

I doubt Vector2 will readily be usable in the function but if you figure out the offset you probably can.

Upvotes: 2

Related Questions