- Home /
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.
Answer by Blazor Ramone · Feb 26, 2012 at 02:25 AM
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);
}
}
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.
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".
Ins$$anonymous$$d 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 deter$$anonymous$$e 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 $$anonymous$$INI$$anonymous$$U$$anonymous$$ 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.
Sorry, the second fix was only needed because my turretOffset was negative.
Assu$$anonymous$$g 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 = $$anonymous$$athf.Atan((turretOffset - radius) / toTarget.magnitude) * $$anonymous$$athf.Rad2Deg;
offsetAngle2 = $$anonymous$$athf.Atan((turretOffset + radius)/ toTarget.magnitude) * $$anonymous$$athf.Rad2Deg;
one of these will be $$anonymous$$imum one will be max depending on your orientation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064455im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need Help with random rotatition on game turret 2 Answers
Turret script. Making it aim in heigth only. 1 Answer
3D Turret Rotation 0 Answers
3D nested Turret Prefab Rotation 1 Answer
Bullet reload problem 3 Answers