Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When do you use Vector3 and Vector3.new?

Asked by 4 years ago

When do you use Vector3 and Vector3.new?

2 answers

Log in to vote
0
Answered by 4 years ago

It depends on the situation of which you're using Vector3 values.

If, for example, you want to assign a part's Position to another part's Position, use the target's Vector3:

local part1 = workspace.Part1
local part2 = workspace.Part2
game.Loaded:Wait()
if part1 and part2 then
    part1.Position = part2.Position
end

The Vector3 constructor can also be used here:

part1.Position = Vector3.new(part2.Position)

Vector3.new() creates a new 3D vector and this new vector is saved if assigned to a variable. Vector3 itself is a datatype representing a 4x1 matrix. The first 3 values are x, y, and z, respectively. The last value is the translation value, and this is usually -1.

When do I use Vector3 or Vector3.new() then?

If you want to assign a Vector3 value to another existing Vector3 value, simply assign it to that value instead of using the constructor (although the constructor may also be used).

If you want to assign a Vector3 value to a Vector3 value that doesn't exist, use the Vector3 constructor.

Ad
Log in to vote
0
Answered by 4 years ago

Vector3 is the name of the class that we use when storing a 3d vector. A class, refers to the 'type of data' that makes up a variable. Examples include CFrame, UDim2, Vector2.

local p = Vector3.new(0,0,0) creates a variable, named p, that stores that Vector3. You would never use just Vector3 in your code, and if you try this you'll find you get an error. Instead, Vector3 is just the term for the type of data that you are storing in variable p.

Answer this question