【问题标题】:Rotate Camera around a gameObject on Mouse drag in Unity在 Unity 中鼠标拖动时围绕游戏对象旋转相机
【发布时间】:2019-02-24 12:45:02
【问题描述】:

我想在拖动鼠标时围绕游戏对象(比如一个立方体)旋转相机,以模拟游戏对象正在旋转的感觉(就像我们在场景编辑器中旋转对象以及在购物网站中一样)。

下面的脚本是我正在使用的脚本。但有时脚本的行为非常奇怪。摄像机沿与预期方向相反的方向旋转。为什么会这样?我需要对代码进行哪些更改才能使其正常工作?请帮忙。

using UnityEngine;
using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour
{
    public Camera cameraObj;
    public GameObject myGameObj;
    public float speed = 2f;

    void Update()
    {
        RotateCamera();
    }

    void RotateCamera()
    {
        if(Input.GetMouseButton(0))
        {
         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         Vector3.up,
                                         -Input.GetAxis("Mouse X")*speed);

         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         Vector3.right,
                                         -Input.GetAxis("Mouse Y")*speed);
        } 

    }
}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    我认为是参考轴引起的。

    因为您使用了Vector3.upVector3.right,而不是相机的那个,所以它没有按照预期的方向旋转。 所以,你应该像下面这样改变。

    void RotateCamera()
    {
        if(Input.GetMouseButton(0))
        {
         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         cameraObj.transform.up,
                                         -Input.GetAxis("Mouse X")*speed);
    
         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         cameraObj.transform.right,
                                         -Input.GetAxis("Mouse Y")*speed);
        } 
    
    }
    

    【讨论】:

    • 这段代码对你有用吗?对我来说,它以同样奇怪的行为旋转。我想围绕对象旋转相机,这意味着我应该使用相机应该围绕其旋转的游戏对象轴。所以我什至尝试了myGameObj.transform.upmyGameObj.transform.right。这也不起作用。
    • 我不知道“奇怪的行为”到底是什么意思。你能解释一下更多细节吗?
    • 在某些方向上,当我将鼠标从左向右移动时,立方体似乎在从右向左移动。垂直鼠标拖动也会发生同样的事情。
    • 对不起,我无法理解你的情况。在我的工作区中,它按我的意图运行良好。
    【解决方案2】:

    我在 Unity 中使用鼠标或触摸旋转相机的解决方案

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    public class CameraRotator : MonoBehaviour
    {
        public Transform target;
        public Camera mainCamera;
        [Range(0.1f, 5f)]
        [Tooltip("How sensitive the mouse drag to camera rotation")]
        public float mouseRotateSpeed = 0.8f;
        [Range(0.01f, 100)]
        [Tooltip("How sensitive the touch drag to camera rotation")]
        public float touchRotateSpeed = 17.5f;
        [Tooltip("Smaller positive value means smoother rotation, 1 means no smooth apply")]
        public float slerpValue = 0.25f; 
        public enum RotateMethod { Mouse, Touch };
        [Tooltip("How do you like to rotate the camera")]
        public RotateMethod rotateMethod = RotateMethod.Mouse;
    
    
        private Vector2 swipeDirection; //swipe delta vector2
        private Quaternion cameraRot; // store the quaternion after the slerp operation
        private Touch touch;
        private float distanceBetweenCameraAndTarget;
    
        private float minXRotAngle = -80; //min angle around x axis
        private float maxXRotAngle  = 80; // max angle around x axis
    
        //Mouse rotation related
        private float rotX; // around x
        private float rotY; // around y
        private void Awake()
        {
            if (mainCamera == null)
            {
                mainCamera = Camera.main;
            }
    
            
        }
        // Start is called before the first frame update
        void Start()
        {
            distanceBetweenCameraAndTarget = Vector3.Distance(mainCamera.transform.position, target.position);
        }
    
        // Update is called once per frame
        void Update()
        {
            if (rotateMethod == RotateMethod.Mouse)
            {
                if (Input.GetMouseButton(0))
                {
                    rotX += -Input.GetAxis("Mouse Y") * mouseRotateSpeed; // around X
                    rotY += Input.GetAxis("Mouse X") * mouseRotateSpeed;
                }
    
                if (rotX < minXRotAngle)
                {
                    rotX = minXRotAngle;
                }
                else if (rotX > maxXRotAngle)
                {
                    rotX = maxXRotAngle;
                }
            }
            else if (rotateMethod == RotateMethod.Touch)
            {
                if (Input.touchCount > 0)
                {
                    touch = Input.GetTouch(0);
                    if (touch.phase == TouchPhase.Began)
                    {
                        //Debug.Log("Touch Began");
    
                    }
                    else if (touch.phase == TouchPhase.Moved)
                    {
                        swipeDirection += touch.deltaPosition * Time.deltaTime * touchRotateSpeed;
                    }
                    else if (touch.phase == TouchPhase.Ended)
                    {
                        //Debug.Log("Touch Ended");
                    }
                }
    
                if (swipeDirection.y < minXRotAngle)
                {
                    swipeDirection.y = minXRotAngle;
                }
                else if (swipeDirection.y > maxXRotAngle)
                {
                    swipeDirection.y = maxXRotAngle;
                }
            }
    
        }
    
        private void LateUpdate()
        {
    
            Vector3 dir = new Vector3(0, 0, -distanceBetweenCameraAndTarget); //assign value to the distance between the maincamera and the target
    
            Quaternion newQ; // value equal to the delta change of our mouse or touch position
            if (rotateMethod == RotateMethod.Mouse)
            {
               newQ  = Quaternion.Euler(rotX , rotY, 0); //We are setting the rotation around X, Y, Z axis respectively
            }
            else
            {
                newQ = Quaternion.Euler(swipeDirection.y , -swipeDirection.x, 0);
            }
            cameraRot = Quaternion.Slerp(cameraRot, newQ, slerpValue);  //let cameraRot value gradually reach newQ which corresponds to our touch
            mainCamera.transform.position = target.position + cameraRot * dir;
            mainCamera.transform.LookAt(target.position);
    
        }
    
        public void SetCamPos()
        {
            if(mainCamera == null)
            {
                mainCamera = Camera.main;
            }
            mainCamera.transform.position = new Vector3(0, 0, -distanceBetweenCameraAndTarget);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      要按照鼠标移动的方向移动立方体,请更改代码,例如:

      void RotateCamera()
      {
          if (Input.GetMouseButton(0))
          {
              cameraObj.transform.RotateAround(myGameObj.transform.position,
                                              Vector3.up,
                                              Input.GetAxis("Mouse X") * speed);
      
              cameraObj.transform.RotateAround(myGameObj.transform.position,
                                              Vector3.right,
                                              -Input.GetAxis("Mouse Y") * speed);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-05
        • 2021-03-13
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        相关资源
        最近更新 更多