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 aVector3
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