Skip to content

Vector3

Client-side
Server-side
Shared

Element category: Vector

Represents a 3D Vector. Used for storing and manipulating three-dimensional coordinates (x, y, z).

OOP-Only Methods

create

Default constructor for the Vector3 class. Returns a Vector3 object.

Vector3(mixed vectorOrX[, float y, float z])
  • vectorOrX: float | table | vector3 – Vector3, table, or floats indicating vector's coordinates
  • y: float – If vectorOrX is a float, this is the Y coordinate
  • z: float – If vectorOrX is a float, this is the Z coordinate
cross

Calculates the cross product of two vectors. The result is a vector orthogonal to both.

vector3 Vector3:cross ( vector3 vector )
  • vector: Vector3 – Vector to calculate the cross product with.
dot

Calculates the dot (scalar) product of two vectors.

float Vector3:dot ( vector3 vector )
  • vector: Vector3 – Vector to calculate the dot product with.
getX

Returns the X component of the vector.

float Vector3:getX ( )
setX

Sets the X component of the vector.

void Vector3:setX ( float x )
  • x: float – The new X value.
getY

Returns the Y component of the vector.

float Vector3:getY ( )
setY

Sets the Y component of the vector.

void Vector3:setY ( float y )
  • y: float – The new Y value.
getZ

Returns the Z component of the vector.

float Vector3:getZ ( )
setZ

Sets the Z component of the vector.

void Vector3:setZ ( float z )
  • z: float – The new Z value.
normalize

Normalizes the vector to a unit vector (length of 1). Modifies the original vector.

bool Vector3:normalize ( )
getNormalized

Returns a normalized version of the vector without modifying the original.

vector3 Vector3:getNormalized ( )
getLength

Returns the length (magnitude) of the vector.

float Vector3:getLength ( )
getSquaredLength

Returns the squared length of the vector (useful to avoid square root operations).

float Vector3:getSquaredLength ( )
intersectsSegmentTriangle

Determines if a line segment intersects with a triangle.

vector3 Vector3:intersectsSegmentTriangle ( vector3 segmentDir, vector3 triVert0, vector3 triVert1, vector3 triVert2 )
  • segmentDir: Vector3 – The direction vector of the segment.
  • triVert0: Vector3 – First vertex of the triangle.
  • triVert1: Vector3 – Second vertex of the triangle.
  • triVert2: Vector3 – Third vertex of the triangle.
intersectsSegmentTriangle (static)

Static version of the segment-triangle intersection method.

vector3 Vector3.intersectsSegmentTriangle ( vector3 origin, vector3 segmentDir, vector3 triVert0, vector3 triVert1, vector3 triVert2 )
  • origin: Vector3 – Origin point of the segment.
  • segmentDir: Vector3 – Direction of the segment.
  • triVert0: Vector3 – First triangle vertex.
  • triVert1: Vector3 – Second triangle vertex.
  • triVert2: Vector3 – Third triangle vertex.

Code Examples

shared

This example sorts all players in a nice line on the center of the map.

local players = getElementsByType("player")
local newPlayerPosition = Vector3(-#players - 1, 0, 10) -- Initialize the position vector for the first player in the list
for _, player in ipairs(players) do
-- Move each player 1 unit forward in X from the previous one
newPlayerPosition.x = newPlayerPosition.x + 1
setElementPosition(player, newPlayerPosition)
end

See Also

Articles