Math.clamp with walkspeed

I am trying to use math.clamp to achieve the following:

The walkspeed of my NPC increases over time between two values.

  • Basic overview:
    MinimumWS = 35
    MaximumWS = 65
    MaxTime = 60
    It will reach maximumWS if it has been over MaxTime

      			--config.ForgetTime = 60
      			--tick() - charTicks[tChar] = % has been since it started chasing this úser
      			--config.RunSpeedMax = max speed
      			--config.RunSpeedMin = min speed
      			
      			local WSC = (tick() - charTicks[tChar]) / config.ForgetTime
      			WSC = ? -- what do I put here?
      			WSC = math.clamp(config.RunSpeedMax - WSC,config.RunSpeedMin,config.RunSpeedMax)
      			hum.WalkSpeed = WSC
    

Not really sure how to use it in this, as I usually just use it with 0 and 1.

I would do a linear interpolation between RunSpeedMin and RunSpeedMax.

function lerp(a, b, t)
    return a + (b - a) * t
end

local t = math.clamp((tick() - charTicks[tChar]) / config.ForgetTime, 0, 1)
hum.WalkSpeed = lerp(config.RunSpeedMin, config.RunSpeedMax, t)
1 Like