public float?     fov;                                          // The desired FOV of the camera (optional).

        // Lerp between two CameraInput values.
        public static CameraInput Lerp(CameraInput from, CameraInput to, float t)
        {
            CameraInput result = new CameraInput();

            result.position = Vector3.Lerp(from.position, to.position, t);
            result.rotation = Quaternion.Slerp(from.rotation, to.rotation, t);

            if (from.fov.HasValue && to.fov.HasValue)
            {
                result.fov = Mathf.Lerp(from.fov.Value, to.fov.Value, t);
            }

            return(result);
        }
Exemple #2
0
        void LateUpdate()
        {
            // Call on each controller (camera, steering, and aiming) to do their thing.

            // Camera
            // Transition between the previous and current camera controllers if within transition transition time.
            if (Time.time <= cameraTransitionEndTime && previousCameraController && cameraController)
            {
                // Get previous and new CameraController inputs
                CameraInput previousCameraInput = previousCameraController.UpdateCamera(ref controlReferences);
                CameraInput newCameraInput      = cameraController.UpdateCamera(ref controlReferences);

                // Set FOV to default if not set by controller to prepare for lerp.
                if (!previousCameraInput.fov.HasValue)
                {
                    previousCameraInput.fov = defaultCameraFOV;
                }
                if (!newCameraInput.fov.HasValue)
                {
                    newCameraInput.fov = defaultCameraFOV;
                }

                // Calculate ratio from start to end transition time
                float t = Mathf.InverseLerp(cameraTransitionEndTime - cameraTransitionTime, cameraTransitionEndTime, Time.time);
                // Smooth out ratio value
                t = Mathf.SmoothStep(0, 1, Mathf.SmoothStep(0, 1, t));
                // Lerp between them
                cameraInput = CameraInput.Lerp(previousCameraInput, newCameraInput, t);
            }
            else
            {
                // Exit the previous camera.
                if (previousCameraController)
                {
                    previousCameraController.Exit(ref controlReferences);
                    previousCameraController = null;
                }

                // Update the camera with the current controller.
                if (cameraController)
                {
                    cameraInput = cameraController.UpdateCamera(ref controlReferences);
                }
            }

            // Update rig position/rotation with camera controller input.
            transform.position = cameraInput.position;
            transform.rotation = cameraInput.rotation;
            // Update FOV
            if (driverCamera)
            {
                driverCamera.fieldOfView = cameraInput.fov.HasValue ? cameraInput.fov.Value : defaultCameraFOV;
            }

            // Steering
            if (steeringController)
            {
                vehicleInput = steeringController.UpdateSteering(ref controlReferences);
            }

            if (vehicle)
            {
                // Provide input to vehicle and turrets.
                vehicle.input = vehicleInput;

                // Aiming
                if (aimingController)
                {
                    turretInput = aimingController.UpdateAiming(ref controlReferences);
                }

                foreach (Turret turret in turrets)
                {
                    turret.input = turretInput;
                }
            }
        }