From Garry's Mod
Gradually approaches the target value by the specified amount.
The value we're currently at.
The target value. This function will never overshoot this value.
The amount that the current value is allowed to change by to approach the target. (It makes no difference whether this is positive or negative.)
New current value, closer to the target than it was previously.
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
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