0

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!

THG
  • 1
  • 1
  • 2
  • "but it doesn't work" - why? what error or behavior do you get? what would you expect? Does calling `Quaternion.Euler(-1, 2, 3)` **not** result in an euler angle of `-1, 2, 3`? Keep in mind that there can be multiple euler angles representing the same actual rotation. I personally try my to avoid euler angles, while they might be easier to understand, I find them more difficult to actually use. – JonasH Oct 20 '22 at 15:09

2 Answers2

0

I don't know what is not working on your side, I have made a quick test and that was the result:

GameObject go1 = new GameObject("GO1");
go1.transform.position = Vector3.zero;
go1.transform.rotation = Quaternion.identity;

GameObject go2 = new GameObject("GO2");
go1.transform.position = Vector3.zero;

Quaternion q = Quaternion.Euler(1.0f, 2.0f, 3.0f);
Debug.Log($"q: {q.eulerAngles}");
// prints -> q: (1.00, 2.00, 3.00)
go1.transform.rotation = q;
// in the inspector -> X: 1, Y: 2, Z: 3

Quaternion q2 = Quaternion.Euler(q.eulerAngles.x * -1.0f, q.eulerAngles.y, q.eulerAngles.z);
Debug.Log($"q2: {q2.eulerAngles}");
// prints -> q2: (359.00, 2.00, 3.00)

go2.transform.rotation = Quaternion.Euler(q.eulerAngles.x * -1.0f, q.eulerAngles.y, q.eulerAngles.z);
// in the inspector -> X: -1, Y: 2, Z: 3
h4ri
  • 359
  • 2
  • 9
  • Neither do I, I got it to work differently in the meantime. But thanks for your help! – THG Oct 25 '22 at 10:11
0

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.

THG
  • 1
  • 1
  • 2