# Rotating a parent rectangle to align child with target

My beautiful drawings will explain better than many words (or at least I hope):

The structure:

• The black rectangle is a GameObject (my weapon sprite).
• A and B are also GameObjects (children of black rectangle): StartShotPoint and ShotPoint used for the direction of my projectile.
• C is the first hypothetical target

In this situation my sprite will be rotate of 0 degree.

In this second image there is the question. I want to find the rotation of my sprite (-10 degree in the example) knowing the Vector2 destination D and of course all the coordinates of the 3 GameObjects.

The situation is simple as long as the player rotates the weapon, but in this case is the AI (after finding the player at the point D) must turn the weapon in the right direction. I know how to find the direction between one point and another, but between two decentralized points and a third one messed up my brain.

I really don't know which "direction" to take.

• Have you tried using transform.LookAt? If it appears to be a valid solution for your context here is some further discussion from Unity forums discussing trying to apply it in 2D contexts vs implementing from scratch. Commented Nov 13, 2019 at 18:27
• @disc_code22 Unfortunately I don't think I can use transform.LookAt or simply VectorB - VectorA to find the direction and then the angle, because the direction must be given by A and B (go through A and B) by rotating the rectangle (father of A and B). Or at least I can't find the correct way for use LookAt in my case (similar in concept, but very different in the specific case).
– Baro
Commented Nov 13, 2019 at 19:37
• Cant you pretty much ignore B, at least when finding the rotation angle? It will always be on the line between A and the target point no? Or how about another conceptual approach: A and B are both offset from the center line of the rectangle by a distance lets call Y. You can call LookAt on the rectangle, but target (Dx, Dy-Y). Thereby you can accommodate for the offset and still take advantage of the LookAt function that already exists. If this sounds like a potential solution I would be happy to expand this comment into a full answer. Commented Nov 13, 2019 at 19:44
• @disc_code22 Mate... is perfect !!! Two lines of code to find the direction! Vector3 FakeTarget = new Vector3(RealTarget + (ShotPoint - Rectangle.position)); and direction = FakeTarget - Rectangle.postion. I don't know how to thank you !!! Indeed, yes I know ... Write the answer and I'll accept it :). Thanks again.
– Baro
Commented Nov 13, 2019 at 20:25
• I think the title of this question could use improvement but I'm struggling to come up with a good title. Maybe "Rotating a parent rectangle to align child with target"? If anyone has other suggestions I would be very open to them. If no more title ideas come in the next couple hours I will go ahead and re-title the question. Commented Nov 13, 2019 at 21:48

Okay so the goal is to be able to rotate a rectangular, which I'll call sprite, to line up with an arbitrary point in 2D space, which I'll call target.

Rotating sprite to line up with target can be achieved through the Transform.LookAt method provided by Unity, however this alignment will be based upon sprite's center, and we would rather have the target aligned with an arbitrary point within the sprite. We'll call this point firepoint, as in the case of a gun or cannon sprite, where projectiles need to be fired from a specific point and not just the center of the sprite.

Basically the Transform.LookAt nearly gives us the correct rotation, we just need to translate it. Currently it is aligning sprite center with target, and instead we want firepoint aligned with target.

To align firepoint and target we can calculate an offset target, that is offset from the target position by the same amount that firepoint is offset from the sprite center. Now, using offset target for the sprite to LookAt, we line up the center point and offset target. This ends up aligning firepoint and target since the difference between both targets and both points is offset.

This can be illustrated using the diagrams you provided:

Looking at the diagram above, imagine that we have been given our target as point C. To align firepoint and C, we need the sprite to LookAt D, the point that exactly offset away from C.

Looking at the above diagram, which I have shoddily modified, we have a target D, also I have added an offset target E below point D by amount offset. By LookAt'ing the rect towards offset target E we will end up lining up firepoint with the target.

Further information can be found in the Unity Docs on LookAt, as well as in this Unity forum thread where users discussed how to recreate or use the LookAt function effectively in a 2D context.