0
\$\begingroup\$

Hello I have a question about rotating a Rod hinged to a fixed point (so it swings all around), and targets. short form, I would like to know how to find a rotation that would point my rod (while still attached to the fixed point) towards the position of the target.

I would also like to find the rotation as a quaternion, and/or axis-angle to prevent gimbal lock.

I am using unity

The image I made shows my problem with clarity. enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ What have you tried? \$\endgroup\$
    – Tyyppi_77
    Oct 20 at 6:57
1
\$\begingroup\$

Assuming your cylinder is oriented so that its local y+ axis (green arrow on the local transform gizmo) points along the length of the cylinder (like it does for the default Unity cylinder), then you're looking for a quaternion that rotates (0, 1, 0) to lie parallel to (target.position - pivot.position).

(If that's not how your axes are set up for this object, be sure to edit your question so that critical details like this are made explicit)

Unity has a a couple of great convenience methods to do just this. You could try:

transform.rotation = Quaternion.FromToRotation(
                         Vector3.up,
                         target.position - pivot.position
                     );

or

Quaternion zToTarget = Quaternion.LookRotation(target.position - pivot.position);
Quaternion yToZ = Quaternion.Euler(90, 0, 0);
transform.rotation = zToTarget * yToZ;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.