# Turret rotation angle

Let’s say I have a turret and a target like the ones shown bellow. How do I calculate rotation angle I need to rotate the turret around y axis for it to start hitting the target and the angle at which it becomes rotated too much and stops hitting the target. Keep in mind that the cannon is not placed in the center of the turret. Ok typically if the cannon was in line with the pivot point of the turret you could use The Quaternion.LookAt function to determine the desired Rotation. However, with this offset turret you are going to need to add an additional offset angle. The magnitude of this angle is going to depend on both how far way the target is from the pivot point and how far offset the turret is from the pivot(measured along an axis perpendicular to direction the turret is pointing). Anyway if you divide the turret offset by the distance you will get the tangent of the angle you need. You can use this to calculate the additional rotation to add. An example:

``````public class LookAt : MonoBehaviour
``````

{

``````public Transform target;
public float offSetAngle;
float turretOffset = 2.66f;//distance to barrel along x axis

void Update()
{
Vector3 toTarget = target.position - transform.position;
offSetAngle = Mathf.Atan(turretOffset / toTarget.magnitude) * Mathf.Rad2Deg;
transform.rotation = Quaternion.LookRotation(toTarget)
* Quaternion.AngleAxis(-offSetAngle , Vector3.up);

}
``````

}