void LateUpdate() { if (logicCamera == null) { return; } if (objectToFollow != null) { cameraInfo.targetVelocity = (objectToFollow.position - oldTargetPosition) / Time.deltaTime; oldTargetPosition = objectToFollow.position; cameraInfo.targetPosition = objectToFollow.position; cameraInfo.targetRotation = objectToFollow.rotation; } else { cameraInfo.targetPosition = new Vector3(); cameraInfo.targetRotation = new Quaternion(); cameraInfo.targetVelocity = new Vector3(); } cameraInfo.pivotDistance = 0; cameraInfo.pivotOffset = 0; cameraInfo.pivotRotationIsDefined = false; cameraInfo.useSourcePosition = false; cameraInfo.useTargetPosition = false; cameraInfo.fov = currentFov; logicCamera.UpdateCamera(ref cameraInfo); GetComponent <Camera>().fieldOfView = cameraInfo.fov; if (cameraInfo.useSourcePosition) { var deltaPosition = cameraInfo.targetPosition - cameraInfo.sourcePosition; if (deltaPosition.magnitude > 0.01f) { transform.rotation = Quaternion.LookRotation(deltaPosition); } else { transform.rotation = Quaternion.Euler(0, 0, 0); } transform.position = cameraInfo.sourcePosition; ShakeCamera(); } else { var newPosition = cameraInfo.CameraPosition(); var newRotation = cameraInfo.PivotRotation(); transform.position = newPosition; transform.rotation = newRotation; } cameraInfo.cameraSwitched = false; }
public override void UpdateCamera(ref LogicCameraInfo cameraInfo) { Vector3 hitPosition; bool collidedBack = CollisionFromHeadToPosition(cameraInfo.targetPosition, cameraInfo.CameraPosition(), out hitPosition); float targetPivotDistance; if (collidedBack) { targetPivotDistance = (hitPosition - cameraInfo.targetPosition).magnitude; } else { targetPivotDistance = cameraInfo.pivotDistance; } targetPivotDistance = Mathf.Clamp(targetPivotDistance, minDistance, float.MaxValue); if (cameraInfo.cameraSwitched) { currentPivotDistance = targetPivotDistance; cameraInfo.pivotDistance = currentPivotDistance; } else { float lerpSpeed = targetPivotDistance > currentPivotDistance ? moveOutLerpSpeed : moveInLerpSpeed; currentPivotDistance = Mathf.Lerp(currentPivotDistance, targetPivotDistance, Time.deltaTime * lerpSpeed); cameraInfo.pivotDistance = currentPivotDistance; } }