Camera Follow работает частично, нужна помощь до фини sh скрипт - PullRequest
0 голосов
/ 03 марта 2020

У меня есть мини-проект, где шарик вращается вокруг цилиндра, двигаясь вперед по цилиндру. Я хочу, чтобы моя камера следовала за мячом и вращалась по направлению к мячу, в то время как в камерах зрения цилиндр всегда вперед, но не в исходном положении, шар движется вперед. Так, например, если мяч направлен вправо, я хочу, чтобы камера следовала за мячом вправо, но все время указывала на цилиндр, а не наклоняла мяч. Этот сценарий следует за мячом, когда мяч вращается влево / вправо вокруг цилиндра, но не следует за мячом, когда мяч движется вперед.

public class CylinderCam : MonoBehaviour
 {
     //The ball transform
     public Transform target;
     //Camera height
     public float height = 2f;
     //Camera distance
     public float distance = 1f;

     private void LateUpdate ()
     {
         //The forward direction of the cylinder (replace this with your context)
         Vector3 cylinderDir = Vector3.forward;
         //This is the direction from the cylinder to the ball (which is just the ball's position, assuming the cylinder is at the origin
         Vector3 targetDir = target.position;
         //Here we use the vector projection property of the dot product to figure out the up direction of the cylinder relative to the ball
         Vector3 upDir = (targetDir - cylinderDir * Vector3.Dot (targetDir, cylinderDir)).normalized;

         //Move the camera to the required height and distance from the ball
         transform.position = upDir * height - cylinderDir * distance;

         //Here, we just figure out the look direction of the camera to the target, but instead we use our calculated up vector to rotate the camera around the cylinder
         Quaternion rot = Quaternion.LookRotation (target.position - transform.position, upDir);
         transform.rotation = rot;
     }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...