unity3d - How can I get the distance from point a to point b on a custom (relative) axis? -


trying find distance point point b given custom axis. have pictures me better explain:

one

i'm trying find distance red pink (or gray) on 2 custom axes. axis red green (axis rg) , axis red blue (axis rb).

two three

you're asking vector projection.

given 2 vectors a , b, a projected onto b?

in picture projected onto b.

in case, a seems difference between red , pink, b you're calling custom axis.

calculating projection involves dot product. lucky you, unity provides vector3.dot make easy.

we can calculate projection scalar. "this much" in direction of b:

float projscalar = vector3.dot(a, b.normalized); 

this gives length you're asking about.

if needed, can convert result vector, casting length in direction of b:

vector3 projvector = b.normalized * projscalar; 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -