AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SphereBasedTagalong.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
12  public class SphereBasedTagalong : MonoBehaviour
13  {
14  [Tooltip("Sphere radius.")]
15  public float SphereRadius = 1.0f;
16 
17  [Tooltip("How fast the object will move to the target position.")]
18  public float MoveSpeed = 2.0f;
19 
23  [SerializeField]
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;
26 
30  [SerializeField]
31  [Tooltip("Used to initialize the initial position of the SphereBasedTagalong before being hidden on LateUpdate.")]
32  private bool hideOnStart;
33 
34  [SerializeField]
35  [Tooltip("Display the sphere in red wireframe for debugging purposes.")]
36  private bool debugDisplaySphere = false;
37 
38  [SerializeField]
39  [Tooltip("Display a small green cube where the target position is.")]
40  private bool debugDisplayTargetPosition = false;
41 
42  private Vector3 targetPosition;
43  private Vector3 optimalPosition;
44  private float initialDistanceToCamera;
45 
46  private void Start()
47  {
48  initialDistanceToCamera = Vector3.Distance(transform.position, CameraCache.Main.transform.position);
49  }
50 
51  private void Update()
52  {
53  optimalPosition = CameraCache.Main.transform.position + CameraCache.Main.transform.forward * initialDistanceToCamera;
54  Vector3 offsetDir = transform.position - optimalPosition;
55 
56  if (offsetDir.magnitude > SphereRadius)
57  {
58  targetPosition = optimalPosition + offsetDir.normalized * SphereRadius;
59 
60  float deltaTime = useUnscaledTime
61  ? Time.unscaledDeltaTime
62  : Time.deltaTime;
63 
64  transform.position = Vector3.Lerp(transform.position, targetPosition, MoveSpeed * deltaTime);
65  }
66  }
67 
68  private void LateUpdate()
69  {
70  if (hideOnStart)
71  {
72  hideOnStart = !hideOnStart;
73  gameObject.SetActive(false);
74  }
75  }
76 
77  public void OnDrawGizmos()
78  {
79  if (Application.isPlaying == false) { return; }
80 
81  Color oldColor = Gizmos.color;
82 
83  if (debugDisplaySphere)
84  {
85  Gizmos.color = Color.red;
86  Gizmos.DrawWireSphere(optimalPosition, SphereRadius);
87  }
88 
89  if (debugDisplayTargetPosition)
90  {
91  Gizmos.color = Color.green;
92  Gizmos.DrawCube(targetPosition, new Vector3(0.1f, 0.1f, 0.1f));
93  }
94 
95  Gizmos.color = oldColor;
96  }
97  }
98 }
A Tagalong that stays at a fixed distance from the camera and always seeks to stay on the edge or ins...
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn't been cached yet...
Definition: CameraCache.cs:20