Hello
I am trying to make an aiming system for my 2d game. I need to make a gameobject look at one point while rotating around another gameobject.
I have included a diagram to try and explain.
Any help would be appreciated. Thanks!
Hello
I am trying to make an aiming system for my 2d game. I need to make a gameobject look at one point while rotating around another gameobject.
I have included a diagram to try and explain.
Any help would be appreciated. Thanks!
To anyone that comes across this:
I finally figured out a solution. We can assume that the distance from the pivot to the target is the hypotenuse of a triangle, and that the distance from the pivot to the shooting axis (as in the length of a line perpendicular to the shooing axis to the pivot) is another side of the triangle. Using some trigonometry, we come to the equation:
x = Distance from the pivot to the shooting axis
h = Distance from the pivot to the target location
Pivot Rotation = Rotation to target - asin(x / h)
Note that all distances are positive, as they are a single distance (Vector2.magnitude), not a position in world coordinates or a vector.
If you want to understand the math behind this, I recommend starting here: Introduction to Game Development (E22: trigonometry) - YouTube
Hope this helps!