AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
MoveToPosition.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 System.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 using UnityEngine.Events;
8 
9 namespace HoloToolkit.Examples.Prototyping
10 {
22  public class MoveToPosition : MonoBehaviour
23  {
32  public enum LerpTypes { Linear, EaseIn, EaseOut, EaseInOut, Free }
33 
34  [Tooltip("The GameObject with the position to animate")]
35  public GameObject TargetObject;
36 
37  [Tooltip("The target Vector3 to animate to")]
38  public Vector3 TargetValue;
39 
40  [Tooltip("Ease type to use for the tween")]
42 
43  [Tooltip("Duration of the animation in seconds")]
44  public float LerpTime = 1f;
45 
46  [Tooltip("Auto start? or status")]
47  public bool IsRunning = false;
48 
49  [Tooltip("Use the local position instead of world position")]
50  public bool ToLocalTransform = false;
51 
52  [Tooltip("animation is complete!")]
53  public UnityEvent OnComplete;
54 
55  // animation ticker
56  private float mLerpTimeCounter;
57 
58  // cached start value, updates every time a new animation starts
59  private Vector3 mStartValue;
60 
64  private void Awake()
65  {
66  if (TargetObject == null)
67  {
68  TargetObject = this.gameObject;
69  }
70  mStartValue = GetPosition();
71  }
72 
76  public void StartRunning()
77  {
78  if (TargetObject == null)
79  {
80  TargetObject = this.gameObject;
81  }
82 
83  mStartValue = GetPosition();
84  mLerpTimeCounter = 0;
85  IsRunning = true;
86  }
87 
91  public void ResetTransform()
92  {
93  if (ToLocalTransform)
94  {
95  TargetObject.transform.localPosition = mStartValue;
96  }
97  else
98  {
99  TargetObject.transform.position = mStartValue;
100  }
101  IsRunning = false;
102  mLerpTimeCounter = 0;
103  }
104 
108  public void Reverse()
109  {
110  TargetValue = mStartValue;
111  mStartValue = TargetValue;
112  mLerpTimeCounter = 0;
113  IsRunning = true;
114  }
115 
119  public void StopRunning()
120  {
121  IsRunning = false;
122  }
123 
128  private Vector3 GetPosition()
129  {
130  return ToLocalTransform ? TargetObject.transform.localPosition : TargetObject.transform.position;
131  }
132 
139  private Vector3 GetNewPosition(Vector3 currentPosition, float percent)
140  {
141  Vector3 newPosition = Vector3.zero;
142  switch (LerpType)
143  {
144  case LerpTypes.Linear:
145  newPosition = Vector3.Lerp(mStartValue, TargetValue, percent);
146  break;
147  case LerpTypes.EaseIn:
148  newPosition = Vector3.Lerp(mStartValue, TargetValue, QuadEaseIn(0, 1, percent));
149  break;
150  case LerpTypes.EaseOut:
151  newPosition = Vector3.Lerp(mStartValue, TargetValue, QuadEaseOut(0, 1, percent));
152  break;
153  case LerpTypes.EaseInOut:
154  newPosition = Vector3.Lerp(mStartValue, TargetValue, QuadEaseInOut(0, 1, percent));
155  break;
156  case LerpTypes.Free:
157  newPosition = Vector3.Lerp(currentPosition, TargetValue, percent);
158  break;
159  default:
160  break;
161  }
162 
163  return newPosition;
164  }
165 
166  // ease curves
167  public static float QuadEaseIn(float s, float e, float v)
168  {
169  return e * (v /= 1) * v + s;
170  }
171 
172  public static float QuadEaseOut(float s, float e, float v)
173  {
174  return -e * (v /= 1) * (v - 2) + s;
175  }
176 
177  public static float QuadEaseInOut(float s, float e, float v)
178  {
179  if ((v /= 0.5f) < 1)
180  return e / 2 * v * v + s;
181 
182  return -e / 2 * ((--v) * (v - 2) - 1) + s;
183  }
184 
188  private void Update()
189  {
190  if (IsRunning && LerpType != LerpTypes.Free)
191  {
192 
193  mLerpTimeCounter += Time.deltaTime;
194  float percent = mLerpTimeCounter / LerpTime;
195 
196  if (ToLocalTransform)
197  {
198  TargetObject.transform.localPosition = GetNewPosition(TargetObject.transform.localPosition, percent);
199  }
200  else
201  {
202  TargetObject.transform.position = GetNewPosition(TargetObject.transform.position, percent);
203  }
204 
205  if (percent >= 1)
206  {
207  IsRunning = false;
208  OnComplete.Invoke();
209  }
210  }
211  else if (LerpType == LerpTypes.Free)
212  {
213  bool wasRunning = IsRunning;
214  if (ToLocalTransform)
215  {
216  TargetObject.transform.localPosition = GetNewPosition(TargetObject.transform.localPosition, LerpTime * Time.deltaTime);
217  IsRunning = TargetObject.transform.localPosition != TargetValue;
218  }
219  else
220  {
221  TargetObject.transform.position = GetNewPosition(TargetObject.transform.position, LerpTime * Time.deltaTime);
222  IsRunning = TargetObject.transform.localPosition != TargetValue;
223  }
224 
225  if (IsRunning != wasRunning && !IsRunning)
226  {
227  OnComplete.Invoke();
228  }
229  }
230  }
231  }
232 }
void Reverse()
Run the animation backwards for a ping-pong effect
A position animation component that supports easing and local position The animation can be triggered...
LerpTypes
ease types Linear: steady progress EaseIn: ramp up in speed EaseOut: ramp down in speed EaseInOut: ra...
static float QuadEaseOut(float s, float e, float v)
static float QuadEaseInOut(float s, float e, float v)
static float QuadEaseIn(float s, float e, float v)
void ResetTransform()
Reset the position back to the cached starting position