I made used a little trigonometry to make a sentry gun automatically aim at targets which it works (At least it aims pretty close to the target) but it always appears to make the gun aim above the target by roughly 0.5 - 10 degrees depending on the location of the target.

Note - I will add the interpolation later but any optimizations or code clean up would be handy too.

The code:

```
Vector3 v = this.Target.transform.position - this.gameObject.transform.position;
if ( v.z > 0 )
{
this.XAxis.transform.rotation = Quaternion.Euler (
Mathf.Atan ( v.y / v.z ) * Mathf.Rad2Deg * -1,
this.XAxis.transform.rotation.eulerAngles.y,
this.XAxis.transform.rotation.eulerAngles.z
);
this.YAxis.transform.rotation = Quaternion.Euler (
this.YAxis.transform.rotation.eulerAngles.x,
Mathf.Atan ( v.x / v.z ) * Mathf.Rad2Deg,
this.YAxis.transform.rotation.eulerAngles.z
);
}
if ( v.z < 0 )
{
this.XAxis.transform.rotation = Quaternion.Euler (
Mathf.Atan ( v.y / v.z ) * Mathf.Rad2Deg,
this.XAxis.transform.rotation.eulerAngles.y,
this.XAxis.transform.rotation.eulerAngles.z
);
this.YAxis.transform.rotation = Quaternion.Euler (
this.YAxis.transform.rotation.eulerAngles.x,
Mathf.Atan ( v.x / v.z ) * Mathf.Rad2Deg + 180,
this.YAxis.transform.rotation.eulerAngles.z
);
}
```

Explanation of the Variables:

v = the difference in the positions of the gun and target

XAxis = the part of the gun that does the rotation on the X axis (Up and Down)

YAxis = the part of the gun that does the rotation on the Y axis (Left and Right)

*Update - Apparently the issue of the gun aiming too high gets worse when the difference in the z positions of the target and gun get close to zero. Another new issue is that I noticed that the gun also seems to aim below the target if the target is lower than the gun.