Understanding Vectors

Understanding Vectors


Hello, my fellow scripters!

Today, in this community tutorial, I’m going to be going through every single little tip & trick relating to Vectors! From this post, you will learn:

  • What a Vector2 and a Vector3 means.
  • How to properly use them.
  • The tips & tricks on them.

Let’s begin.


Vector2:

A Vector2 is a datatype where you can store 2 values, usually a x and a y, into one variable. This datatype is usually used for 2D positioning, like a UI’s position, a mouse’s position on the screen and other things.

Here is an example code:

local x = 6
local y = 27

local vector = Vector2.new(x,y)

Remember, a Vector2 is just two numbers put into one variable, you can use a separate x and y variable if you wanted to, but this is simpler.


Vector3:

A Vector3 is identical to a Vector2, but it has an extra value inside, the z axis. This datatype is used for many more things, including position and orientation, a size, and anything else that goes in all directions in the physical world!

Here is an example code:

local x = 6
local y = 27
local z = 13

local vector = Vector3.new(x,y,z)

So now we know what vectors are, both Vector2 and Vector3. What can we do with this?


Using Vectors

Since we already covered this, this will be short. You can use a vector to set a property 's x, y, and an optional z. Let’s try creating a Part inside workspace, then setting it’s position to the spawn position:

local part = Instance.new("Part", workspace)

part.Position = Vector3.new(0,0,0) --x,y,z

Load the game in your studio, and you will see that the Part’s position has been set to those coordinates. Pretty neat, right?


Modifying Vectors

In this section, we’ll be going through modifying vectors. There’s many ways on how to modify them, so we’ll be only covering the more simpler ones.


Changing a specific axis

I’ve seen many people do this incorrectly, so let’s go over the proper way.

Let’s say you wanted to change the x axis in a Vector3. Take a look at what people usually do:

local vector = Vector3.new(1,1,1)

vector = Vector3.new(0,vector.Y,vector.Z)
--Sets the X axis to 0

Even though this works, it’s not really efficient, since you’re going through all the values in the vector. Take a look at this correct version:

local vector = Vector3.new(1,1,1)

vector -= Vector3.new(1,0,0)
--Subtracts the X value by 1, leading to the value 0

This code subtracts the x axis by 1, without needing to through all the values and confirming they’ll be left untouched. This code is more efficient, especially if your vector has a very long full name, like: workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position, you can just call this once and leave the other values untouched, to modify the one you want.


Adding two vectors together

This method is useful if you plan on changing more than one value in a vector.

Let’s say you wanted to change both the y and z values. This is what you would probably do with the previous method I used:

local vector = Vector3.new(5,9,1)

vector -= Vector3.new(0,9,0)
vector -= Vector3.new(0,0,1)
--Sets both the Y and Z axises to 0

This is very inefficient, you’re duplicating a block of code to do one thing, when it can very easily do two things. This is what you should do:

local vector = Vector3.new(5,9,1)

vector -= Vector3.new(0,9,1)
--Sets both the Y and Z axises to 0

(Please note that the operator can only be used once, so if you have to add a specific axis with a negative operator, you would have to make the axis itself negative to do that.)


The Aftermath

There’s much more relating to this, but it’d be pointless to list it here since it all branches off from those two examples I just listed above.

So, that’s it! You have now learnt what a Vector2 and a Vector3 is, how to use them, and the tips & tricks relating to how to use them.

I hope you found this post useful, I created this because I’ve seen many people with this common issue, if there’s anything I missed out, go ahead and tell me and I’ll make sure to edit this into the community tutorial.

Have a good day!

-DiamondDrencher1

14 Likes

Thank you man, this helped me well as i had no idea what those were… they are basically variables that store 2 items

I would argue that the best way of changing a component of a vector depends on the situation.

When you want to change one component of a vector, you don’t necessarily know the number that you need to subtract.

Let’s say we have a vector v and we want to change it such that it’s y component is a . I’ll write the code in both ways you have shown.

  1. Directly setting the component to the desired value (the way you call incorrect):
v = Vector3.new(v.X, a, v.Z)
  1. Subtracting a vector from the original vector (the way you call correct):
local v -= Vector3.new(0, v.Y - a, 0)

I consider the first way better in this case. In my opinion, it better shows what is being done: changing one component to a spesific value while keeping the other components the same. There are no calculations involved.

The second way looks more complicated in this case because of the calculations. What is being done is not obvious in my opinion.

I don’t know about the performance of the alternative ways. My opinions are based on what I consider better for readability. Also, I do agree that subtracting may be better if there’s a long path to the vector, and probably in some other situations too.

Summary:

  • If the desired change is known, I agree that the second way is better.
  • If the desired final value of the component is known, I would consider the first way better in many cases.

The reason why I thought the first option was complicated is because of the very long-named instances.

You wouldn’t want this long line in your code:

workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position = Vector3.new(workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position.X,y,workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position.Z)

Sure, you can have a variable, but all for just changing a Vector3?

In that case (very long path), I would probably use a variable containing LoadingBrick. Without using a variable, that statement would be quite long even if you used your preferred way.

workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position -= Vector3.new(0, workspace.GameFolder.Parts.Lobby.Bricks.LoadingBrick.Position.Y - y, 0)

This is really useful for new scripters, so I applaud you. But the big problem is there’s alot more to Vectors that you could’ve included aswell. Like Camera Movement or Scaling/Size. Though, there’s likely already other tutorials out there for that stuff so if they’d need it they could find it. Overall, I’d say it’s a good starting place for newer scripters that want to learn more about Vectors. :+1:

The whole point of this is to help more of the beginner scripters. This isn’t an entire documentary on Vectors.

You should teach the first year uni’s intro to linear alg :clap:

isn’t this common sense ???

maybe not for newer programmers???

just use the roblox developer docs you forumcel

I mentioned things that beginners would likely want to know aswell. I purposely didn’t include complicated things because I meant that you should’ve include more basics for beginners.