【发布时间】:2019-07-13 22:45:54
【问题描述】:
我在 Unity 的 360 Image 工作。下面的代码工作正常,但我必须用我的两个手指来拖动相机。
如何使用单点触控来移动?而且我还想在移动时点击对象。
public class DragCamera : MonoBehaviour {
#if UNITY_EDITOR
bool isDragging = false;
float startMouseX;
float startMouseY;
Camera cam;
void Start () {
cam = GetComponent<Camera>();
}
void Update () {
if(Input.GetMouseButtonDown(1) && !isDragging )
{
isDragging = true;
// save the mouse starting position
startMouseX = Input.mousePosition.x;
startMouseY = Input.mousePosition.y;
}
else if(Input.GetMouseButtonUp(1) && isDragging)
{
// set the flag to false
isDragging = false;
}
}
void LateUpdate()
{
if(isDragging)
{
float endMouseX = Input.mousePosition.x;
float endMouseY = Input.mousePosition.y;
//Difference (in screen coordinates)
float diffX = endMouseX - startMouseX;
float diffY = endMouseY - startMouseY;
float newCenterX = Screen.width / 2 + diffX;
float newCenterY = Screen.height / 2 + diffY;
Vector3 LookHerePoint = cam.ScreenToWorldPoint(new Vector3(newCenterX, newCenterY, cam.nearClipPlane));
//Make our camera look at the "LookHerePoint"
transform.LookAt(LookHerePoint);
//starting position for the next call
startMouseX = endMouseX;
startMouseY = endMouseY;
}
}
#endif
}
}
【问题讨论】: