物品跟随鼠标移动
正交视角
void Update() { Vector3 v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition); transform.position = new Vector3(v3.x, v3.y, 0); }
透视角
void Update() { Vector3 v3 = GetWorldPositionOnPlane(Input.mousePosition); transform.position = new Vector3(v3.x, v3.y, 0); } public Vector3 GetWorldPositionOnPlane(Vector3 screenPosition, float z = 0) { Ray ray = Camera.main.ScreenPointToRay(screenPosition); Plane xy = new Plane(Vector3.forward, new Vector3(0, 0, z)); float distance; xy.Raycast(ray, out distance); return ray.GetPoint(distance); }