Hello, I posted a few days ago about a similar issue. But now I’ve scratched that and I’m using hinge constraints, however I’m having trouble finding the formula to calculate the angle required for the gun barrel to aim towards the target. See photos
Vector3s work with no effective difference to a 2-dimensional vector as far as the fundamental math goes.
To calculate this with math you would use dot product (which is essentially v1.mag*v2.mag*cos(angle between vectors). Fortunately Roblox has v1:Dot(v2) for this, which saves some trouble.
This would be the math required to get the angle between two vectors:
a = workspace.a
b = workspace.b
dot = a.Position:Dot(b.Position)
m_a = a.Position.Magnitude
m_b = b.Position.Magnitude
angle_between = math.deg(math.acos((dot)/(m_a*m_b)))
print(angle_between)
Probably because this is solving for the overall angular difference between two 3D vectors, and because magnitude cannot be negative in nature you have no actual direction, just the angle difference. This angle difference could never be > math.pi.
Here’s a better approach to get the actual angle:
a = workspace.a
b = workspace.b
axis1, axis2 = "X", "Y"
p_v = a.CFrame:PointToObjectSpace(b.Position)
angle = math.deg(math.atan2(p_v[axis2], p_v[axis1]))