【问题标题】:Drag 360 view camera in unity统一拖动360视图相机
【发布时间】: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
    }
}

【问题讨论】:

    标签: c# unity3d touch


    【解决方案1】:

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

    我不知道你是否真的理解你得到的脚本。

    真是一团糟。我会为你修好它。

    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

    祝你好运

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多