基本上,我需要单击并拖动相机,但每次都不需要将光标传送到屏幕中央。 到目前为止,我的摄影机移动是:
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);
}
}
到目前为止,我碰到的唯一事情是通过.NET进行移动,但这根本不是多平台的,而且似乎也很糟糕。 我唯一的选择就是在我可以控制的Vector3位置上制作一个“虚拟鼠标”,这对已经内置的GUI引擎不起作用吗?
答案 0 :(得分:1)
有一种解决方法
无论何时解锁光标,都可以将其位置设置为所需位置。
首先使用语句添加这些内容。
using System.Runtime.InteropServices;
using System.Drawing;
然后将这些行添加到您的班级中。
[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();
然后在您的启动功能中使用该功能
void Start()
{
GetCursorPos(out cursorPos);
}
然后,您最终可以在任意位置调用功能SetCursorPos(x, y);
,将光标设置为屏幕上x和y的任意点,作为屏幕上的坐标
我怀疑Vector2是否可以在该函数中方便使用,但是如果您知道偏移量,您可能可以使用。