AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
RotateToValue.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 {
14  public class RotateToValue : MonoBehaviour
15  {
24  public enum LerpTypes { Linear, EaseIn, EaseOut, EaseInOut, Free }
25 
26  [Tooltip("The object to animate")]
27  public GameObject TargetObject;
28 
29  [Tooltip("The rotation value to animate to")]
30  public Quaternion TargetValue;
31 
32  [Tooltip("The type of ease to apply to the tween")]
34 
35  [Tooltip("Duration of the animation in seconds")]
36  public float LerpTime = 1f;
37 
38  [Tooltip("auto start? or status")]
39  public bool IsRunning = false;
40 
41  [Tooltip("Use the localRotation instead of world rotation")]
42  public bool ToLocalTransform = false;
43 
44  [Tooltip("animation complete!")]
45  public UnityEvent OnComplete;
46 
47  // animation ticker
48  private float mLerpTimeCounter;
49 
50  // starting/current rotation
51  private Quaternion mStartValue;
52 
57  private void Awake()
58  {
59  if (TargetObject == null)
60  {
61  TargetObject = this.gameObject;
62  }
63  mStartValue = GetRotation();
64  }
65 
69  public void StartRunning()
70  {
71  if (TargetObject == null)
72  {
73  TargetObject = this.gameObject;
74  }
75 
76  mStartValue = GetRotation();
77  mLerpTimeCounter = 0;
78  IsRunning = true;
79  }
80 
84  public void ResetTransform()
85  {
86  if (ToLocalTransform)
87  {
88  this.transform.localRotation = mStartValue;
89  }
90  else
91  {
92  this.transform.rotation = mStartValue;
93  }
94  IsRunning = false;
95  mLerpTimeCounter = 0;
96  }
97 
101  public void Reverse()
102  {
103  TargetValue = mStartValue;
104  mStartValue = TargetValue;
105  mLerpTimeCounter = 0;
106  IsRunning = true;
107  }
108 
112  public void StopRunning()
113  {
114  IsRunning = false;
115  }
116 
117  // get the current rotation
118  private Quaternion GetRotation()
119  {
120  return ToLocalTransform ? TargetObject.transform.localRotation : TargetObject.transform.rotation;
121  }
122 
129  private Quaternion GetNewRotation(Quaternion currentRotation, float percent)
130  {
131  Quaternion newPosition = Quaternion.identity;
132  switch (LerpType)
133  {
134  case LerpTypes.Linear:
135  newPosition = Quaternion.Lerp(mStartValue, TargetValue, percent);
136  break;
137  case LerpTypes.EaseIn:
138  newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseIn(0, 1, percent));
139  break;
140  case LerpTypes.EaseOut:
141  newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseOut(0, 1, percent));
142  break;
143  case LerpTypes.EaseInOut:
144  newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseInOut(0, 1, percent));
145  break;
146  case LerpTypes.Free:
147  newPosition = Quaternion.Lerp(currentRotation, TargetValue, percent);
148  break;
149  default:
150  break;
151  }
152 
153  return newPosition;
154  }
155 
156  // ease curves
157  public static float QuadEaseIn(float s, float e, float v)
158  {
159  return e * (v /= 1) * v + s;
160  }
161 
162  public static float QuadEaseOut(float s, float e, float v)
163  {
164  return -e * (v /= 1) * (v - 2) + s;
165  }
166 
167  public static float QuadEaseInOut(float s, float e, float v)
168  {
169  if ((v /= 0.5f) < 1)
170  return e / 2 * v * v + s;
171 
172  return -e / 2 * ((--v) * (v - 2) - 1) + s;
173  }
174 
178  private void Update()
179  {
180  // manual animation, only runs when auto started or StartRunning() is called
181  if (IsRunning && LerpType != LerpTypes.Free)
182  {
183  // get the time
184  mLerpTimeCounter += Time.deltaTime;
185  float percent = mLerpTimeCounter / LerpTime;
186 
187  // set the rotation
188  if (ToLocalTransform)
189  {
190  this.transform.localRotation = GetNewRotation(this.transform.localRotation, percent);
191  }
192  else
193  {
194  this.transform.rotation = GetNewRotation(this.transform.rotation, percent);
195  }
196 
197  // fire the event if complete
198  if (percent >= 1)
199  {
200  IsRunning = false;
201  OnComplete.Invoke();
202  }
203  }
204  else if (LerpType == LerpTypes.Free) // is always running, just waiting for the TargetValue to change
205  {
206  bool wasRunning = IsRunning;
207 
208  // set the rotation
209  if (ToLocalTransform)
210  {
211  this.transform.localRotation = GetNewRotation(this.transform.localRotation, LerpTime * Time.deltaTime);
212  IsRunning = this.transform.localRotation != TargetValue;
213  }
214  else
215  {
216  this.transform.rotation = GetNewRotation(this.transform.rotation, LerpTime * Time.deltaTime);
217  IsRunning = this.transform.rotation != TargetValue;
218  }
219 
220  // fire the event if complete
221  if (IsRunning != wasRunning && !IsRunning)
222  {
223  OnComplete.Invoke();
224  }
225  }
226  }
227  }
228 }
static float QuadEaseIn(float s, float e, float v)
void ResetTransform()
Set the rotation to the cached starting value
void StartRunning()
Start the rotation animation
static float QuadEaseOut(float s, float e, float v)
LerpTypes
ease types Linear: steady progress EaseIn: ramp up in speed EaseOut: ramp down in speed EaseInOut: ra...
void Reverse()
reverse the rotation - go back
animates the rotation of an object with eases
static float QuadEaseInOut(float s, float e, float v)