math | CraytaKit
Skip to main content

math

Class Functions

Class Function NameReturn TypeDescriptionTags
math.lerp(number a, number b, number t)numberThe interpolated float result between the two float valuesNone
math.sign(number a)numberReturns 1 if value is position, -1 if value is negativeNone
math.copysign(number a, number b)numberCopies the sign from b onto aNone
math.clamp(number a, number min, number max)numberClamps the given value between the given minimum float and maximum float values. Returns the given value if it is within the min and max rangeNone

Examples

lerp

Linearly interpolates between a and b by t.

The parameter t is not clamped to the range [0, 1] and is effectively the same as a + (b - a) * t

When t = 0 returns a. When t = 1 return b. When t = 0.5 returns the midpoint of a and b.

function ExampleScript:Lerp()
local a = 0
local b = 100

print(math.lerp(a, b, -0.5)) -- prints -50
print(math.lerp(a, b, 0)) -- prints 0
print(math.lerp(a, b, 0.5)) -- prints 50
print(math.lerp(a, b, 1)) -- prints 100
print(math.lerp(a, b, 1.5)) -- prints 150
end

sign

function ExampleScript:Sign()
local a = 100
local b = -25
print(math.sign(a)) -- prints 1
print(math.sign(b)) -- prints -1
end

copysign

function ExampleScript:CopySign()
local a = 100
local b = -25
local c = math.copysign(a, b)
print(c) -- prints -100
end

clamp

function ExampleScript:Heal(amt)
self.health = self.health + amt
self.health = math.clamp(self.health, 1, 100) -- Ensure we stay within 1 and 100 health
end
© 2021 CraytaKit