/// <summary> /// Updates the rotation of the camera based on the target and /// </summary> public void UpdateRotation() { if (!Enabled) { return; } FixOffsets(); Quaternion targetRotation = GetCurrentRotation(); if (!DoesRotationNeedUpdating(targetRotation)) { return; } // The actual angle between the previous rotation and the current rotation. float angle = Quaternion.Angle(_previousTargetState.Rotation, targetRotation); // The t for lerping consistantly at the angles per second value. // If we're snapping, then we always do 1, otherwise, we do math. // We use mathf.abs because negative angles per second doesn't make sense. // I could force the AnglePerSecond value to change, but I decided to leave it incase people track angle per second values negatively here. float t = SnapRotation ? 1 : Mathf.Clamp01(Mathf.Abs(AnglePerSecond * Time.deltaTime) / angle); // We're going to be finished if t is equal to 1 (or greater, but because we're clamping that shouldn't be an issue.) _finishedAdjustment = t >= 1; if (t >= 1) { _forceAdjustment = false; } // Hit the t with the lerp transformer after we've done everything we have to with the real t value. // This honestly probably won't be used by many if any people, but the option is here if you want to mess with it. t = RotationLerpTransformer.Process(t); // Calculate the rotation we want for this update Quaternion rotation = Quaternion.Lerp(_previousTargetState.Rotation, targetRotation, t); // Actually rotate the camera CameraTransform.Rotation = rotation; CameraTransform.Rotate(CameraTransform.Right, YRotationOffset); CameraTransform.Rotate(_target.Target.up, -XRotationOffset); // Set the previous state information _previousTargetState.Rotation = rotation; _previousTargetState.Position = _target.Target.position; _previouslyDeterminedRotation = targetRotation; }
/// <summary> /// If the Offsets for rotation are adjusted at run time, this will fix the rotation. /// </summary> private void FixOffsets() { if (_previousXRotationOffset != XRotationOffset) { float diff = XRotationOffset - _previousXRotationOffset; _previousXRotationOffset = XRotationOffset; CameraTransform.Rotate(_target.Target.up, -diff); } if (_previousYRotationOffset != YRotationOffset) { float diff = YRotationOffset - _previousYRotationOffset; _previousYRotationOffset = YRotationOffset; CameraTransform.Rotate(CameraTransform.Right, diff); } }