Rotate object towards target

I have a turret that is supposed to rotate toward it’s target if it is in view. This is what I have for the rotation code:

Vector3 targetPoint = target.root.position;
						
Quaternion targetRotation = Quaternion.LookRotation (targetPoint - (rotatingTransformY.transform.position), Vector3.up);
				
rotatingTransformY.transform.rotation = Quaternion.RotateTowards(rotatingTransformY.transform.rotation, targetRotation, 40.0f * Time.smoothDeltaTime);

//make sure it only rotates on it's Y axis
rotatingTransformY.transform.eulerAngles = new Vector3(0, rotatingTransformY.transform.eulerAngles.y, 0);

The problem I am having with this is that the closer the turret gets to looking at the target, the rotation starts slowing down. This is an “Anti air turret” that is supposed to be able to take down flying vehicles in my game, but it isn’t working that well, for example, if I fly by the turret with my helicopter, the turret is always lagging just behind the helicopter (probably due to RotateTowards lerp funcitonality), so it never catches up to the helicopter game object. I have tried to find a solution to this, but pretty much everywhere I’ve looked, there’s always the “solved” answer by using Lerp, Slerp etc… but that is not what I need. I need it to rotate towards the target at a constant speed.

Does anyone have any solutions to this?

you should be restricting to the Y axis before apply the RotateTowards, not after.

Create a vector from turret to target, remove the y component, normalize, pass into LookRotation.

Vector3 lookDir = (targetPoint - rotatingTransformY.transform.position);
lookDir.y = 0;
lookDir.Normalize();
Quaternion targetRotation = Quaternion.LookRotation(lookDir, Vector3.up);

As for the lag behind…

This is because you’re moving only part way to a target point that is changing. You’ll never reach the target.

Either target a point ahead of the target (in the direction it’s heading)… which is hard.

OR

Don’t lerp by a constant lerp value t. Instead calculate a lerp value t that would change a set number of degrees each tick (appropriately), so that it’s like the turret has a constant speed. And only lerp by some smaller value if the change needed between current and locked-on is shorter than the average change per tick.

You can do this by get the rotation difference between current and final, measure the size of that angle around it’s rotating axis (see Quaternion.AxisAngle), scale to some speed of angles/second, find this percentage compared to the full angle, this is your new t to put into the lerp. If smaller than angle/speed, then just put the full rotation in.

Thanks for the great response! looks like this may be what I need