Beispiel #1
0
    public void LateUpdate()
    {
        if (!mainCamera)
        {
            return;
        }
        if (player.Paused && mainCamera != null)
        {
            mainCamera.transform.localPosition = new Vector3(-85.77416f, 32.8305f, -69.88891f);
            mainCamera.transform.localRotation = Quaternion.Euler(16.48679f, 21.83607f, 6.487632f);
            return;
        }

        if (player.networkView.isMine)
        {
            // Update inferred position and velocity
            Vector3 newPosition   = player.transform.position;
            Vector3 newVelocity   = (newPosition - LastInferredBodyPosition) / Time.deltaTime;
            Vector3 velocityDelta = newVelocity - LastInferredVelocity;
            LastInferredVelocity     = newVelocity;
            LastInferredBodyPosition = newPosition;
            AddYSpringImpulse(velocityDelta.y);
            YSpring.Update();
            // Clamp magnitude to prevent camera from clipping through ground on hard landings
            YSpring.CurrentValue = Mathf.Clamp(YSpring.CurrentValue, -4f, 4f);
            if (IsExteriorView)
            {
                BarrelFirstPersonOffsetTransform.localPosition = Vector3.zero;
            }
            else
            {
                // Values between 0.3f and 0.8f seem to look best for scaling the gun offset Y.
                BarrelFirstPersonOffsetTransform.localPosition = new Vector3(0f, YSpring.CurrentValue * 0.3f, 0f);
            }

            ViewBobSpring.Update();

            // Higher delay time when the camera is further from the gun
            QueuedScreenRecoils.DelayTime = IsExteriorView ? 0.071f : 0.048f;
            //QueuedScreenRecoils.DelayTime = 0.06f;
            // Remove stuff from the lag queue and put it in the actual spring
            foreach (var eulerAngles in QueuedScreenRecoils.Update())
            {
                CosmeticSpring.AddImpulse(eulerAngles);
            }
            CosmeticSpring.Update();

            if (Input.GetButtonDown("ToggleCameraSmoothing"))
            {
                HasSmoothedRotation = !HasSmoothedRotation;
            }
            if (Input.GetButtonDown("ToggleRaycastCrosshair"))
            {
                UsesRaycastCrosshair = !UsesRaycastCrosshair;
            }

            // Smooth changes in base field of view (if player adjusts FOV, we
            // want it to be a slower smoothing than zooming in/out)
            SmoothedBaseFieldOfView = Mathf.Lerp(SmoothedBaseFieldOfView, BaseFieldOfView,
                                                 1.0f - Mathf.Pow(0.0001f, Time.deltaTime));
            // Interpolate field of view, set on main camera if necessary
            SmoothedFieldOfView = Mathf.Lerp(SmoothedFieldOfView, DesiredFieldOfView,
                                             1.0f - Mathf.Pow(0.000001f, Time.deltaTime));
            if (mainCamera != null)
            {
                if (!Mathf.Approximately(mainCamera.fieldOfView, SmoothedFieldOfView))
                {
                    mainCamera.fieldOfView = SmoothedFieldOfView;
                }
            }

            // Update and smooth view position
            SmoothedViewOffset = Vector3.Lerp(SmoothedViewOffset, DesiredViewOffset,
                                              1.0f - Mathf.Pow(0.00001f, Time.deltaTime));
            transform.localPosition = SmoothedViewOffset;

            // TODO we need smarter handling for toggilng smoothing and first/third person at the same time
            if (HasSmoothedRotation && IsExteriorView)
            {
                // TODO make a nicer interface for goofy power curve
                var amt = (float)Math.Pow(0.0000000000001, Time.deltaTime);
                actualCameraRotation = Quaternion.Slerp(actualCameraRotation, transform.rotation, 1.0f - amt);
            }
            else
            {
                actualCameraRotation = transform.rotation;
            }

            Vector3 scaledLocalPosition = Vector3.Scale(transform.localPosition, transform.lossyScale);
            Vector3 direction           = actualCameraRotation * scaledLocalPosition;
            Vector3 cameraPosition      = transform.parent.position + direction;

            // Modify Y for spring
            cameraPosition.y += YSpring.CurrentValue;

            // We don't want to use view bob when zoomed in: we want the
            // reticule to be very responsive.
            Quaternion usedViewBob = Quaternion.Lerp(
                ViewBobSpring.CurrentValue, Quaternion.identity, ZoomedAmount);

            // TODO can mainCamera be null here?
            if (mainCamera != null)
            {
                mainCamera.transform.position = cameraPosition;
                mainCamera.transform.rotation =
                    actualCameraRotation *
                    usedViewBob *
                    CosmeticSpring.CurrentValue;
            }


            var rawCrosshairPosition = GetCrosshairPosition();
            SmoothedCrosshairPosition = Vector2.Lerp(SmoothedCrosshairPosition, rawCrosshairPosition,
                                                     1.0f - Mathf.Pow(CrosshairSmoothingSpeed, -CrosshairSmoothingSpeed * Time.deltaTime));
            Camera.main.GetComponent <WeaponIndicatorScript>()
            .CrosshairPosition = SmoothedCrosshairPosition;
        }
    }