0
\$\begingroup\$

In the game I'm creating I have a square that rotates around a parent point, I want the square to always aim upright however (think of a rotating platform). What I've done is simply use child.transform.rotation = Quaternion.Euler(0.0f, 0.0f, -gameObject.transform.rotation.z) This rotates the child object in the opposite direction so that they're always aiming in the right direction. Except it doesn't, there is some error that piles up with bigger rotations. For example at 90º the child object will be rotated -90.77 instead which is very noticeable given that the game has a grid of sorts and when objects are not aligned to it they instantly pop out.

So my question is, is there any way to avoid this error?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

transform.rotation is not an Euler angle triplet. It is a quaternion. So its z component is not a rotation angle that you can pipe through Quaternion.Euler meaningfully.

If you want to apply only the roll of one object to another, you can do something like this:

child.transform.rotation = Quaternion.LookRotation(Vector3.forward, transform.up);

This forms a rotation whose forward direction maps to world forward (so 0 pitch / yaw), and whose up direction is as close as possible to parallel to your reference object's up vector when using roll alone.

Or to roll it in the opposite direction, take the Quaternion.Inverse of the above.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .