Now, I haven’t coded this, but there is some further math you could use that avoids angles entirely, and has a neat outcome at the very end, from a Unity gamedev point of view:
A is at (0, 0).
B is at (s, 0).
C is at (u, v).
B’ is at (x, y), the point where the rotated arm aims the cannon at C.
Triangle AB’C is right at B’.
Thus, AB’ dot B’C = 0.
The distance from A to B’ is the same as from A to B.
Thus, x^2 + y^2 = s^2 (which is the equation of a circle of radius s, centered at the origin).
AB’ dot B’C = (x - u)x + (y - v)y = 0.
By a little algebra, that dot product becomes the equation of a circle centered at (u/2, v/2):
(x - u/2)^2 + (y - v/2)^2 = (u^2 + v^2) / 4.
We want the points where these two circles intersect.
By using a method published by Paul Bourke (also written up on StackOverflow), we can find those intersections:
Here’s what we know:
The first circle is defined by:
x0 = 0.
y0 = 0.
r0 = s.
The second circle is defined by:
x1 = u/2.
y1 = v/2.
r1 = sqrt(u^2 + v^2) / 2
(Note that if r0 > r1, you can’t make the cannon point at C.)
Using Bourke’s method (which is generalized, making some of the next few steps unnecessary, but harmless), proceed as follows:
d = r1
a = (r0^2 - r1^2 + d^2) / (2d)
h = sqrt(r0^2 - a^2)
x2 = x0 + a(x1 - x0) / d.
y2 = y0 + a(y1 - y0) / d.
First solution at P3:
x3 = x2 + h(y1 - y0) / d.
y3 = y2 - h(x1 - x0) / d.
Second solution at P3’:
x3’ = x2 - h(y1 - y0) / d.
y3’ = y2 + h(x1 - x0) / d.
Now, only one solution points at C, the other points away. You can take a vector cross-product to know which one you want:
if |AP3 x P3C| > 0, P3 is your solution, otherwise you use P3’.
Now, here’s why this is cool: if the cannon were translated back down the arm to A, it would be pointing along a line that included a point C’, defined by translating C by the same amount you just translated the cannon. This point is simply C’ = C - B’ (remember that B’ is either P3 or P3’, depending on the cross-product).
This means you can make the cannon point at C by using the Transform.LookAt function and making A’s transform look at C’. You never need to calculate, nor even know, the angle of rotation.