Core has special types of data structures for vectors. The types have useful constructors, properties, and functions that can be found using the Core API page.
Vector Dimensions
In Core, there are three different vector types: Vector2, Vector3, and Vector4. The numbers correspond to the amount of components each vector holds.
Constructors
A vector can be created using a special function named constructor. There are many different options for the parameter values that can be passed into a constructor function to assign the components a starting value.
Here are examples to create each type of vector:
--Create a Vector2 with the values (0, 0)local v2 = Vector2.New()
--Create a Vector3 with the values (40, 0, 100)local v3 = Vector3.New(40, 0, 100)
--Create a Vector4 with the values (40, 0, 100, 85)local v4 = Vector4.New(v3, 85)Code language:Lua(lua)
Constants
The vectors also have useful presets for quickly creating vectors with common component values. These are known as constants.
Here are some examples using constants to create vectors:
--Create a Vector 2with the values (0, 0)
local v2 = Vector2.ZERO
--Create a Vector3 with the values (1, 0, 0)
local v3 = Vector3.FORWARD
--Create a Vector 4with the values (1, 1, 1, 1)
local v4 = Vector4.ONECode language:JavaScript(javascript)
Properties
Once a vector is created then it will have property values available to be accessed. These include the components of the vector and the size of the vector.
Here are a couple of examples of accessing the properties of a Vector3:
--Create a Vector3 with the values (30, 500, -1)
local v3 = Vector3.New(30, 500, -1)
--print the y component
print(v3.y)
--print the size of the vector
print(v3.size)Code language:PHP(php)
Functions
A vector has functions that can perform actions on the vector.
A common example is to normalize a vector, like so:
----Create a Vector3 with the values (30, 500, -1)
local v3 = Vector3.New(30, 500, -1)
--Create the normalized vector of v3
local normalizedV3 = v3:GetNormalized()Code language:JavaScript(javascript)
Operators
Math can be done on vectors, similar to numbers. The API will show the correct syntax to perform a certain operation. It’s important to keep the order of operands in mind for some operations.
Here are some common examples:
--Add two vectors to make a sum vector
local sum = Vector3.UP + Vector3.RIGHT
print(sum)
--(0, 1, 1)
--Subtract two vector to make an offset vector
local offset = Vector2.New(20, 10) - Vector2.New(1, 5)
print(offset) --(19, 5)
--Multiply a vector by a number
local scaledVector = Vector3.New(0, 1, 2) * 100print(scaledVector) --(0, 100, 200)
--Get the dot product of two vectors
local dotProduct = Vector3.UP .. Vector3.RIGHT
print(dotProduct) --0Code language:PHP(php)
Lerp
Lerp is short for linear interpolation. For Core vectors, it’s a class function that can transition from one vector to another.
This can be useful to move an object between two position vectors, such as in the video below:
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!