Unity 拖动相机跟随鼠标(手指)移动(例如移动小地图)
if (Input.GetMouseButtonDown(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = miniCamera.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray, out distance))
{
dragStartPos = ray.GetPoint(distance);
}
}
if (Input.GetMouseButton(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = miniCamera.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray, out distance))
{
dragCurrentPos = ray.GetPoint(distance);
Vector3 difference = dragStartPos - dragCurrentPos;
Debug.Log(difference);
newPos = miniCamera.transform.position + difference;
}
miniCamera.transform.position = Vector3.Lerp(miniCamera.transform.position, newPos, moveTime * Time.deltaTime);
}
```
|