Direction vector between two parts?

How would I get the direction from one player to another one? Like a lookvector, but instead from one part to another. I tried to do (Part1.CFrame.p - Part2.CFrame.p).Unit but it doesn’t seem to be it.

9 Likes

That’s correct. (part2.Position - part1.Position).unit will get you the direction vector between the two parts with a magnitude of 1. If you want the vector to extend from a point you’ll need to translate it by one of the part’s positions by adding it.

i.e part1.Position + (part2.Position - part1.Position).unit

you can then multiply the direction vector by whatever magnitude you need.

27 Likes

A direction vector does never rely on a translation though. The order determines what direction the vector has, if @vsnry his result is an incorrect vector he likely needs to switch the order or multiply by -1.

right, but for most intents and purposes, a direction vector extended from the origin is less useful than a direction vector extended from an arbitrary point.

…if the direction is all that matters the position is entirely irrelevant though and is better left at the origin.

All i did was extend my answer to include an additional use case - i’m not sure what the issue is here?

1 Like

I do not think there is much of an issue but it does not help OP to translate the vector as a solution to getting the correct direction vector. It seemed odd as response to the post as stated. However stating a vector with a position included as direction vector is a better alternative to one from the origin seemed a silly statement to say the least.

Maybe it’s all interpretation, but I assumed he wanted the direction from the first part. I apologize if this was confusing.

1 Like

I see what you mean with that, I guess we simply consider differently what OP actually asked for. When a lookvector is mentioned I generally consider it a direction and nothing more, which would be from the origin as that is where a vector starts from generally.

2 Likes

As @Chaotic_Cody mentioned in his first response, your original implementation is correct.

That being said, look at this awful way I used to do this years ago:

local direction = CFrame.new(Part1.CFrame.p, Part2.CFrame.p).lookVector

DON’T DO THAT

8 Likes

lmao that’s pretty great

On the other hand, I still see experienced developers constantly trying to do:

CFrame.new(at, lookAt) * CFrame.Angles(blah blah blah), when what they really wanted in the first place was CFrameFromTopBack(at, topVector, backVector), but they didn’t know that such a function was possible. Fortunately that later is finally being added to the API soon.

7 Likes

here is a simple script:

local part0 = workspace.Part1
local part1 = workspace.Part2
local direction = part1.Position - part0.Position

You almost had it, you used CFrame which would not work. You only need position.