0
\$\begingroup\$

I have a turret transform which is a child of weapon game object. Which in itself is a child of player object. I want to rotate this turret towards a world point (Raycasthit.point of a ray going forward from center of the screen).

One thing to note is that weapon model is made in blender and turret bone have a default rotation of (90,0,0). It looks like this:

Image of turret setup, with green arrow pointing along barrel

I tried the usual Quaternion.LookRotation method by subtracting raypoint from turret local position, from world position and even by converting the raypoint from world to local by InverseTransformPoint and I also tried changing the world up direction to every possible combination. I also tried SetLookDirection method with every possible combination. But I just can't seem to make the rotation work properly.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Quaternion.LookRotation(forward, up) gives you a rotation that points the local z+ axis (blue arrow) toward the "forward" argument, and the local y+ axis (green arrow) as close as possible toward the "up" argument.

Since you want to point with the green axis instead, you can just apply an extra rotation to it:

// Make a rotation that points z+ at our target, and y+ away from the turret base.
// The second argument is optional, but helps control "twisting" of the barrel.
Quaternion zToTarget = Quaternion.LookRotation(
                          hit.point - transform.position,
                          directionOutFromBase
                       );

// Make a rotation that points y+ toward z+ (and z+ toward y-).
Quaternion yToZ = Quaternion.Euler(90, 0, 0);

// Chain these rotations, so we put the y axis where the z axis was,
// then transform the z axis to point at our target, effectively
// pointing with our y axis instead!
transform.rotation = zToTarget * yToZ;
\$\endgroup\$

You must log in to answer this question.

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