Find a point in space using Vector3 angles and Raycast
I've been trying to figure out how to calculate a point in space using the hypotenuse of a right triangle created with a raycast.
At this point I couldn't find or think of any solution to this problem.
The blue dots in the image are the values I have at any given momment, and I need to find the 'E' value to calculate the distance between B and E.
Can you help me solve this issue?
Answer by DanielErhardt · Mar 01, 2016 at 06:32 PM
I came up with the solution for this a couple days ago.
This is a right triangle, and will always be because C is the ground position (y = 0) of A at all times; Since it'll always be a right triangle, the distance between A and B is always proportional to the distance between A and E.
Here is how you do it:
Remember: A is a ray origin, and D is a raycast hit point;
float AB = Vector3.Distance(A.origin, B.transform.position);
float AC = Vector3.Distance(A.origin, C.transform.position);
float AD = Vector3.Distance(A.origin, D.point);
float proportion = ab/ac; //this gives a float between 0 and 1 to be used as percentage
float AE = AD * proportion;
//Now we use the direction of the ray and AE as the magnitude to find E;
Vector3 E = A.direction * AE;
It took me quite a while to understand this, since math is not the strongest of my qualities, but there it is.
Your answer
![](https://koobas.hobune.stream/wayback/20220612064735im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wall sliding with characterController against an edge 0 Answers
Need to place an object at a certain angle form the camera and at a certaine distance 0 Answers
Raycast under crosshair 0 Answers
Check if a point is on the right or left of a vector? 2 Answers
How do I find a point along a line with a forced x and z values 0 Answers