math.Approach
Example
Demonstrates what this function does
print( math.Approach( 0, 5, 1 ) ) -- attempts to increment 0 by 1, 0 + 1 is less than 5 so returns 1
print( math.Approach( 4, 5, 3 ) ) -- attempts to increment 4 by 3, 4 + 3 = 7 is greater than 5 so returns 5
Output:
1
5
Example
Common usage example of this function with a control variable.
local MyNumber = 0
local Target = 0
local LastThink = 0
local ChangeRate = 1
hook.Add( "Think", "math.Approach Example", function()
local now = CurTime()
local timepassed = now - LastThink
LastThink = now
MyNumber = math.Approach( MyNumber, Target, ChangeRate * timepassed )
-- Normally, you would use MyNumber in code that appears here.
end )
-- The following functions are for example only:
function GetMyNumber()
return MyNumber
end
function SetMyNumberTarget( newtarget )
Target = newtarget
end
function SetMyNumberChangeRate( newrate )
ChangeRate = newrate
end