How does Vector3 - Vector3 produce a direction?

Can someone please explain to me how to find a direction using (Vector3 - Vector3)
I know that subtracting a Vector3 from a Vector3 just subtracts the matching X Y Z coordinates from the Vector3 and I know that .Unit just returns the given direction but with a magnitude of 1. I just don’t understand how that works to give a direction in the workspace. Can someone please explain or give examples.

1 Like

Vector3 - Vector3 does not tell us a lot, but it certainly sets one of the vectors as its origin and accounting the result of the calculation as the vector with a direction.

You can visualize a two-dimensional vector where it goes from point A to point B, and subtracting one of them yields the same vector but shifted towards the origin. It’s still parallel.

It doesn’t give a direction, it just gives the point in between the two positions, but in some contexts a vector3 point can be used as a direction.

For example, if you take the vector3 point 1, 0, 0 and add it to a part’s position, the part will move 1 stud to the right, so technically the vector3 point 1, 0, 0’s direction is to the right.

how does this work then?. main and torso are just two Vector3’s in the workspace and it some how makes a ray face in the direction of “Torso” relative to “Main”

local rayCastResult = workspace:Raycast(main.Position, (torso.Position - main.Position).Unit * 100)

You have two positions A and B. Think of these of dots, or as arrows going from 0, 0, 0 to x, y, z.

image

We subtract A from B by flipping A, and matching its tail up with B:

image

Notice that this pink arrow also represents the offset from A to B, if we move it:

image

Note that we can’t really move vectors like this. They all start at 0,0,0 and end at some position x, y, z. But it’s also useful to think of them as free-floating arrows with a defined magnitude and direction.

3 Likes

Also, you could imagine this in one dimension if it makes it easier to think about.

Imagine two numbers on a number line. A = 9 and B = 4.

What’s the one-dimensional “direction vector” from A to B? Obviously, it’s -5:

2 Likes

Oh i understand now. the subtracting it will kind of “flip” the arrow/ point and give it a different off set. but it is still facing the same direction because it is relative to b. but could you please show me and example of how we could make an object face a direction using this?

Well, the easiest way is to cheat and use one of the CFrame constructors that will do it for you:

local A = Vector3.new(0,5,2)
local B = Vector3.new(10,10,10)
local part = workspace.Part

part.CFrame = CFrame.lookAt(A, B)

Doing it manually is more difficult, and not usually necessary. But it does involve using this kind of vector and CFrame.fromMatrix. I can go into it more if you want, but the above usually suffices.

B - A usually comes up in raycasting, when you need to provide a direction vector, but all you have is a start and end point.

1 Like

yes could you please explain the harder way. I see it used all the time but never really understand it. oh and thanks for all the help btw[

Do you have an example of what way exactly you’re thinking of?

for example a part looking at another part with out using cframe.lookat. finding the direction using what we have talked about and implementing it. I know it’s more complicated then it needs to be but it will help me in the future. and I cant seem do replicate it in studio

Okay, I’ll give it a shot.

A part’s rotation comes from three important vectors. These are:

local cf = workspace.Part.CFrame

-- the three rotation vectors:
local right = cf.RightVector -- where the part's right face is pointing
local up    = cf.UpVector    -- where the part's top face is pointing
local back  = -cf.LookVector -- where the part's back face is pointing

image
(right=red, up=green, back=blue)

If you know these ahead of time you can make a CFrame from scratch with fromMatrix:

local right = Vector3.new(1, 0, -1) -- 45 degrees northeast
local up = Vector3.new(0, 1, 0)     -- straight upwards
local back = Vector3.new(1, 0, 1)   -- 45 degrees southeast

part.CFrame = CFrame.fromMatrix(part.Position, right, up, back)

image

So… if we wanted a part to point from position A to position B, we need to figure out these three vectors.

back is easy—it’s what we just learned, B-A. Except, in this case, we want the reverse direction, so we use

local back = A - B

right can be calculated by using the cross-product with some global “upwards” direction (not the same as the actual up vector, mind you). I leave it to you to research how the cross product works. In ROBLOX:

local right = Vector3.new(0, 1, 0):Cross(back)

up can also be calculated with the cross product, but this time from the other two vectors:

local up = back:Cross(right)

Putting it all together:

local part = workspace.Part

local A = part.Position
local B = Vector3.new(3,3,-3)

local back = A - B
local right = Vector3.new(0, 1, 0):Cross(back)
local up = back:Cross(right)

part.CFrame = CFrame.fromMatrix(A, right, up, back)

image

(yellow is B)

Viola. Let me know if you have questions, that was a lot to take in :slight_smile:

2 Likes

thanks i have an understanding on this now. but could you explain how the Cframe.fromMatrix works. What do the 3 values in the variable “right” represent?.

What part are you referring to?

these. the each component of the vector3 that you are using for the CFrame. normally they are x y z coordinates but what do they represent now and how do I understand and manipulate them?

Those are vectors, as I explained in my post. They’re the colored arrows in the first image, as I explained in my post.

CFrame.fromMatrix turns a position + those three vectors into a CFrame.

I can’t really explain any more. I recommend you read this to understand what a vector is:

And/or watch this:

After you understand that, you should read this:

Anything else I would say would just be repeating those. Between all of these resources you should be able to figure it out :slight_smile:

If you have any specific questions, you can make a new post. But most if not all of your questions will probably have been asked before so search the forum!

3 Likes

I will defiantly be reading these, and thank you so much.

1 Like

so… b-a is a longer length than the dotted arrows? just a bit hard to tell since no measurement