1

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?

jklw10
  • 117
  • 7

1 Answers1

2

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.

rob1997
  • 176
  • 7
  • It would work probably if i can get the accurate screen position from unity but i'd really like to avoid this since it really doesn't seem multiplatform... Guess o could find out how to do this for everyplatform i want it to work for... – jklw10 Dec 14 '19 at 17:25
  • there're ways you can do it for each target platform of course but it'll take a bit of research and I hope I guided you in the right direction and give me that sweet sweet green tick :) thanks. – rob1997 Dec 14 '19 at 20:33
  • i'm going to see if there are any tools ready made that do this already multiplatformly if not i'll give u a cookie – jklw10 Dec 15 '19 at 18:38
  • well this would work but it's not multiplatform. if someone finds the same thing for linux and osx make sure to add them. – jklw10 Dec 19 '19 at 10:22
  • I found a bit of a lead on Linux here https://stackoverflow.com/questions/16727215/how-to-set-cursor-position-in-gtk-linux-monodevelop as for OSX I found no leads at all but the way I see it you'll have to significantly change your linux/osx build, so basically you'll have to maintain three projects. I now understand why big studios never release on these platforms anyways thanks for the cookie :) – rob1997 Dec 19 '19 at 20:10