0
\$\begingroup\$

I have a transform which I need to point one of its axes in the direction of another game object, without disturbing or rotating the other axes in any way.

Consider the way your hips move around on a horizontal plane, while both your upper and lower body rotate to face the hip bone's position, without turning left or right.

See the waist

How do I achieve the above using Unity transforms in C#?

Controller is moving only on a horizontal plane. T1 and T2 need to point towards this without changing its front facing direction

In this image, both T1 and T2 need to face the controller anywhere it moves without spinning on their Up axis, meaning their forward axis must always remain forward.

While reviewing my question I see Billboarding as a suggestion. I am asking how to billboard both T1 and T2 to the controller without turning their forward directions.

The problem I am facing comes down to the three axes. If I apply LookAt() to the controller, even if setting the x, y, or z, or both xz or xy or zy to 0, there is no way that LookAt() can point T1 in the right direction along the Z and X axes while ignoring the Y. It is always Z rotation only or X rotation only or undesired Y turning as the result. LookAt is causing the front of the body to face away from the front when the controller moves, I need to lock it to face forward, but tilt side to side and back and forth.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I get a lot of mileage out of this helper function:

static Quaternion TurretLookRotation(Vector3 exactUp, Vector3 approximateForward) {
    return Quaternion.LookRotation(exactUp, -approximateForward) * Quaternion.Euler(90, 0, 0);
}

This gives you a quaternion that points the local up axis exactly in the direction specified by exactUp, then twists to keep the local forward axis as close as possible to approximateForward.

So you might want to use it something like this:

torso.rotation = TurretLookRotation(torso.position - hips.position, camera.forward);
legs.rotation = TurretLookRotation(hips.position - legs.position, camera.forward);

(Though you may want to negate camera.forward if you want to point the forward axis at the camera)

To keep a point of contact with the hip object, you'll also need to shift the positions of the leg/torso objects, something like:

torso.position = hips.position + torso.rotation * (new Vector3(0, torsoPivotHeight, 0));
\$\endgroup\$
2
  • \$\begingroup\$ Thank you for this, I am about to test it out. Also thank you for editing the post properly \$\endgroup\$
    – DIGITII
    Commented Oct 6, 2022 at 15:23
  • \$\begingroup\$ This worked perfectly. Thank you once again. If there is any gold or point to distribute in the future I have save this to remember to award your answer. \$\endgroup\$
    – DIGITII
    Commented Oct 7, 2022 at 3:07

You must log in to answer this question.

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