AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
MoveWithObject.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 
5 using HoloToolkit.Unity;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Examples.Prototyping
9 {
27  public class MoveWithObject : MonoBehaviour
28  {
29  [Tooltip("The game object this object will follow : Main Camera by default")]
30  public GameObject ReferenceObject;
31 
32  [Tooltip("Auto start? or status")]
33  public bool IsRunning = false;
34 
35  [Tooltip("translation speed : higher is faster")]
36  public float LerpPositionSpeed = 1f;
37 
38  [Tooltip("rotation speed : higher is faster")]
39  public float LerpRotationSpeed = 0.5f;
40 
41  [Tooltip("Lock the x axis if the object is set to face the reference object")]
42  public bool KeepUpRight = false;
43 
44  [Tooltip("An game object containing an Interactive component to call on air-tap")]
45  public GameObject ReferenceInteractive;
46 
47  [Tooltip("Does not center the object to the reference object's transform.forward vector")]
48  public bool KeepStartingOffset = true;
49 
50  [Tooltip("Force the object to always face the reference object")]
51  public bool FaceObject = true;
52 
53  [Tooltip("Force the object to keep relative to the reference object's transform.forward")]
54  public bool KeepInFront = true;
55 
56  [Tooltip("Magnetism speed to move closer to the reference object")]
57  public float Magnetism = 0;
58 
59  [Tooltip("Minimum distance to stay away from the reference object if magnetism is used")]
60  public float MagnetismPaddingDistance = 1f;
61 
62  // the position different between the objects position and the reference object's transform.forward
63  private Vector3 mOffsetDirection;
64 
65  // this object's direction
66  private Vector3 mDirection;
67 
68  // the offset rotation at start
69  private Quaternion mOffsetRotation;
70 
71  // the offset distance at start
72  private float mOffsetDistance = 0;
73 
74  // the amount of magnetism to apply
75  private float mMagnetismPercentage = 1;
76 
77  private Vector3 mNormalzedOffsetDirection;
78 
82  private void Awake()
83  {
84  if (ReferenceObject == null)
85  {
86  ReferenceObject = CameraCache.Main.gameObject;
87  }
88  }
89 
90  // start the object following the reference object
91  public void StartRunning()
92  {
93 
94  if (ReferenceObject == null)
95  ReferenceObject = CameraCache.Main.gameObject;
96 
97  mOffsetDirection = this.transform.position - ReferenceObject.transform.position;
98  mOffsetDistance = mOffsetDirection.magnitude;
99  mDirection = ReferenceObject.transform.forward.normalized;
100  mNormalzedOffsetDirection = mOffsetDirection.normalized;
101  mOffsetRotation = Quaternion.FromToRotation(mDirection, mNormalzedOffsetDirection);
102  IsRunning = true;
103 
104  mMagnetismPercentage = 1;
105 
106  if (ReferenceInteractive != null)
107  {
108  InputManager.Instance.PushModalInputHandler(ReferenceInteractive);
109  }
110  }
111 
115  public void StopRunning()
116  {
117 
118  IsRunning = false;
119 
120  if (ReferenceInteractive != null)
121  {
122  InputManager.Instance.PopModalInputHandler();
123  }
124  }
125 
131  protected virtual void UpdatePosition(Vector3 position, float time)
132  {
133  // update the position
134  this.transform.position = Vector3.Lerp(this.transform.position, position, LerpPositionSpeed * time);
135 
136  // rotate to face the reference object
137  if (FaceObject)
138  {
139  Quaternion forwardRotation = Quaternion.LookRotation(this.transform.position - ReferenceObject.transform.position);
140  this.transform.rotation = Quaternion.Lerp(this.transform.rotation, forwardRotation, LerpRotationSpeed * time);
141  }
142 
143  // lock the x axis
144  if (KeepUpRight)
145  {
146  Quaternion upRotation = Quaternion.FromToRotation(this.transform.up, Vector3.up);
147  this.transform.rotation = upRotation * this.transform.rotation;
148  }
149 
150  }
151 
155  protected virtual void Update()
156  {
157  if (IsRunning)
158  {
159  Vector3 newDirection = ReferenceObject.transform.forward;
160 
161  // move the object in front of the reference object
162  if (KeepInFront)
163  {
164  if (KeepStartingOffset)
165  {
166  newDirection = Vector3.Normalize(mOffsetRotation * ReferenceObject.transform.forward);
167  }
168  }
169  else
170  {
171  newDirection = mNormalzedOffsetDirection;
172  // could we allow drifting?
173  }
174 
175  // move toward the reference object
176  if (Magnetism > 0)
177  {
178 
179  float magnetismDelta = MagnetismPaddingDistance / (mOffsetDistance * mMagnetismPercentage) - 1;
180  if (Mathf.Abs(magnetismDelta * 100) > 0.01f)
181  {
182  mMagnetismPercentage += Time.deltaTime * magnetismDelta * Magnetism;
183  }
184  }
185 
186  Vector3 lerpPosition = ReferenceObject.transform.position + newDirection * mOffsetDistance * mMagnetismPercentage;
187 
188  UpdatePosition(lerpPosition, Time.deltaTime);
189  }
190  }
191  }
192 }
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
virtual void UpdatePosition(Vector3 position, float time)
update the position of the object based on the reference object and configuration ...
Makes the assigned object follow and face another object. A potential use is moving a UI panel around...
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
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
void StopRunning()
stop the object from following