- Home /
 
is this the right way to find an angle?
trying to take a weapon turret and a target and rotate the weapon to aim at the target.
I'm not just rotating though because the weapon has a horizontal and vertical dial so i want to really do 2 rotations at once and I think that means i basically need to find 2 angles, the x angle and the y angle and rotate seperatly each dial on the right axis the right amount. I'm also working within angle constraints so weapons don't have 360 views.
because i'm doing 2 rotations instead of 1 it seems like i should be doing 2 2d rotations so i'm using vector2 not vector3.
for the x rotation i'm ignoring the y axis and for the y im ignoring the x.
 vector2 XWeapon;
 vector2 XTarget;
 vector2 YWeapon;
 vector2 YTarget;
 
 
             XWeapon.x = WeaponTransform.position.x;
             XWeapon.y = WeaponTransform.position.z;
             
             YWeapon.x = WeaponTransform.position.y;
             YWeapon.y = WeaponTransform.position.z;
             
             XTarget.x = AimPoint.x;
             XTarget.y = AimPoint.z;
             
             YTarget.x = AimPoint.y;
             YTarget.y = AimPoint.z;
             
             XAngleToTarget = Vector2.Angle(XWeapon,XTarget);
             YAngleToTarget = Vector2.Angle(YWeapon,YTarget);
 
               I'm just wanting someone to confirm to me that that looks like the right way to do it (so it'll work, i'll be testing of course but i want to make sure in principle it should work) and that i'm not going the long way about doing it.
Answer by robertbu · Jun 30, 2013 at 10:17 PM
I don't see how you use the two ?AngleToTarget values, but one issue is that Vector2.Angle() is unsigned, so you are not going to know which direction to rotate to aim your weapon. Another potential issue is that you are not calculating vectors from the right locations. You are using a vectors from the world origin, not vectors from the weapon in calculating the angles.
I posted a turret solution here:
http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html
There is no rotation limit, but it does handle the separate up/down, left/right rotations.
Your answer