# Turret rotation angle

 0 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. more ▼ asked Feb 26 '12 at 12:05 AM Tomas 5 ● 4 ● 4 ● 8 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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); } ``````} more ▼ answered Feb 26 '12 at 02:25 AM Blazor Ramone 134 ● 1 ● 2 ● 5 I'm not sure if this is the best solution to this problem and I'm interested in seeing other solutions if anyone has them. Feb 26 '12 at 02:32 AM Blazor Ramone Thanks for your answer. What you have explained works with a couple of modifications, but I am not marking this as a solution yet because you missed one little detail in my question. Firstly, the fixes I needed to do for your solution to work: I want my turret to rotate only along y axis, so "targetPos.y = transform.position.y". Instead of "Quaternion.AngleAxis(-offSetAngle , Vector3.up)" I used "Quaternion.AngleAxis(+offSetAngle , Vector3.up)". Now the thing you missed in the question. Your solution presents a way to determine the angle I need to rotate my turret so it would start hitting the target right in the middle, but I wanted to find the MINIMUM angle I need to rotate my turret for it to start hitting the target (just the side of the target not the center) and the angle at which it becomes rotated too much and stops hitting the target. Let's assume my target is circular like the one in the picture. Feb 26 '12 at 05:47 PM Tomas Sorry, the second fix was only needed because my turretOffset was negative. Feb 26 '12 at 06:12 PM Tomas Assuming that the target is or can roughly be approximated as a sphere or circle you can compute two offset angles where you add or subtract the targets radius to the turretOffset.offsetAngle1 = Mathf.Atan((turretOffset - radius) / toTarget.magnitude) * Mathf.Rad2Deg;offsetAngle2 = Mathf.Atan((turretOffset + radius)/ toTarget.magnitude) * Mathf.Rad2Deg;one of these will be minimum one will be max depending on your orientation. Feb 26 '12 at 07:22 PM Blazor Ramone Thank you for your help :) Feb 26 '12 at 07:29 PM Tomas add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: