Search code examples
c#unity-game-enginegeometrytrigonometryunity3d-editor

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?

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