12 [RequireComponent(typeof(BoxCollider), typeof(Interpolator))]
16 [Tooltip(
"The distance in meters from the camera for the Tagalong to seek when updating its position.")]
17 public float TagalongDistance = 2.0f;
18 [Tooltip(
"If true, forces the Tagalong to be TagalongDistance from the camera, even if it didn't need to move otherwise.")]
19 public bool EnforceDistance =
true;
21 [Tooltip(
"The speed at which to move the Tagalong when updating its position (meters/second).")]
22 public float PositionUpdateSpeed = 9.8f;
23 [Tooltip(
"When true, the Tagalong's motion is smoothed.")]
24 public bool SmoothMotion =
true;
25 [Range(0.0f, 1.0f), Tooltip(
"The factor applied to the smoothing algorithm. 1.0f is super smooth. But slows things down a lot.")]
26 public float SmoothingFactor = 0.75f;
41 protected const int frustumLeft = 0;
42 protected const int frustumRight = 1;
43 protected const int frustumBottom = 2;
44 protected const int frustumTop = 3;
49 tagalongCollider = GetComponent<BoxCollider>();
55 interpolator.SmoothPositionLerpRatio = SmoothingFactor;
62 frustumPlanes = GeometryUtility.CalculateFrustumPlanes(mainCamera);
66 Vector3 tagalongTargetPosition;
67 if (CalculateTagalongTargetPosition(transform.position, out tagalongTargetPosition))
76 else if (!interpolator.
Running && EnforceDistance)
81 Ray ray =
new Ray(mainCamera.transform.position, transform.position - mainCamera.transform.position);
82 transform.position = ray.GetPoint(TagalongDistance);
98 bool needsToMove = !GeometryUtility.TestPlanesAABB(frustumPlanes, tagalongCollider.bounds);
104 toPosition = fromPosition;
110 toPosition = cameraTransform.position + cameraTransform.forward * TagalongDistance;
114 Ray ray =
new Ray(toPosition, Vector3.zero);
115 Plane plane =
new Plane();
116 float distanceOffset = 0f;
121 bool moveRight = frustumPlanes[frustumLeft].GetDistanceToPoint(fromPosition) < 0;
122 bool moveLeft = frustumPlanes[frustumRight].GetDistanceToPoint(fromPosition) < 0;
129 plane = frustumPlanes[frustumLeft];
130 ray.direction = -cameraTransform.right;
136 plane = frustumPlanes[frustumRight];
137 ray.direction = cameraTransform.right;
139 if (moveRight || moveLeft)
143 plane.Raycast(ray, out distanceOffset);
147 toPosition.x = ray.GetPoint(distanceOffset).x;
152 bool moveDown = frustumPlanes[frustumTop].GetDistanceToPoint(fromPosition) < 0;
153 bool moveUp = frustumPlanes[frustumBottom].GetDistanceToPoint(fromPosition) < 0;
156 plane = frustumPlanes[frustumTop];
157 ray.direction = cameraTransform.up;
161 plane = frustumPlanes[frustumBottom];
162 ray.direction = -cameraTransform.up;
164 if (moveUp || moveDown)
166 plane.Raycast(ray, out distanceOffset);
167 toPosition.y = ray.GetPoint(distanceOffset).y;
172 ray =
new Ray(cameraTransform.position, toPosition - cameraTransform.position);
176 toPosition = ray.GetPoint(TagalongDistance);