- Home /
How can I get the distance from point a to point b on a custom axis? (Includes pictures)
Trying to find the distance from point a to point b given a custom axis. I have some pictures to help me better explain:
I'm trying to find the distance from red to pink (or gray) on two custom axes. The axis from red to green (axis RG) and the axis from red to blue (axis RB). The colored lines are just to help show where the points are in 3D space. The "points" are located at the bottom of the colored lines shown. These colored lines just point straight up in the air for reference.
Here's a link to a few more pictures for clarification.
I'll give you a free copy of my game when it's finished if you can help me out.
I'm confused that the red and pink is two lines not points. if you means "the point red/pink line crossed with the plane" Vector3.Distance is useful.(use point.transform.position as parameter)
Even with all the drawings, I'm confused by your question. What defines the two points you want to measure from? Are you looking for the closest point on the two lines or something else? Are the two axes always parallel? Are you looking for the shortest distance between the two lines?
@sooncat the lines are just to help point out the points in 3D space. The "points" are located at the bottom of the red and pink lines. These lines just point straight up in the air for reference.
@robertbu trying to implement something like this. I'm measuring (horizontally only) from the red point (known) to the pink point (known) relative to my axes. The two axes are never parallel and they're never perpendicular.
Take a look at the $$anonymous$$ath3D script in the Unity Wiki. There are several things that may help you in there. In particular, you may be looking for ClosestPointsOnTwoLines(). Given the two point, you can calculate the distance between them.
I'm not certain I understand your question but it looks like you want to measure from the red point to the point on an axis that is closest to the pink point:
If that is the case then you can do it quite simply like:
var point1 : Vector3;
var point2 : Vector3;
private var pointVector : Vector3;
var axis1 : Vector3;
var axis2 : Vector3;
pointVector = point2-point1;
distance from axis1 is: Vector3.Project(pointVector, axis1).magnitude
distance from axis2 is: Vector3.Project(pointVector, axis2).magnitude
Scribe
Answer by alok1974 · Feb 06, 2014 at 04:37 AM
Here's the psuedocode:
let v_rp be Vector from red to pink then, v_rp = transform.poition of pink - transform.position of red
let d_rp be distance between red to pink then, d_rp = v_rp.magnitude
ler v_rg be Vector from red to green then, v_rg = transform.poition of green - transform.position of red
let theta be angle between v_rp and v_rg then, theta = Vector3.Angle(v_rg, v_rp)
convert theta to radians theta *= Mathf.Deg2Rad
then distance d between red to pink projected on the axis of red to green is given by:
d = d_rp / Mathf.Cos(theta)
That's it. Now to get the same for on red to blue axis, just replace green by blue in the above pseudo code.
Rest remains the same.
Cheers!
Your answer
![](https://koobas.hobune.stream/wayback/20220613132646im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Checking camera distance between object in a AR game 1 Answer
Fog only working on light sources? 1 Answer
Unity or Custom Engine? 1 Answer