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

XRrig character controller rotation

Discussion in 'AR/VR (XR) Discussion' started by stippy, Mar 28, 2020.

  1. stippy

    stippy

    Joined:
    Mar 1, 2020
    Posts:
    9
    Hi guys,

    TL;DR:
    Position of character controller used for VR while having HMD input + button/stick input to turn seems unreasonably difficult for me - what is the best way to achieve that?

    Long version:
    Setup:
    XRrig (attached characterController + Movement script)
    -----> Child MainCamera

    I move my camera and my character controller according to the HMD, as walking around physically should also move the character ingame. When I now use the SnapTurnProvider, then the turn axis is the XRrig's center, not the characterController's center. How do you guys handle that kind of stuff?

    I move my character controller according to the HMD by:
    Code (CSharp):
    1. head = GetComponent<XRRig>().cameraGameObject;
    2. newCenter.x = head.transform.localPosition.x;
    3. newCenter.z = head.transform.localPosition.z;
    4. characterController.center = newCenter;
    Which makes the characterController's center reposition to the current HMD position. Works great!

    Moving the player by
    Code (CSharp):
    1. characterController.Move(movement * Time.deltaTime);
    also works as intended.

    Now I have a raycast from the characterController downwards to detect the ground standing on and it's slope angle:
    Code (CSharp):
    1. Physics.Raycast(characterController.transform.position + characterController.center, Vector3.down, out groundHit, 5.0f), layerMaskGround))
    That's were my problem begins: I use
    Code (CSharp):
    1. characterController.transform.position + characterController.center
    to find the current characterController's position. That works great and adapts to the HMD movement and the movement via input stick. But I am really struggling with the additional turning of the player by button/stick input. As soon as I turn, the raycast starting position gets messed up, as it seems that the turning axis my rotation is around, seems wrong. I seem to be unable to find a solution for that.

    Currently using:
    Code (CSharp):
    1. var xrRig = system.xrRig;
    2. Quaternion orientation = xrRig.transform.rotation;
    3. Vector3 euler = transform.rotation.eulerAngles;
    4.  
    5. euler.y += position.x * Time.deltaTime * turningSpeed;
    6. xrRig.transform.rotation = Quaternion.Euler(euler);
     
  2. stippy

    stippy

    Joined:
    Mar 1, 2020
    Posts:
    9
    Okay, seems like I found a solution to my problem: To find the current characterController's position to use for the raycast, use:
    Code (CSharp):
    1. position = transform.TransformPoint(newCenter);