Search code examples
c#unity-game-enginerotationquaternionseuler-angles

Unity: Inversing rotation around certain axis from quaternions


I have been researching about this topic for the past 3 days, but I seem to not understand how to handle quaternions correctly.

I have a variable pose with a rotation property of the type "quaternion" that results in the euler angles (1, 2, 3). I want to modify this variable pose, so that it would result in the euler angles (-1, 2, 3).

My current attempt looks like this:

initialGameObject.rotation = pose.rot -> results in a rotation of (1, 2, 3)

otherGameObject.rotation = Quaternion.Euler(pose.rot.eulerAngles.x * -1f, pose.rot.eulerAngles.y, pose.rot.eulerAngles.z) -> I want that to result in a rotation of (-1, 2, 3), but it doesn't work

I would be so thankful if somebody could help me with that problem!


Solution

  • What did the trick for me was to store the edited rotation of the first gameobject in a Vector3 variable before assigning it to the second gameobject.

    initialGameObject.rotation = pose.rot
    
    var rot = new Vector3(pose.rot.eulerAngles.x * -1f, pose.rot.eulerAngles.y, pose.rot.eulerAngles.z);
    
    otherGameObject.rotation = Quaternion.Euler(rot);
    

    Hope that helps if others stumble across a similar problem.