【问题标题】:Unity3D - How to rotate a camera like Google Earth?Unity3D - 如何旋转像谷歌地球这样的相机?
【发布时间】:2018-09-08 14:29:42
【问题描述】:

我正在 Unity3D 中创建一个游戏,其中包含一个场景中的行星。当玩家在屏幕上的任意位置按住右键单击时,我希望相机围绕地球移动并沿鼠标的矢量方向旋转。我写出了如下所示的代码。该代码运行良好,但是如果鼠标在地球上而不是在地球外,玩家只能围绕地球旋转相机。如何更改它,即使鼠标不在地球上,相机也会围绕地球旋转?

public class CameraHumanMovement : MonoBehaviour 
{
     [Header("Settings")]
     public float planetRadius = 100f;
     public float distance;

     public GameObject planet;   
     public Transform camTransform;   
     public Transform holderTransform;

     private Vector3? mouseStartPosition1;
     private Vector3? currentMousePosition1;

     public bool dog;

     private void LateUpdate()
     {

         if (Input.GetMouseButtonDown(1))
             mouseStartPosition = GetMouseHit();

         if (mouseStartPosition != null)
             DragPlanet();

         if (Input.GetMouseButtonUp(1))
             StaticPlanet();
     }

     private void DragPlanet()
     {
         currentMousePosition1 = GetMouseHit();
         RotateCamera((Vector3)mouseStartPosition1, (Vector3)currentMousePosition1);
     }

     private void StaticPlanet()
     {
         mouseStartPosition1 = null;
         currentMousePosition1 = null;
     }

     private void RotateCamera(Vector3 dragStartPosition, Vector3 dragEndPosition)
     {
         //normalised for odd edges
         dragEndPosition = dragEndPosition.normalized * planetRadius;
         dragStartPosition = dragStartPosition.normalized * planetRadius;

         // Cross Product
         Vector3 cross = Vector3.Cross(dragEndPosition, dragStartPosition);

         // Angle for rotation
         float angle = Vector3.SignedAngle(dragEndPosition, dragStartPosition, cross);

         //Causes Rotation of angle around the vector from Cross product
         holderTransform.RotateAround(planet.transform.position, cross, angle);
     }

     private static Vector3? GetMouseHit()
     {
         RaycastHit hit;

         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
         {
             return hit.point;
         }
         return null;
     }
 }

【问题讨论】:

标签: unity3d


【解决方案1】:

一个简单的代码可能是这样的:

public class CameraHumanMovement : MonoBehaviour
{
    //Rotation speed exposed on the inspector
    public float RotationSpeed = 10f;

    private void Update()
    {
        //Whenever the left mouse button is pressed rotate this object that holds this script in the x and y axis. 
        //You can reference your planet and put this script in another object if you want and it will be "yourplanetobject.transform etc.."
        if(Input.GetMouseButton(0))
        {
            float rotX = Input.GetAxis("Mouse X") * RotationSpeed * Time.deltaTime;
            float rotY = Input.GetAxis("Mouse Y") * RotationSpeed * Time.deltaTime;

            transform.Rotate(Vector3.up, -rotX);
            transform.Rotate(Vector3.right, rotY);
        }
    }
}

干杯!

【讨论】:

  • 感谢您的回复。所以我之前实际上已经尝试过这种方法。这种方法的问题在于,一旦相机到达北极或南极,如果你试图朝着正确的矢量方向前进,相机会在其轴上旋转而不是朝着矢量右方向移动,因为事实上它只适用于 2 个轴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多