Skip to content
Untitled Sandbox - Developer Documentation
docs/api/math

@usbx/math

Vectors, colors, random numbers

local math3d = require("@usbx/math")

math3d.vector3

value

Vector functions and constants. angle is in degrees.

example
local spot = math3d.vector3.new(0, 10, 0) + math3d.vector3.up

Use it forposition, direction and velocity maths.

math3d.vector3.new

function

Makes a vector from x, y and z numbers, where right, up and back are positive.

math3d.vector3.new(x: number, y: number, z: number) -> vector
example
local spot = math3d.vector3.new(0, 10, 0)

Use it forsetting where a part spawns, or the spot a player teleports to.

xRight is positive.
yUp is positive.
zBack is positive.

math3d.vector3.zero

value

Gives the vector 0, 0, 0, which points nowhere and has no length.

math3d.vector3.zero: vector

Use it forresetting a position, or starting a total that you add movements to.

math3d.vector3.up

value

Gives the vector that points straight up, which is the positive y direction.

math3d.vector3.up: vector

Use it forlifting a spawned part above the ground, or launching players into the air from a jump pad.

math3d.vector3.magnitude

function

Gives the length of a vector, which for a velocity is the speed.

math3d.vector3.magnitude(value: vector) -> number
example
local speed = math3d.vector3.magnitude(part.velocity)

Use it forchecking how fast a car or ball is moving, using its velocity.

valueThe vector.

math3d.vector3.normalize

function

Gives a vector that points the same way but has a length of 1.

math3d.vector3.normalize(value: vector) -> vector
example
local direction = math3d.vector3.normalize(player.position - door.position)

Use it forfinding the direction from a cannon to a player, so you can push them that way.

valueThe vector; its length becomes 1.

math3d.vector3.dot

function

Gives the dot product of two vectors, a single number that shows how much they point the same way.

math3d.vector3.dot(a: vector, b: vector) -> number
example
local rising = math3d.vector3.dot(part.velocity, math3d.vector3.up) > 0

Use it forchecking whether a part is rising or falling, or whether something is facing a player.

aA vector.
bAnother vector.

math3d.vector3.cross

function

Gives a vector at right angles to both of the vectors you pass in.

math3d.vector3.cross(a: vector, b: vector) -> vector
example
local side = math3d.vector3.cross(math3d.vector3.up, part.velocity)

Use it forfinding the sideways direction of a moving car or player, like their left or right.

aA vector.
bAnother vector.

math3d.vector3.distance

function

Gives the distance between two points, in studs.

math3d.vector3.distance(a: vector, b: vector) -> number
example
local distance = math3d.vector3.distance(player.position, door.position)

Use it foropening a door when a player gets close, or checking who is nearest to a goal.

aA point.
bAnother point.

math3d.vector3.lerp

function

Gives a point between two points, where alpha 0 is the start and 1 is the end.

math3d.vector3.lerp(a: vector, b: vector, alpha: number) -> vector
example
local halfway = math3d.vector3.lerp(player.position, door.position, 0.5)

Use it forplacing a marker halfway between two checkpoints, or moving something along a path step by step.

aThe start.
bThe end.
alpha0 gives a, 1 gives b, 0.5 the middle.

math3d.vector3.angle

function

Gives the angle between two vectors in degrees, so 0 means they point the same way.

math3d.vector3.angle(a: vector, b: vector) -> number
example
local angle = math3d.vector3.angle(part.velocity, math3d.vector3.up)

Use it forchecking whether a ramp is too steep to climb, or how far off target a shot is.

aA vector.
bAnother vector.

math3d.vector3.rotate

function

Turns a vector by a rotation in degrees, the same way an instance's rotation turns it.

math3d.vector3.rotate(value: vector, rotation: vector) -> vector
example
local front = math3d.vector3.rotate(math3d.vector3.new(0, 0, -1), door.rotation)

Use it forfinding which way a door or turret is facing, from its rotation.

valueThe vector to turn.
rotationThe rotation in degrees.

math3d.color

value

Color constructors. rgb rounds and clamps each channel to 0 to 255. hex takes "#RRGGBB" and needs the #.

example
door.color = math3d.color.hex("#FFAA00")

Use it forpainting parts, lights and text in the colors you want, or blending one color into another.

math3d.color.rgb

function

Makes a color from red, green and blue amounts, each from 0 to 255.

math3d.color.rgb(r: number, g: number, b: number) -> Color
example
door.color = math3d.color.rgb(255, 0, 0)

Use it forturning a button red when it is pressed, and green again when it is ready.

rRed, 0 to 255.
gGreen, 0 to 255.
bBlue, 0 to 255.

math3d.color.hex

function

Makes a color from text like "#FFAA00", which must start with the #.

math3d.color.hex(text: string) -> Color
example
door.color = math3d.color.hex("#FFAA00")

Use it forusing a color code you copied from a design tool or a palette website.

text"#RRGGBB", with the #.

math3d.color.hsv

function

Makes a color from a hue in degrees, plus a saturation and value from 0 to 1.

math3d.color.hsv(hue: number, saturation: number, value: number) -> Color
example
door.color = math3d.color.hsv(120, 1, 1)

Use it forcycling a disco floor through the rainbow by stepping the hue around the wheel.

hueDegrees, 0 to 360.
saturation0 (grey) to 1.
value0 (black) to 1.

math3d.color.lerp

function

Blends two colors, where alpha 0 gives the first and 1 gives the second.

math3d.color.lerp(a: Color, b: Color, alpha: number) -> Color
example
door.color = math3d.color.lerp(math3d.color.rgb(255, 0, 0), math3d.color.rgb(0, 0, 255), 0.5)

Use it forfading a health bar from green to red as the player loses health.

aThe start.
bThe end.
alpha0 gives a, 1 gives b, 0.5 the middle.

math3d.look_at

function

The rotation in degrees that turns the front (-Z) of an instance at from toward to.

math3d.look_at(from: vector, to: vector) -> vector
example
door.rotation = math3d.look_at(door.position, player.position)

Use it forturning a turret, camera prop or NPC statue so it faces a player or a point.

fromWhere the instance is.
toThe point it should face.

math3d.random

function

A random number from min up to max, max not included.

math3d.random(min: number, max: number) -> number
example
local speed = math3d.random(10, 20)

Use it forpicking a random speed for a falling block, or a random wait before the next wave.

minThe lowest value.
maxThe value it stays below.

math3d.random_int

function

A random whole number from min to max, both included.

math3d.random_int(min: number, max: number) -> number
example
local roll = math3d.random_int(1, 6)

Use it forrolling a dice, or picking a random map number from the ones you have.

minThe lowest whole number.
maxThe highest whole number.

math3d.random_seed

function

Sets the seed of random and random_int for this script. The same seed gives the same numbers.

math3d.random_seed(seed: number) -> ()

Use it forgetting the same random obby layout every time, which helps while you are testing.

seedAny number.

math3d.random_generator

function

A new random generator of its own, for gacha, loot and shuffles that must be fair and replayable. The same whole-number seed gives the same draws on every server and release; without a seed it starts from the server's own randomness.

math3d.random_generator(seed: number?) -> RandomGenerator
example
local rng = math3d.random_generator(42)

Use it forloot drops or card shuffles that you can replay from a seed.

seedoptionalA whole number. The same seed gives the same draws everywhere; nil starts somewhere no one can predict.

math3d.random_generator_from

function

A generator that goes on exactly where the one whose state() gave this text was. Empty or other text is an error, so a player with no saved state yet starts with random_generator().

math3d.random_generator_from(state: string) -> RandomGenerator
example
local rng = math3d.random_generator_from(profile:get("gacha.rng"))

Use it forrestoring a generator from a saved state, for example after a player rejoins.

stateText from RandomGenerator:state(), such as "pcg32-v1:…".
↑ ↓ moveEnter openEsc close