Unity3D - Как повернуть камеру, как Google Earth? - PullRequest
0 голосов
/ 07 сентября 2018

Я создаю игру в 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;
     }
 }

1 Ответ

0 голосов
/ 07 сентября 2018

Простой код может быть таким:

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);
        }
    }
}

enter image description here

A gif execution of the code explained above.

Ура!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...