Tank turret locking localRotation of X, Z

Hi all, I’m using look rotation and slerp to move a tank turret slowly towards a crosshair. I’m wondering if anyone could help me figure out how to lock the X and Z axis of the turret so if the tank is at an angle, the turret follows the tank’s angle. Here’s how I have it moving:

Quaternion newRotation = Quaternion.LookRotation(aimAt - transform.position, Vector3.up);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * speed);

This is what it looks like at an angle. You can see the turret is staying up-right, but it should be angled with the chassis of the tank.

I guess I’m trying to figure out how to transfer the rotation to the localRotation?

I can throw the tip - if you want this to work well you need to do this with local rotation and reset angles on other axis. In addition I think slerp can’t do that well as it might want to use shortcut. Also if you need to stablize on other axis you might need to remap position (cast on plane).

Thank you for the tips. I’ll start looking into reworking this! If there’s one thing I keep trying to learn and can never retain, it’s Quaternions…

Here was my approach for a turret with separate traverse and elevation… it may be useful to you:

Turret aiming/rotating:

In particular, look at how I structured the hierarchy of Transforms.

1 Like