14 [Tooltip(
"Sphere radius.")]
15 public float SphereRadius = 1.0f;
17 [Tooltip(
"How fast the object will move to the target position.")]
18 public float MoveSpeed = 2.0f;
24 [Tooltip(
"When moving, use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
25 private bool useUnscaledTime =
true;
31 [Tooltip(
"Used to initialize the initial position of the SphereBasedTagalong before being hidden on LateUpdate.")]
32 private bool hideOnStart;
35 [Tooltip(
"Display the sphere in red wireframe for debugging purposes.")]
36 private bool debugDisplaySphere =
false;
39 [Tooltip(
"Display a small green cube where the target position is.")]
40 private bool debugDisplayTargetPosition =
false;
42 private Vector3 targetPosition;
43 private Vector3 optimalPosition;
44 private float initialDistanceToCamera;
48 initialDistanceToCamera = Vector3.Distance(transform.position,
CameraCache.
Main.transform.position);
54 Vector3 offsetDir = transform.position - optimalPosition;
56 if (offsetDir.magnitude > SphereRadius)
58 targetPosition = optimalPosition + offsetDir.normalized * SphereRadius;
60 float deltaTime = useUnscaledTime
61 ? Time.unscaledDeltaTime
64 transform.position = Vector3.Lerp(transform.position, targetPosition, MoveSpeed * deltaTime);
68 private void LateUpdate()
72 hideOnStart = !hideOnStart;
73 gameObject.SetActive(
false);
79 if (Application.isPlaying ==
false) {
return; }
81 Color oldColor = Gizmos.color;
83 if (debugDisplaySphere)
85 Gizmos.color = Color.red;
86 Gizmos.DrawWireSphere(optimalPosition, SphereRadius);
89 if (debugDisplayTargetPosition)
91 Gizmos.color = Color.green;
92 Gizmos.DrawCube(targetPosition,
new Vector3(0.1f, 0.1f, 0.1f));
95 Gizmos.color = oldColor;