Search Unity

Question Problem with isolating pitch, yaw, roll of GameObject (XR Controller)

Discussion in 'Scripting' started by JacksonHutchings, Feb 25, 2024.

  1. JacksonHutchings

    JacksonHutchings

    Joined:
    May 24, 2019
    Posts:
    1
    I need to be pointed in the right direction for how to solve this problem.

    I am implementing a game mechanic where I can make an XR controller behave like a traditional flight joystick. I would like the controller rotation input data to be processed in a way where the pitch, yaw, and roll can be separated from it's rotation. To be clear, I am NOT looking for the x, y, z rotation. I am looking to retrieve a value for how much the controller independently yawed, pitched, and rolled from one frame to the next. What I want seems very similar to input-axis of joystick and mouse, I just don't know how to effectively convert a rotation to that.

    Sorry for the clunky word-salad, this has been very difficult for me to try and understand.



    So far I can't make this work using Euler angles as the x, y, z components of the rotation all have an effect on one another. When I have looked at Quaternions and built-in methods, I get the feeling that I can find a hacky solution but so far I see no solid pathway.

    Thank you all!

    Edit:
    I'd like to add, for clarification, that no matter the orientation of the controller, I would like a "yaw" of the controller to be able to be read as a yaw/y-axis rotation regardless of whether the controller is also "rolled" about the z-axis.
     
    Last edited: Feb 25, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,825
    Getting the individual axis is not the issue I suppose?

    Which means you have either 3 absolute or delta rotation angles for each axis. I would start with creating a Quaternion from the delta angles, and if you have absolute angles, you'd first have to determine the delta from the current rotation.

    Then add the delta rotation to the existing rotation but as quaternion. In quaternion logic this means multiplication: newRotation = currentRotation * deltaRotation

    Of course with xyz euler angles as the input things can break down, like the sign of one axis flipping. But you'll see that when it happens and you can then apply corrective measures or google for that particular issue.


    Also worth looking into the Input System package. For one it has many samples and probably ships with one that works with mobile gyro, and it may provide some convenience features on top.