示例#1
0
        public void AlignCameraRotationWithTrack()
        {
            UnityEditor.Undo.RecordObject(transform, ALIGN_ROTATION_FUNCTION_NAME);

            TrackPath track = FindObjectOfType <TrackPath>();

            if (track == null)
            {
                Debug.LogError("No track path found in scene; cannot align camera with track.", this);
                return;
            }

            if (track.LinearPath == null)
            {
                track.GenLinearPath();
            }

            Vector3 pointOnPath;
            Vector3 trackDirection;
            Vector3 trackRight;

            track.LinearPath.GetClosestPointOnPath(transform.position, out pointOnPath, out trackDirection, out trackRight, true);
            Vector3 trackUp = Vector3.Cross(trackDirection, trackRight);

            Debug.DrawLine(transform.position, pointOnPath, Color.green, 5f);
            Debug.DrawLine(pointOnPath, pointOnPath + trackUp * 2.5f, Color.green, 5f);

            transform.SetLookRotation(trackDirection, trackUp);
        }