I have a behavior script for a turret and want it to make it aim in a way so that the shaft only turns left and right to align with the target and the barrels only up and down to point at it, but i’m not exactly sure how to do so.
Here’s what i have so far (which only makes the turret spin wildy):
void LookAt (GameObject target)
{
Vector3 targetRelativePosition = target.transform.position - transform.position;
float targetRotation = Mathf.Atan2 (targetRelativePosition.x, targetRelativePosition.y) * Mathf.Rad2Deg + shaft.transform.eulerAngles.y;
shaft.transform.eulerAngles = Vector3.up * targetRotation;
}