Skip to content

Commit 59e115f

Browse files
authored
ControllerManager Fix For Teleport (#13)
* Controller Manager now uses new Input Helpers, and has both Activations and Deactivation buttons as properties. (cherry picked from commit 4172254f01b66e6dd98f9b041c2f228536229287) * Execution order is now constant. * Updated enum values to match new settings * PR Feedback * Typo Fix * Revert Unwanted upgrade change * Proper usages for sample * Fix Button Values for Grab
1 parent 221a117 commit 59e115f

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

VR/Assets/Prefabs/XRRig_Demo.prefab

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ MonoBehaviour:
582582
m_EnableInputActions: 1
583583
m_ControllerNode: 5
584584
m_SelectUsage: 2
585-
m_ActivateUsage: 1
585+
m_ActivateUsage: 2
586586
m_UIPressUsage: 2
587587
m_AxisToPressThreshold: 0.1
588588
m_ModelPrefab: {fileID: 0}
@@ -1344,6 +1344,8 @@ MonoBehaviour:
13441344
m_Script: {fileID: 11500000, guid: ba706e5cce610884ab194e68db6a910e, type: 3}
13451345
m_Name:
13461346
m_EditorClassIdentifier:
1347+
m_ActivationButtons: 0b00000006000000
1348+
m_DeactivationButtons: 03000000
13471349
m_LeftBaseController: {fileID: 1717954560873690078}
13481350
m_LeftTeleportController: {fileID: 1717954561233508939}
13491351
m_RightBaseController: {fileID: 1717954560309945980}

VR/Assets/Scripts/ControllerManager.cs

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@
44
using UnityEngine.XR;
55
using UnityEngine.XR.Interaction.Toolkit;
66

7+
[DefaultExecutionOrder(kControllerManagerUpdateOrder)]
78
public class ControllerManager : MonoBehaviour
89
{
10+
// Slightly after the default, so that any actions such as release or grab can be processed *before* we switch controllers.
11+
public const int kControllerManagerUpdateOrder = 10;
12+
913
InputDevice m_RightController;
1014
InputDevice m_LeftController;
1115

12-
bool m_LeftTouchPadClicked;
13-
bool m_LeftPrimaryButtonClicked;
14-
bool m_RightTouchPadClicked;
15-
bool m_RightPrimaryButtonClicked;
16+
[SerializeField]
17+
[Tooltip("The buttons on the controller that will trigger a transition to the Teleport Controller.")]
18+
List<InputHelpers.Button> m_ActivationButtons = new List<InputHelpers.Button>();
19+
/// <summary>
20+
/// The buttons on the controller that will trigger a transition to the Teleport Controller.
21+
/// </summary>
22+
public List<InputHelpers.Button> activationButtons { get { return m_ActivationButtons; } set { m_ActivationButtons = value; } }
23+
24+
25+
[SerializeField]
26+
[Tooltip("The buttons on the controller that will force a deactivation of the teleport option.")]
27+
List<InputHelpers.Button> m_DeactivationButtons = new List<InputHelpers.Button>();
28+
/// <summary>
29+
/// The buttons on the controller that will trigger a transition to the Teleport Controller.
30+
/// </summary>
31+
public List<InputHelpers.Button> deactivationButtons { get { return m_DeactivationButtons; } set { m_DeactivationButtons = value; } }
1632

1733
[SerializeField]
1834
[Tooltip("The Game Object which represents the left hand for normal interaction purposes.")]
@@ -30,7 +46,6 @@ public class ControllerManager : MonoBehaviour
3046
/// </summary>
3147
public GameObject leftTeleportController { get { return m_LeftTeleportController; } set { m_LeftTeleportController = value; } }
3248

33-
3449
[SerializeField]
3550
[Tooltip("The Game Object which represents the right hand for normal interaction purposes.")]
3651
GameObject m_RightBaseController;
@@ -39,7 +54,6 @@ public class ControllerManager : MonoBehaviour
3954
/// </summary>
4055
public GameObject rightBaseController { get { return m_RightBaseController; } set { m_RightBaseController = value; } }
4156

42-
4357
[SerializeField]
4458
[Tooltip("The Game Object which represents the right hand when teleporting.")]
4559
GameObject m_RightTeleportController;
@@ -48,6 +62,9 @@ public class ControllerManager : MonoBehaviour
4862
/// </summary>
4963
public GameObject rightTeleportController { get { return m_RightTeleportController; } set { m_RightTeleportController = value; } }
5064

65+
bool m_LeftTeleportDeactivated = false;
66+
bool m_RightTeleportDeactivated = false;
67+
5168
/// <summary>
5269
/// A simple state machine which manages the three pieces of content that are used to represent
5370
/// A controller state within the XR Interaction Toolkit
@@ -160,7 +177,7 @@ struct ControllerState
160177
/// <summary>
161178
/// Sets up the controller
162179
/// </summary>
163-
public void Initalize()
180+
public void Initialize()
164181
{
165182
m_State = ControllerStates.MAX;
166183
m_Interactors = new InteractorController[(int)ControllerStates.MAX];
@@ -222,8 +239,11 @@ public void SetState(ControllerStates nextState)
222239

223240
void OnEnable()
224241
{
225-
m_RightControllerState.Initalize();
226-
m_LeftControllerState.Initalize();
242+
m_LeftTeleportDeactivated = false;
243+
m_RightTeleportDeactivated = false;
244+
245+
m_RightControllerState.Initialize();
246+
m_LeftControllerState.Initialize();
227247

228248
m_RightControllerState.SetGameObject(ControllerStates.Select, m_RightBaseController);
229249
m_RightControllerState.SetGameObject(ControllerStates.Teleport, m_RightTeleportController);
@@ -255,7 +275,6 @@ void RegisterDevices(InputDevice connectedDevice)
255275
#else
256276
if (connectedDevice.role == InputDeviceRole.LeftHanded)
257277
#endif
258-
259278
{
260279
m_LeftController = connectedDevice;
261280
m_LeftControllerState.ClearAll();
@@ -277,35 +296,68 @@ void RegisterDevices(InputDevice connectedDevice)
277296
void Update()
278297
{
279298
if (m_LeftController.isValid)
280-
{
281-
m_LeftController.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out m_LeftTouchPadClicked);
282-
m_LeftController.TryGetFeatureValue(CommonUsages.primaryButton, out m_LeftPrimaryButtonClicked);
299+
{
300+
bool activated = false;
301+
for(int i = 0; i < m_ActivationButtons.Count; i++)
302+
{
303+
m_LeftController.IsPressed(m_ActivationButtons[i], out bool value);
304+
activated |= value;
305+
}
283306

284-
// if we're clicking the touch pad, or the primary button, swap to the teleport state.
285-
if (m_LeftTouchPadClicked || m_LeftPrimaryButtonClicked)
307+
bool deactivated = false;
308+
for (int i = 0; i < m_DeactivationButtons.Count; i++)
309+
{
310+
m_LeftController.IsPressed(m_DeactivationButtons[i], out bool value);
311+
m_LeftTeleportDeactivated |= value;
312+
}
313+
314+
if (deactivated)
315+
m_LeftTeleportDeactivated = true;
316+
317+
// if we're pressing the activation buttons, we transition to Teleport
318+
if (activated && !m_LeftTeleportDeactivated)
286319
{
287320
m_LeftControllerState.SetState(ControllerStates.Teleport);
288321
}
289322
// otherwise we're in normal state.
290323
else
291324
{
292325
m_LeftControllerState.SetState(ControllerStates.Select);
326+
327+
if(!activated)
328+
m_LeftTeleportDeactivated = false;
293329
}
294330
}
295331

296332
if (m_RightController.isValid)
297-
{
298-
m_RightController.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out m_RightTouchPadClicked);
299-
m_RightController.TryGetFeatureValue(CommonUsages.primaryButton, out m_RightPrimaryButtonClicked);
333+
{
334+
bool activated = false;
335+
for (int i = 0; i < m_ActivationButtons.Count; i++)
336+
{
337+
m_RightController.IsPressed(m_ActivationButtons[i], out bool value);
338+
activated |= value;
339+
}
300340

341+
bool deactivated = false;
342+
for (int i = 0; i < m_DeactivationButtons.Count; i++)
343+
{
344+
m_RightController.IsPressed(m_DeactivationButtons[i], out bool value);
345+
deactivated |= value;
346+
}
347+
348+
if (deactivated)
349+
m_RightTeleportDeactivated = true;
301350

302-
if (m_RightTouchPadClicked || m_RightPrimaryButtonClicked)
351+
if (activated && !m_RightTeleportDeactivated)
303352
{
304353
m_RightControllerState.SetState(ControllerStates.Teleport);
305354
}
306355
else
307356
{
308357
m_RightControllerState.SetState(ControllerStates.Select);
358+
359+
if (!activated)
360+
m_RightTeleportDeactivated = false;
309361
}
310362
}
311363
}

0 commit comments

Comments
 (0)