Skip to content

Commit 7b247e8

Browse files
Support lerping with unscaled time to enable pause menus
This change enables editor input, tagalongs, and the cursor to work while the time scale is set to 0. Setting the time scale to 0 is a common practice to implement game "pause" behavior as is done in the Unity sample project. With this change, tagalong menus will still function even when the game is paused.
1 parent 11f0e24 commit 7b247e8

File tree

8 files changed

+143
-77
lines changed

8 files changed

+143
-77
lines changed

Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public enum CursorStateEnum
2323
/// <summary>
2424
/// Not IsHandVisible
2525
/// </summary>
26-
Observe,
26+
Observe,
2727
/// <summary>
2828
/// Not IsHandVisible AND not IsInputSourceDown AND TargetedObject exists
2929
/// </summary>
30-
ObserveHover,
30+
ObserveHover,
3131
/// <summary>
3232
/// IsHandVisible AND not IsInputSourceDown AND TargetedObject is NULL
3333
/// </summary>
@@ -73,10 +73,13 @@ public enum CursorStateEnum
7373
[Tooltip("The distance from the hit surface to place the cursor")]
7474
public float SurfaceCursorDistance = 0.02f;
7575

76+
[Header("Motion")]
77+
[Tooltip("When lerping, use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
78+
public bool UseUnscaledTime = true;
79+
7680
/// <summary>
7781
/// Blend value for surface normal to user facing lerp
7882
/// </summary>
79-
[Header("Motion")]
8083
public float PositionLerpTime = 0.01f;
8184

8285
/// <summary>
@@ -105,7 +108,7 @@ public Vector3 Position
105108
{
106109
get { return transform.position; }
107110
}
108-
111+
109112
public Quaternion Rotation
110113
{
111114
get { return transform.rotation; }
@@ -153,7 +156,7 @@ public bool IsVisible
153156
}
154157
}
155158

156-
#region MonoBehaviour Functions
159+
#region MonoBehaviour Functions
157160

158161
private void Awake()
159162
{
@@ -177,7 +180,7 @@ private void Update()
177180
/// <summary>
178181
/// Override for enable functions
179182
/// </summary>
180-
protected virtual void OnEnable(){}
183+
protected virtual void OnEnable() { }
181184

182185
/// <summary>
183186
/// Override for disable functions
@@ -194,7 +197,7 @@ private void OnDestroy()
194197
UnregisterManagers();
195198
}
196199

197-
#endregion
200+
#endregion
198201

199202
/// <summary>
200203
/// Register to events from the managers the cursor needs.
@@ -302,17 +305,21 @@ protected virtual void UpdateCursorTransform()
302305
}
303306
}
304307

308+
var deltaTime = UseUnscaledTime
309+
? Time.unscaledDeltaTime
310+
: Time.deltaTime;
311+
305312
// Use the lerp times to blend the position to the target position
306-
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime / PositionLerpTime);
307-
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime / ScaleLerpTime);
308-
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime / RotationLerpTime);
313+
transform.position = Vector3.Lerp(transform.position, targetPosition, deltaTime / PositionLerpTime);
314+
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, deltaTime / ScaleLerpTime);
315+
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, deltaTime / RotationLerpTime);
309316
}
310317

311318
/// <summary>
312319
/// Updates the visual representation of the cursor.
313320
/// </summary>
314321
public void SetVisiblity(bool visible)
315-
{
322+
{
316323
if (PrimaryCursorVisual != null)
317324
{
318325
PrimaryCursorVisual.gameObject.SetActive(visible);
@@ -414,7 +421,7 @@ public virtual CursorStateEnum CheckCursorState()
414421
{
415422
return CursorStateEnum.Select;
416423
}
417-
else if(cursorState == CursorStateEnum.Select)
424+
else if (cursorState == CursorStateEnum.Select)
418425
{
419426
return CursorStateEnum.Release;
420427
}

Assets/HoloToolkit/Input/Scripts/InputSources/EditorHandsInput.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public EditorHandData(IInputSource inputSource, uint handId)
3939
public bool IsFingerDownPending;
4040
public bool FingerStateChanged;
4141
public float FingerStateUpdateTimer;
42-
public float FingerDownStartTime;
42+
public float FingerDownStartTime;
4343
public readonly InputSourceEventArgs InputSourceArgs;
4444
}
4545

@@ -56,15 +56,15 @@ public EditorHandData(IInputSource inputSource, uint handId)
5656
/// </summary>
5757
private const float FingerPressDelay = 0.07f;
5858

59-
/// <summary>
60-
/// The maximum interval between button down and button up that will result in a clicked event.
61-
/// </summary>
62-
private const float MaxClickDuration = 0.5f;
59+
/// <summary>
60+
/// The maximum interval between button down and button up that will result in a clicked event.
61+
/// </summary>
62+
private const float MaxClickDuration = 0.5f;
6363

64-
/// <summary>
65-
/// Number of fake hands supported in the editor.
66-
/// </summary>
67-
private const int EditorHandsCount = 2;
64+
/// <summary>
65+
/// Number of fake hands supported in the editor.
66+
/// </summary>
67+
private const int EditorHandsCount = 2;
6868

6969
/// <summary>
7070
/// Array containing the hands data for the two fake hands
@@ -205,19 +205,33 @@ private void Update()
205205
/// </summary>
206206
private void UpdateHandData()
207207
{
208+
float time;
209+
float deltaTime;
210+
211+
if (manualHandControl.UseUnscaledTime)
212+
{
213+
time = Time.unscaledTime;
214+
deltaTime = Time.unscaledDeltaTime;
215+
}
216+
else
217+
{
218+
time = Time.time;
219+
deltaTime = Time.deltaTime;
220+
}
221+
208222
if (manualHandControl.LeftHandInView)
209223
{
210224
GetOrAddHandData(0);
211225
currentHands.Add(0);
212226

213-
UpdateHandState(manualHandControl.LeftHandSourceState, editorHandsData[0]);
227+
UpdateHandState(manualHandControl.LeftHandSourceState, editorHandsData[0], deltaTime, time);
214228
}
215229

216230
if (manualHandControl.RightHandInView)
217231
{
218232
GetOrAddHandData(1);
219233
currentHands.Add(1);
220-
UpdateHandState(manualHandControl.RightHandSourceState, editorHandsData[1]);
234+
UpdateHandState(manualHandControl.RightHandSourceState, editorHandsData[1], deltaTime, time);
221235
}
222236
}
223237

@@ -245,7 +259,7 @@ private EditorHandData GetOrAddHandData(uint sourceId)
245259
/// </summary>
246260
/// <param name="handSource">Hand source to use to update the position.</param>
247261
/// <param name="editorHandData">EditorHandData structure to update.</param>
248-
private void UpdateHandState(DebugInteractionSourceState handSource, EditorHandData editorHandData)
262+
private void UpdateHandState(DebugInteractionSourceState handSource, EditorHandData editorHandData, float deltaTime, float time)
249263
{
250264
// Update hand position
251265
Vector3 handPosition;
@@ -266,26 +280,26 @@ private void UpdateHandState(DebugInteractionSourceState handSource, EditorHandD
266280
editorHandData.FingerStateChanged = false;
267281
if (editorHandData.FingerStateUpdateTimer > 0)
268282
{
269-
editorHandData.FingerStateUpdateTimer -= Time.deltaTime;
283+
editorHandData.FingerStateUpdateTimer -= deltaTime;
270284
if (editorHandData.FingerStateUpdateTimer <= 0)
271285
{
272286
editorHandData.IsFingerDown = editorHandData.IsFingerDownPending;
273287
editorHandData.FingerStateChanged = true;
274-
if (editorHandData.IsFingerDown)
275-
{
276-
editorHandData.FingerDownStartTime = Time.time;
277-
}
288+
if (editorHandData.IsFingerDown)
289+
{
290+
editorHandData.FingerDownStartTime = time;
291+
}
278292
}
279293
}
280294

281-
SendHandStateEvents(editorHandData);
295+
SendHandStateEvents(editorHandData, time);
282296
}
283297

284298
/// <summary>
285299
/// Sends the events for hand state changes.
286300
/// </summary>
287301
/// <param name="editorHandData">Hand data for which events should be sent.</param>
288-
private void SendHandStateEvents(EditorHandData editorHandData)
302+
private void SendHandStateEvents(EditorHandData editorHandData, float time)
289303
{
290304
// Hand moved event
291305
if (editorHandData.HandDelta.sqrMagnitude > 0)
@@ -305,12 +319,12 @@ private void SendHandStateEvents(EditorHandData editorHandData)
305319
RaiseSourceUpEvent(editorHandData.InputSourceArgs);
306320

307321
// Also send click event when using this hands replacement input
308-
if (Time.time - editorHandData.FingerDownStartTime < MaxClickDuration)
309-
{
322+
if (time - editorHandData.FingerDownStartTime < MaxClickDuration)
323+
{
310324
// We currently only support single taps in editor
311325
SourceClickEventArgs args = new SourceClickEventArgs(this, editorHandData.HandId, 1);
312-
RaiseSourceClickedEvent(args);
313-
}
326+
RaiseSourceClickedEvent(args);
327+
}
314328
}
315329
}
316330
}

Assets/HoloToolkit/Input/Scripts/InputSources/RawInteractionSourcesInput.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public SourceData(IInputSource inputSource, uint sourceId)
5252
/// </summary>
5353
private const float SourcePressDelay = 0.07f;
5454

55+
[Tooltip("Use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
56+
public bool UseUnscaledTime = true;
57+
5558
/// <summary>
5659
/// Dictionary linking each source ID to its data.
5760
/// </summary>
@@ -106,7 +109,7 @@ public override bool TryGetOrientation(uint sourceId, out Quaternion orientation
106109
orientation = Quaternion.identity;
107110
return false;
108111
}
109-
112+
110113
private void Update()
111114
{
112115
newSources.Clear();
@@ -180,7 +183,11 @@ private void UpdateSourceState(InteractionSourceState interactionSource, SourceD
180183
sourceData.SourceStateChanged = false;
181184
if (sourceData.SourceStateUpdateTimer >= 0)
182185
{
183-
sourceData.SourceStateUpdateTimer -= Time.deltaTime;
186+
var deltaTime = UseUnscaledTime
187+
? Time.unscaledDeltaTime
188+
: Time.deltaTime;
189+
190+
sourceData.SourceStateUpdateTimer -= deltaTime;
184191
if (sourceData.SourceStateUpdateTimer < 0)
185192
{
186193
sourceData.IsSourceDown = sourceData.IsSourceDownPending;

Assets/HoloToolkit/Input/Scripts/Utilities/AxisController.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public enum AxisDestination
5555

5656
public float SensitivityScale = 3.0f;
5757

58+
[Tooltip("Use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
59+
public bool UseUnscaledTime = true;
60+
5861
public AxisType axisType = AxisType.Mouse;
5962
public ButtonController.ButtonType buttonType = ButtonController.ButtonType.None;
6063

@@ -82,19 +85,19 @@ public enum AxisDestination
8285

8386
public bool AxisTypeIsKeyboard
8487
{
85-
get{ return AxisType.KeyboardArrows <= axisType && axisType <= AxisType.KeyBoardHomeEndPgUpPgDown; }
88+
get { return AxisType.KeyboardArrows <= axisType && axisType <= AxisType.KeyBoardHomeEndPgUpPgDown; }
8689
}
8790
public bool AxisTypeIsInputManagerAxis
8891
{
89-
get{ return axisType == AxisType.InputManagerAxis; }
92+
get { return axisType == AxisType.InputManagerAxis; }
9093
}
9194
public bool AxisTypeIsMouse
9295
{
93-
get{ return axisType == AxisType.Mouse; }
96+
get { return axisType == AxisType.Mouse; }
9497
}
9598
public bool AxisTypeIsMouseScroll
9699
{
97-
get{ return axisType == AxisType.MouseScroll; }
100+
get { return axisType == AxisType.MouseScroll; }
98101
}
99102

100103
public void EnableAndCheck(bool value)
@@ -136,9 +139,9 @@ private void Awake()
136139
UnityEngine.Cursor.lockState = CursorLockMode.None;
137140
UnityEngine.Cursor.visible = true;
138141
#endif
139-
}
142+
}
140143

141-
private static float InputCurve(float x)
144+
private static float InputCurve(float x)
142145
{
143146
// smoothing input curve, converts from [-1,1] to [-2,2]
144147
return (Mathf.Sign(x) * (1.0f - Mathf.Cos(.5f * Mathf.PI * Mathf.Clamp(x, -1.0f, 1.0f))));
@@ -379,52 +382,56 @@ private Vector3 InputManagerAxisLookTick()
379382

380383
private Vector3 KeyboardLookTick()
381384
{
385+
var deltaTime = UseUnscaledTime
386+
? Time.unscaledDeltaTime
387+
: Time.deltaTime;
388+
382389
Vector3 rot = Vector3.zero;
383390
if (axisType == AxisType.KeyboardArrows)
384391
{
385-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftArrow, KeyCode.RightArrow));
386-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.DownArrow, KeyCode.UpArrow));
392+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftArrow, KeyCode.RightArrow));
393+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.DownArrow, KeyCode.UpArrow));
387394
}
388395
else if (axisType == AxisType.KeyboardWASD)
389396
{
390-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.A, KeyCode.D));
391-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.S, KeyCode.W));
397+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.A, KeyCode.D));
398+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.S, KeyCode.W));
392399
}
393400
else if (axisType == AxisType.KeyboardQE)
394401
{
395-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Q, KeyCode.E));
402+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Q, KeyCode.E));
396403
}
397404
else if (axisType == AxisType.KeyboardIJKL)
398405
{
399-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.J, KeyCode.L));
400-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.K, KeyCode.I));
406+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.J, KeyCode.L));
407+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.K, KeyCode.I));
401408
}
402409
else if (axisType == AxisType.KeyboardUO)
403410
{
404-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.U, KeyCode.O));
411+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.U, KeyCode.O));
405412
}
406413
else if (axisType == AxisType.Keyboard8426)
407414
{
408-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad4, KeyCode.Keypad6));
409-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad2, KeyCode.Keypad8));
415+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad4, KeyCode.Keypad6));
416+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad2, KeyCode.Keypad8));
410417
}
411418
else if (axisType == AxisType.Keyboard7193)
412419
{
413-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad1, KeyCode.Keypad7));
414-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad3, KeyCode.Keypad9));
420+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad1, KeyCode.Keypad7));
421+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad3, KeyCode.Keypad9));
415422
}
416423
else if (axisType == AxisType.KeyboardPeriodComma)
417424
{
418-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Comma, KeyCode.Period));
425+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Comma, KeyCode.Period));
419426
}
420427
else if (axisType == AxisType.KeyboardBrackets)
421428
{
422-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftBracket, KeyCode.RightBracket));
429+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftBracket, KeyCode.RightBracket));
423430
}
424431
else if (axisType == AxisType.KeyBoardHomeEndPgUpPgDown)
425432
{
426-
rot.x += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.End, KeyCode.Home));
427-
rot.y += InputCurve(Time.deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.PageDown, KeyCode.PageUp));
433+
rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.End, KeyCode.Home));
434+
rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.PageDown, KeyCode.PageUp));
428435
}
429436
return rot;
430437
}

0 commit comments

Comments
 (0)