统一拖动360视图相机

时间:2019-07-13 22:45:54

标签: c# unity3d touch

我正在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
    }
}

1 个答案:

答案 0 :(得分:0)

您需要为您的应用添加真正的触摸支持。

我不知道您是否真正理解所获得的脚本。

一团糟。我会为您修复。

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中移动输入的更多信息,请阅读this

祝你好运