From 2323c7b0fb1e53ffac2a1db6b1ec0cc0fddbc620 Mon Sep 17 00:00:00 2001 From: Harvey Ball Date: Fri, 23 Jun 2017 09:10:59 +0100 Subject: [PATCH] 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. --- .../VRTK/SDK/Simulator/SDK_SimController.cs | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Assets/VRTK/SDK/Simulator/SDK_SimController.cs b/Assets/VRTK/SDK/Simulator/SDK_SimController.cs index d8e33148b..57e028a9d 100644 --- a/Assets/VRTK/SDK/Simulator/SDK_SimController.cs +++ b/Assets/VRTK/SDK/Simulator/SDK_SimController.cs @@ -127,6 +127,7 @@ public override uint GetControllerIndex(GameObject controller) /// The GameObject of the controller public override GameObject GetControllerByIndex(uint index, bool actual = false) { + SetupPlayer(); VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; switch (index) { @@ -146,7 +147,7 @@ public override GameObject GetControllerByIndex(uint index, bool actual = false) /// A Transform containing the origin of the controller. public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference) { - return controllerReference.actual.transform; + return (controllerReference != null && controllerReference.actual != null ? controllerReference.actual.transform : null); } /// @@ -326,6 +327,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers() /// A Vector3 containing the current velocity of the tracked object. public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference) { + SetupPlayer(); uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); switch (index) { @@ -345,6 +347,7 @@ public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference /// A Vector3 containing the current angular velocity of the tracked object. public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference) { + SetupPlayer(); uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); switch (index) { @@ -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(); - leftController = simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent(); + GameObject simPlayer = SDK_InputSimulator.FindInScene(); + if (simPlayer != null) + { + rightController = (rightController == null ? simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent() : rightController); + leftController = (leftController == null ? simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent() : leftController); + } } } @@ -521,6 +532,7 @@ protected virtual bool GetControllerButtonState(uint index, string keyMapping, B /// Returns true if the button is being pressed. protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCode button) { + SetupPlayer(); if (index >= uint.MaxValue) { return false; @@ -528,14 +540,14 @@ protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCod 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; }