Skip to content

Commit

Permalink
fix(SDK): ensure simulator controllers are set up on scene change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thestonefox committed Jun 23, 2017
1 parent 7737a18 commit 2323c7b
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Assets/VRTK/SDK/Simulator/SDK_SimController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public override uint GetControllerIndex(GameObject controller)
/// <returns>The GameObject of the controller</returns>
public override GameObject GetControllerByIndex(uint index, bool actual = false)
{
SetupPlayer();
VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
switch (index)
{
Expand All @@ -146,7 +147,7 @@ public override GameObject GetControllerByIndex(uint index, bool actual = false)
/// <returns>A Transform containing the origin of the controller.</returns>
public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference)
{
return controllerReference.actual.transform;
return (controllerReference != null && controllerReference.actual != null ? controllerReference.actual.transform : null);
}

/// <summary>
Expand Down Expand Up @@ -326,6 +327,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers()
/// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference)
{
SetupPlayer();
uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
switch (index)
{
Expand All @@ -345,6 +347,7 @@ public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference
/// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference)
{
SetupPlayer();
uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
switch (index)
{
Expand Down Expand Up @@ -423,11 +426,19 @@ public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPres

protected virtual void OnEnable()
{
GameObject simPlayer = SDK_InputSimulator.FindInScene();
if (simPlayer != null)
SetupPlayer();
}

protected virtual void SetupPlayer()
{
if (rightController == null || leftController == null)
{
rightController = simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>();
leftController = simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>();
GameObject simPlayer = SDK_InputSimulator.FindInScene();
if (simPlayer != null)
{
rightController = (rightController == null ? simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : rightController);
leftController = (leftController == null ? simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : leftController);
}
}
}

Expand Down Expand Up @@ -521,21 +532,22 @@ protected virtual bool GetControllerButtonState(uint index, string keyMapping, B
/// <returns>Returns true if the button is being pressed.</returns>
protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCode button)
{
SetupPlayer();
if (index >= uint.MaxValue)
{
return false;
}

if (index == 1)
{
if (rightController != null && !rightController.Selected)
if (!rightController.Selected)
{
return false;
}
}
else if (index == 2)
{
if (leftController != null && !leftController.Selected)
if (!leftController.Selected)
{
return false;
}
Expand Down

0 comments on commit 2323c7b

Please sign in to comment.