Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem using Oculus Touch Joystick Input to move XRRig

Discussion in 'Editor & General Support' started by abi17124, Mar 21, 2020.

  1. abi17124

    abi17124

    Joined:
    Feb 16, 2020
    Posts:
    6
    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.

    Code (CSharp):
    1.  
    2. if (Math.Abs(m_CurrentSpeed) > 0.0f) //Once Controller has been found, check if speed is greater that 0
    3.             {
    4.                 var xrRig = system.xrRig; //Access the XR Rig in Hierarchy
    5.                 if (xrRig != null)
    6.                 {
    7.                     Vector3 xrRigPosition = xrRig.rig.transform.localPosition; //Get the Local Position of the Rig
    8.  
    9.                     //Get the X and Y input from joystick, and create a new movement direction Vector3 to apply to the X-Z Plane of the XR Rig
    10.                     Vector3 direction = new Vector3(currentState.x, 0, currentState.y);
    11.  
    12.                     //Apply this new direction to the XR Rig
    13.                     xrRig.rig.transform.Translate(direction * Time.deltaTime * Math.Abs(m_CurrentSpeed));
    14.                 }
    15.                 m_CurrentSpeed = 0.0f; //Set speed to zero and check for input again.
    16.             }
    17.  

    Picture below show the Local Axes for the XR Rig.
     

    Attached Files:

    Last edited: Mar 21, 2020
  2. BCFEGAmes

    BCFEGAmes

    Joined:
    Oct 5, 2016
    Posts:
    36