Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unity C# – How to find point C in a right-angled triangle?

could you please help me with this task? Here’s the problem:

We know the locations of point A and point B, and we know that our triangle is right-angled. We also know that the right angle will be at point C, and we understand that the sum of all angles in a right-angled triangle equals 180 degrees.

The question is, how do we find the location of point C?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m developing a goalkeeper bot, where point A represents the goalkeeper, B represents the ball, and point C is the location where the bot should reach to stand opposite the ball. The goalkeeper always has their back turned to the goal, so we can use transform.right.

All points are Vector3, where Y will always be 0.

(In the picture, there is a top-down projection. The goal and the goalkeeper can rotate, but the goalkeeper will always have their back to the goal.)

enter image description here

I’ve tried many ways to solve this problem, but haven’t been able to arrive at the correct solution.

>Solution :

As I understand you know the goal rotation / direction and lets just say we can directly use the goal’s right vector as a normal then you could simply use Vector3.Project

Vector3 A, B;
Transform Goal;

// Vector A->B
// mapped onto the Goal.right direction
// and result applied as new vector relative to A
Vector3 C = A + Vector3.Project(B - A, Goal.right);

No need to calculate the angles yourself 😉

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading