Skip to content

Commit 2323c7b

Browse files
committed
fix(SDK): ensure simulator controllers are set up on scene change
There was an issue where the simulator controllers were not being re-setup at the correct time when switching scene. This fix forces the set up of the controllers if they are null when they are supposed to be set.
1 parent 7737a18 commit 2323c7b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Assets/VRTK/SDK/Simulator/SDK_SimController.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public override uint GetControllerIndex(GameObject controller)
127127
/// <returns>The GameObject of the controller</returns>
128128
public override GameObject GetControllerByIndex(uint index, bool actual = false)
129129
{
130+
SetupPlayer();
130131
VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
131132
switch (index)
132133
{
@@ -146,7 +147,7 @@ public override GameObject GetControllerByIndex(uint index, bool actual = false)
146147
/// <returns>A Transform containing the origin of the controller.</returns>
147148
public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference)
148149
{
149-
return controllerReference.actual.transform;
150+
return (controllerReference != null && controllerReference.actual != null ? controllerReference.actual.transform : null);
150151
}
151152

152153
/// <summary>
@@ -326,6 +327,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers()
326327
/// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
327328
public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference)
328329
{
330+
SetupPlayer();
329331
uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
330332
switch (index)
331333
{
@@ -345,6 +347,7 @@ public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference
345347
/// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
346348
public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference)
347349
{
350+
SetupPlayer();
348351
uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
349352
switch (index)
350353
{
@@ -423,11 +426,19 @@ public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPres
423426

424427
protected virtual void OnEnable()
425428
{
426-
GameObject simPlayer = SDK_InputSimulator.FindInScene();
427-
if (simPlayer != null)
429+
SetupPlayer();
430+
}
431+
432+
protected virtual void SetupPlayer()
433+
{
434+
if (rightController == null || leftController == null)
428435
{
429-
rightController = simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>();
430-
leftController = simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>();
436+
GameObject simPlayer = SDK_InputSimulator.FindInScene();
437+
if (simPlayer != null)
438+
{
439+
rightController = (rightController == null ? simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : rightController);
440+
leftController = (leftController == null ? simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : leftController);
441+
}
431442
}
432443
}
433444

@@ -521,21 +532,22 @@ protected virtual bool GetControllerButtonState(uint index, string keyMapping, B
521532
/// <returns>Returns true if the button is being pressed.</returns>
522533
protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCode button)
523534
{
535+
SetupPlayer();
524536
if (index >= uint.MaxValue)
525537
{
526538
return false;
527539
}
528540

529541
if (index == 1)
530542
{
531-
if (rightController != null && !rightController.Selected)
543+
if (!rightController.Selected)
532544
{
533545
return false;
534546
}
535547
}
536548
else if (index == 2)
537549
{
538-
if (leftController != null && !leftController.Selected)
550+
if (!leftController.Selected)
539551
{
540552
return false;
541553
}

0 commit comments

Comments
 (0)