Returns a random float between 0 and 1 (inclusive)
Parameter
Type
Description
local randomNumber = Space.Math.Random();Space.Log(randomNumber);-- prints 0.689094245433807
RandomRange
static float RandomRange (float min, float max)
Returns a random float between min and max (inclusive)
Parameter
Type
Description
local min =100.0;local max =500.0;local randomNumber = Space.Math.RandomRange(min, max );Space.Log(randomNumber);-- prints 0206.659149169922
RandomInteger
static int RandomInteger (int min, int max)
Returns a random float between min (inclusive) and max (exclusive)
Parameter
Type
Description
local min =50;local max =75;local randomInteger = Space.Math.RandomInteger(min, max);Space.Log(randomInteger);-- prints 52
Abs
static float Abs (float v)
static int Abs (int v)
Returns the absolute value of 'val'
Parameter
Type
Description
local value =-4.2local absoluteNum = Space.Math.Abs(value);Space.Log(absoluteNum);-- prints 4 [BUG] should be 4.2
Acos
static float Acos (float v)
Returns the arc cosine value of 'val'
Parameter
Type
Description
local value =0.5;local arcCosine = Space.Math.Acos(value);Space.Log(arcCosine);-- prints 1.04719758033752 (radians) which is 60 degrees
Approximately
static bool Approximately (float a, float b)
True if the difference between a and b is less than epsilon
Parameter
Type
Description
local a =5.0;local b =5;local approx1 = Space.Math.Approximately(a, b);Space.Log(approx1);-- prints truelocal a =5.01;local b =5.0;local approx2 = Space.Math.Approximately(a, b);Space.Log(approx2);-- prints falselocal a =5.01;local b =5.0;local approx3 = Space.Math.Approximately(a, b);Space.Log(approx3);-- prints false
Asin
static float Asin (float v)
Returns the arc sine value of 'val'
Parameter
Type
Description
local value =0.5;local arcSine = Space.Math.Asin(value);Space.Log(arcSine);-- prints 0.523598790168762 (radians) which is 30 degrees
Atan
static float Atan (float v)
Returns the arc tangent value of 'val'
Parameter
Type
Description
local value =1.732050808;local arcTangent = Space.Math.Atan(value);Space.Log(arcTangent);-- prints 1.04719758033752 (radians) which is 60 degrees
Atan2
static float Atan2 (float y, float x)
Returns the arc tangent of y/x
Parameter
Type
Description
local x =0.5;local y =0.5;local arcTangent = Space.Math.Atan2(y, x);Space.Log(arcTangent);-- prints 0.785398185253143
Ceil
static int Ceil (float v)
Returns the ceil value of 'val' as an integer
Parameter
Type
Description
Space.Log(Space.Math.Ceil(4.0));-- prints 4Space.Log(Space.Math.Ceil(4.2));-- prints 5Space.Log(Space.Math.Ceil(-4.2));-- prints -4
local sliderStart =5.0;local sliderStop =10.0;local deltaChange =1.0;local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);Space.Log(currentSliderPos);-- prints 6 (moves forward 1)local sliderStart =5.0;local sliderStop =10.0;local deltaChange =7.0;local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);Space.Log(currentSliderPos); -- prints 10 (caps out at at the target value)-- NOTE: Use negative delta to move away from target.
Move angle value to target, but by no more than delta
Parameter
Type
Description
local sliderStartAngle = Space.Math.Pi/2; -- 90 degreeslocal sliderStopAngle = Space.Math.Pi; -- 180 degreeslocal deltaChange = Space.Math.Pi/6; -- 30 degreeslocal currentSliderPos = Space.Math.MoveTowardsAngle(sliderStartAngle, sliderStopAngle, deltaChange);Space.Log(currentSliderPos);-- prints 2.09439516067505 (moves forward 30 degrees to 120 degrees)-- NOTE: Add 180 degrees (Space.Math.Pi) to move away from target.
NextPowerOfTwo
static int NextPowerOfTwo (int v)
Return the next power of two larger or equal to val
Parameter
Type
Description
Space.Log(Space.Math.NextPowerOfTwo(16));-- prints 16Space.Log(Space.Math.NextPowerOfTwo(17));-- prints 32
PerlinNoise
static float PerlinNoise (float x, float y)
Return 2D Perlin noise for coordinates x and y
Parameter
Type
Description
-- Lua Translation of Unity C# Documentationlocal ball = Space.Host.ExecutingObject;ball.SubscribeToEvents();-- animates this object to move upwards on the y axis with slight random movementlocalanimateBall=function()local heightScale =0.1; -- controls speed of movementlocal xScale =1.0; -- shifts x position on perlin noise planelocal height = heightScale * Space.Math.PerlinNoise(Space.Time * xScale, 0.0);local pos = ball.LocalPosition; pos.y = pos.y + height; ball.LocalPosition = pos; Space.Log(pos.y);endball.OnUpdate(animateBall);-- NOTE: If you need a guaranteed range of [0,1], make sure to clamp the value with Space.Math.Clamp01.
PingPong
static float PingPong (float t, float length)
Return a value between 0 and length that oscillates upwards and back based on the position of 'val'
Parameter
Type
Description
--[[ In this example, if you traced the output of Space.Math.PingPong(value, length),the return values would cycle forward through the range [0,5] then cycle backwards through the range [5,0] in order. --]]local ball = Space.Host.ExecutingObject;local originalPos = ball.LocalPosition;ball.SubscribeToEvents();-- The ball object oscillates back and forth on the x-axis.localanimateBall=function()local value = Space.Time;local length =5.0;local newPos = Vector.New(Space.Math.PingPong(value, length) + originalPos.x, originalPos.y, originalPos.z); ball.LocalPosition = newPos;endball.OnUpdate(animateBall);
Pow
static float Pow (float value, float pow)
Return x raised to y power
Parameter
Type
Description
Space.Log(Space.Math.Pow(2, 4));-- prints 16
Repeat
static float Repeat (float value, float length)
Return a value between 0 and length that returns to 0 after exceeding length based on 'val'
Parameter
Type
Description
--[[ In this example, if you traced the output of Space.Math.Repeat(value, length),the return values would cycle forward through the range [0,5]. The cycle is repeated again from 0.--]]local ball = Space.Host.ExecutingObject;local originalPos = ball.LocalPosition;ball.SubscribeToEvents();-- The ball object moves forward on the x-axis.-- Then, it repeats the same motion again from the beginning.localanimateBall=function()local value = Space.Time;local length =5.0;local newPos = Vector.New(Space.Math.Repeat(value, length) + originalPos.x, originalPos.y, originalPos.z); ball.LocalPosition = newPos;endball.OnUpdate(animateBall);
Round
static int Round (float value)
Returns the nearest integer value to val
Parameter
Type
Description
Space.Log(Space.Math.Round(4.0));-- prints 4Space.Log(Space.Math.Round(4.2));-- prints 4Space.Log(Space.Math.Round(4.5));-- prints 4Space.Log(Space.Math.Round(4.55));-- prints 5Space.Log(Space.Math.Round(4.8));-- prints 5Space.Log(Space.Math.Round(-4.2));-- prints -4Space.Log(Space.Math.Round(-4.8));-- prints -5
Sign
static float Sign (float value)
Returns either 1 or -1 based on the sign of 'val'
Parameter
Type
Description
Space.Log(Space.Math.Sign(4.2));-- prints 1Space.Log(Space.Math.Sign(-4.2));-- prints -1
Sin
static float Sin (float value)
Returns the sine of val
Parameter
Type
Description
local angle =1.04719758033752; -- converts to 60 degreeslocal sine = Space.Math.Sin(angle);Space.Log(sine);-- prints 0.866025447845459local angle2 = Space.Math.Pi/3; -- converts to 60 degreeslocal sine2 = Space.Math.Sin(angle2);Space.Log(sine2);-- prints 0.866025447845459
SmoothStep
static float SmoothStep (float from, float to, float t)
Similar to Lerp but moves slowly closer to the edges ('Spherical Lerp')
Parameter
Type
Description
-- Lua Translation of Unity C# Documentation--[[ In this example, Space.Math.SmoothStep(min, max, time) returns values in the range [1,10] in order.As values approach 10, the interpolation slows down. --]]local ball = Space.Host.ExecutingObject;local originalPos = ball.LocalPosition;local min =1.0;local max =10.0;local duration =5.0; --lower value to speed up; raise to slow downlocal startTime;ball.SubscribeToEvents();localinitStartTime=function() startTime = Space.Time;end-- The ball jumps to min + originalPos, then it moves towards-- max + originalPos while slowing down at the end.localanimateBall=function()local time = (Space.Time - startTime) / duration;local newPos = Vector.New(Space.Math.SmoothStep(min, max, time)+ originalPos.x, originalPos.y, originalPos.z); ball.LocalPosition = newPos;endball.OnStart(initStartTime);ball.OnUpdate(animateBall);
Sqrt
static float Sqrt (float value)
Returns the square root of val
Parameter
Type
Description
Space.Log(Space.Math.Sqrt(16));-- prints 4Space.Log(Space.Math.Sqrt(-16));-- prints NaN
Tan
static float Tan (float value)
Returns the tangent value of 'val'
Parameter
Type
Description
local angle =1.04719758033752; -- converts to 60 degreeslocal tangent = Space.Math.Tan(angle);Space.Log(tangent);-- prints 1.73205089569092local angle2 = Space.Math.Pi/3; -- converts to 60 degreeslocal tangent2 = Space.Math.Tan(angle2);Space.Log(tangent2);-- prints 1.73205089569092
local angle =1.04719758033752; -- converts to 60 degreeslocal tangent = Space.Math.Tan(angle);Space.Log(tangent);-- prints 1.73205089569092local angle2 = Space.Math.Pi/3; -- converts to 60 degreeslocal tangent2 = Space.Math.Tan(angle2);Space.Log(tangent2);-- prints 1.73205089569092
local angle =1.04719758033752; -- converts to 60 degreeslocal tangent = Space.Math.Tan(angle);Space.Log(tangent);-- prints 1.73205089569092local angle2 = Space.Math.Pi/3; -- converts to 60 degreeslocal tangent2 = Space.Math.Tan(angle2);Space.Log(tangent2);-- prints 1.73205089569092