API:Vector3

(Redirected from Vector3)
< Global namespace | Roblox namespace

A Vector3 is a userdata that holds a coordinate in 3D space, described by three numbers. These numbers represent the X, Y, and Z coordinates. They can be used to hold positions (Position), sizes (Size), speeds (Velocity), and more!

Quick Reference

Constructors
SyntaxDescription
Vector3.new()Creates a new Vector3 with X, Y, and Z values of 0.
Vector3.new(number x, number y, number z)Creates a new Vector3 with the X, Y, and Z values specified.
Vector3.FromNormalId(NormalId norm)Creates a unit Vector3 in a particular facing direction.
Vector3.FromAxis(Axis axis)Creates a unit Vector3 for a particular Axis.
Functions
SyntaxDescription
Vector3 Vector3:Lerp(Vector3 goal, number alpha)Returns a Vector3 linearly interpolated between the original value and goal.
number Vector3:Dot(Vector3 other)Returns the scalar dot product of the two vectors.
Vector3 Vector3:Cross(Vector3 other)Returns the vector cross product of the two vectors.
bool Vector3:isClose(Vector3 other, number epsilon)Returns true if all three axis values of the given Vector3 are within the given epsilon, false otherwise.
Properties
SyntaxDescription
number X The x-coordinate.
number Y The y-coordinate.
number Z The z-coordinate.
Vector3 unit A normalized copy of the vector with the same direction as the original, but a magnitude of 1.
number magnitude The length of the vector, or the distance between the point and the origin (0, 0, 0).
Operators
SyntaxDescription
Vector3 + Vector3Returns a new Vector3 with each component of the second added to the corresponding component of the first.
Vector3 - Vector3Returns a new Vector3 with each component of the second subtracted from the corresponding component of the first.
Vector3 * Vector3Returns a new Vector3 with each component of the second multiplied the corresponding component of the first.
Vector3 / Vector3Returns a new Vector3 with each component of the first divided by the corresponding component of the second.
Vector3 * numberReturns a new Vector3 with each component multiplied by the number.
Vector3 / numberReturns a new Vector3 with each component divided by the number.
number * Vector3Returns a new Vector3 with each component multiplied by the number.
number / Vector3Returns a new Vector3 with each component divided by the number.

Constructors

Vector3.new

Vector3 Vector3.new()

Description:
Creates a new Vector3 with X, Y, and Z values of 0.

Vector3 Vector3.new(number x, number y, number z)

Description:
Creates a new Vector3 with the X, Y, and Z values specified.

Vector3.FromNormalId

Vector3 Vector3.FromNormalId(NormalId norm)

Description:
Creates a unit Vector3 in a particular facing direction.

Vector3.FromAxis

Vector3 Vector3.FromAxis(Axis axis)

Description:
Creates a unit Vector3 for a particular Axis.

Methods

Lerp

Vector3 Vector3:Lerp(Vector3 goal, number alpha)

Description:
Returns a Vector3 linearly interpolated between the original value and goal.

Notes:

  • alpha should be between 0 and 1.

Dot

number Vector3:Dot(Vector3 other)

Description:
Returns the scalar dot product of the two vectors.

Notes:

Cross

Vector3 Vector3:Cross(Vector3 other)

Description:
Returns the vector cross product of the two vectors.

Notes:

isClose

bool Vector3:isClose(Vector3 other, number epsilon)

Description:
Returns true if all three axis values of the given Vector3 are within the given epsilon, false otherwise.

Properties

X

number Vector3.X [readonly]

Description:
The x-coordinate.

Y

number Vector3.Y [readonly]

Description:
The y-coordinate.

Z

number Vector3.Z [readonly]

Description:
The z-coordinate.

unit

Vector3 Vector3.unit [readonly]

Description:
A normalized copy of the vector with the same direction as the original, but a magnitude of 1.

magnitude

number Vector3.magnitude [readonly]

Description:
The length of the vector, or the distance between the point and the origin (0, 0, 0).

Operators

Vector3 + Vector3

Vector3 Vector3 + Vector3

Description:
Returns a new Vector3 with each component of the second added to the corresponding component of the first.

Vector3 - Vector3

Vector3 Vector3 - Vector3

Description:
Returns a new Vector3 with each component of the second subtracted from the corresponding component of the first.

Vector3 * Vector3

Vector3 Vector3 * Vector3

Description:
Returns a new Vector3 with each component of the second multiplied the corresponding component of the first.

Vector3 / Vector3

Vector3 Vector3 / Vector3

Description:
Returns a new Vector3 with each component of the first divided by the corresponding component of the second.

Vector3 * number

Vector3 Vector3 * number

Description:
Returns a new Vector3 with each component multiplied by the number.

Vector3 / number

Vector3 Vector3 / number

Description:
Returns a new Vector3 with each component divided by the number.

number * Vector3

Vector3 number * Vector3

Description:
Returns a new Vector3 with each component multiplied by the number.

number / Vector3

Vector3 number / Vector3

Description:
Returns a new Vector3 with each component divided by the number.

Using Vector3s

Moving things around

Open up a new place with a part.

In the command bar, type in this bit here and hit enter:

workspace.Part.Position = Vector3.new(0, 50, 0)

You should see that the brick moved up a good distance, you may need to move the camera to see it. What you just did is changed where the brick is, by changing it's Position. You set Part.Position to a new location by using the Vector3.new constructor. You constructed a new Vector3 using 3 different values. This told the Lua engine to set the brick's Position to 0, 50, 0, making the brick move to that position.

Moving things around with the Position property comes with built-in collision detection. Let's say you have a huge brick, and you try to move another brick inside of it. Instead of appearing inside of the solid brick, the second brick will pop up on top of the large one, right above where it's trying to get to. If you don't want this to happen, you can move objects around with the CFrame property instead.

See CFrame for additional Vector3 operators.

See also

Referenced by