Problem using Oculus Touch Joystick Input to move XRRig
Hey all,
I'm working on a little VR project of mine, I've been using Unity's XR Interaction Toolkit to do much of this and its such an amazing tool over the Oculus Integration library. I've implemented the stock teleportation, and snap turn scripts and they work great. However I realized there is no free walking script using the joystick yet and decided to go ahead and create my own. I have a decent programming background with Python, but fairly new to C#. What I've achieved with my code so far is being able to use the Joystick to get Vector2 inputs XY and move the XR Rig based on that.
However this moves the XR Rig based on its local coordinates which is fine but not the best solution for locomotion. The feature I'm trying to achieve is have the XR Rig's forward (or the Z axis) be where the HMDs local forward coordinates are so that forward movement from pushing up on the joystick moves the XR Rig in the direction the HMD is facing, then left, right, and back being relative to this forward. I cannot for the life of me figure out how to use the Vector2 inputs and the look direction to do so. I've tried a bunch of different attempts to no avail.
It also doesn't help that rotating the XR Rig is out of the question due to it messing up your Guardian placement, so I'm limited to 3DOF. Please forgive me if this is too simple of an issue but I'm stumped!
Also, I feel like this is some simple vector math that I'm being very rusty on, and if you all have suggestions to Unity specific math implementation courses online, throw them at me :). I'd love to stop smashing my head against the wall.
The code listed below exists in the Update method. m_CurrentSpeed is an entry from the inspector for the script, that is greater than 0 if and only if either X or Y Joystick input is greater than a deadzone of 0.75f. XY inputs range from (-0.99 to 0.99) on both axes. I didn't include this for simplicity sake.
if (Math.Abs(m_CurrentSpeed) > 0.0f) //Once Controller has been found, check if speed is > 0
{
var xrRig = system.xrRig; //Access the XR Rig in Hierarchy
if (xrRig != null)
{
//Get Local Position of the Rig
Vector3 xrRigPosition = xrRig.rig.transform.localPosition;
//Get the XY input, create new direction Vector3 to apply to the X-Z Plane of Rig
Vector3 direction = new Vector3(currentState.x, 0, currentState.y);
//Apply this new direction to the XR Rig
xrRig.rig.transform.Translate(direction * Time.deltaTime * Math.Abs(m_CurrentSpeed));
}
m_CurrentSpeed = 0.0f; //Set speed to zero and check for input again.
}
Answer by binoman · Feb 01, 2021 at 09:21 AM
You probably moved on, but I think responding may help others who might come across this page. You could use DeviceBasedContinuousMoveProvider component, add it into the XRRig, with combination of CharacterController component. Check out the WorldInteractionDemo scene in the XR-Interaction-Toolkit-Examples repository, and see how it is solved there. Here: https://github.com/Unity-Technologies/XR-Interaction-Toolkit-Examples/tree/master/VR.