我在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
}
}
您需要向应用添加真正的触摸支持。
我不知道你是否真的理解你得到的剧本。
一团糟。我会帮你解决的。
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.touchCount > 0 && !isDragging )
{
isDragging = true;
// save the mouse starting position
startMouseX = Input.GetTouch(0).position.x;
startMouseY = Input.GetTouch(0).position.y;
}
else if(Input.touchCount == 0 && isDragging)
{
// set the flag to false
isDragging = false;
}
}
void LateUpdate()
{
if(isDragging)
{
float endMouseX = Input.GetTouch(0).position.x;
float endMouseY = Input.GetTouch(0).position.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
}
}
请注意,这段代码很糟糕,很混乱,建议不要使用它。它是未经测试的,这意味着它可能会也可能不会起作用。
我只是来修复你得到的东西。
如果您想了解有关 Unity 中移动输入的更多信息,请阅读此内容
祝你好运