Trying to find the distance from point a to point b given a custom axis. I have some pictures to help me better explain:

one

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).

two three

有帮助吗?

解决方案

You're asking about vector projection.

Given two vectors A and B, what is A projected onto B?

In this picture A is projected onto B.

In your case, A seems to be the difference between red and pink, where B is what you're calling a custom axis.

Calculating this projection usually involves a dot product. Lucky for you, Unity provides Vector3.Dot to make this easy.

We can calculate the projection as a scalar. A is "this much" in the direction of B:

float projScalar = Vector3.Dot(A, B.normalized);

This gives us the length you're asking about.

If needed, we can convert that result into a vector, by casting that length in the direction of B:

Vector3 projVector = B.normalized * projScalar;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top