Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

Given 2 perpendicular lookVectors, how do I calculate the 45 degree Vector3 in between them?

Asked by 5 years ago

So just to be clearer, I mean the Vector3 such as mouse.Hit.lookVector.

1 answer

Log in to vote
5
Answered by 5 years ago

You can accomplish this by adding the two vectors together and taking the unit of that vector.

Note: The following works when the vectors are perpendicular, as well as any case other than the special cases. You will have to do additional checks if you need to verify that the vectors are perpendicular before using this method.

local Vector1 = Vector3.new(0,1,0) -- First LookVector
local Vector2 = Vector3.new(0,0,1) -- Second LookVector

local NewVector = (Vector1 + Vector2).Unit -- Vector in the middle of the two

Special Case

Certain vector combinations, such as adding zero vectors and vectors looking in opposite directions, are impossible to find the unit of. Attempting to do so will return:

Vector3.new(NaN,NaN,NaN)

This can cause parts to do strange things such as disappearing, therefore it is important to check if your vector is NaN (Not a Number) before you use it. You can do this with the following:

local ImpossibleVector = Vector3.new(0,0,0).Unit

ImpossibleVector = ImpossibleVector.X == ImpossibleVector.X and ImpossibleVector or Vector3.new(0,0,0);

Because NaN is the only number that does not equal itself (NaN ~= NaN), you can check if a component of the vector is equal to itself. In line 2 I check if the X component of the vector is equal to iself, returning the vector if it is, if not returning a zero vector.

Special cases that result in NaN vector.

Vector3.new(0,0,0) + Vector3.new(0,0,0) -- Adding zero vectors
Vector3.new(0,1,0) + Vector3.new(0,-1,0) -- Vector looking straight up + Vector looking straight down
0
good ansswer omg WideSteal321 773 — 5y
0
I try my best! GrandpaScriptin 148 — 5y
0
Was a little rusty on my linear algebra. Thanks! bloberous 66 — 5y
Ad

Answer this question