Skip to content

Vector3

Element category: Vector

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

OOP-Only Methods and Variables

create

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

Vector3 create ( ​float|​table|​Vector3 vectorOrX, [ ​float y = 0, ​float z = 0 ] )
  • vectorOrX: Vector3, table, or floats indicating vector's coordinates.
  • y: If vectorOrX is a float, this is the Y coordinate.
  • z: 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|false cross ( ​Vector3 vectorA, ​Vector3 vectorB )
  • vectorA: Source vector.
  • vectorB: Vector to calculate the cross product with.
dot

Calculates the dot (scalar) product of two vectors.

float|false dot ( ​Vector3 vectorA, ​Vector3 vectorB )
  • vectorA: Source vector.
  • vectorB: Vector to calculate the dot product with.
getX

Returns the X component of the vector.

float|false getX ( ​Vector3 vector )
  • vector: Vector3 to get X coordinate from.
Variable: .x
setX

Sets the X component of the vector.

bool setX ( ​Vector3 vector, ​float x )
  • vector: Vector3 to set X coordinate on.
  • x: The new X value.
Variable: .x
getY

Returns the Y component of the vector.

float|false getY ( ​Vector3 vector )
  • vector: Vector3 to get Y coordinate from.
Variable: .y
setY

Sets the Y component of the vector.

bool setY ( ​Vector3 vector, ​float y )
  • vector: Vector3 to set Y coordinate on.
  • y: The new Y value.
Variable: .y
getZ

Returns the Z component of the vector.

float|false getZ ( ​Vector3 vector )
  • vector: Vector3 to get Z coordinate from.
Variable: .z
setZ

Sets the Z component of the vector.

bool setZ ( ​Vector3 vector, ​float z )
  • vector: Vector3 to set Z coordinate on.
  • z: The new Z value.
Variable: .z
normalize

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

bool normalize ( ​Vector3 vector )
  • vector: Vector3 to normalize.
getNormalized

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

Vector3|false getNormalized ( ​Vector3 vector )
  • vector: Vector3 to get normalized version of.
Variable: .normalized
getLength

Returns the length (magnitude) of the vector.

float|false getLength ( ​Vector3 vector )
  • vector: Vector3 to get length from.
Variable: .length
getSquaredLength

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

float|false getSquaredLength ( ​Vector3 vector )
  • vector: Vector3 to get squared length from.
Variable: .squaredLength
intersectsSegmentTriangle

Determines if a line segment intersects with a triangle.

Vector3|false intersectsSegmentTriangle ( ​Vector3 origin, ​Vector3 segmentDir, ​Vector3 triVert0, ​Vector3 triVert1, ​Vector3 triVert2 )
  • origin: Origin point of the segment.
  • segmentDir: The direction vector of the segment.
  • triVert0: First vertex of the triangle.
  • triVert1: Second vertex of the triangle.
  • triVert2: Third vertex of the triangle.

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
-- OR
--newPlayerPosition:setX(newPlayerPosition.x + 1)
-- OR
--Vector3.setX(newPlayerPosition, newPlayerPosition.x + 1)
setElementPosition(player, newPlayerPosition)
end

See Also

Vector Elements
Reference