I am using lerp function to make smooth movement in my game, but after player releases the button it is decreasing to 0 for 3 seconds, is there a way to equalize it to 0 after it reaches 0.01?

I've tried this method, but it just isn't working

vel.x = lerp(vel.x, dir.x * accseleration, 0.2)
 vel.z = lerp(vel.z, dir.z * accseleration, 0.2)
 if vel.x <= 0.01:
	vel.x == 0

also I've tried rounding it to 0.01 but just stops at 0,05 and stays like that until player presses a button again

  • Kojack replied to this.
  • If this is analog stick, then you are probably dealing with a dead zone. It might never be zero. You could do something like this:

    vel.x = lerp(vel.x, dir.x * accseleration, 0.2)
    vel.z = lerp(vel.z, dir.z * accseleration, 0.2)
    vel *= 0.995

    Which will add a sort of fake friction. You can also check if dir.length_squared() is less than 0.1 or something and only apply the friction in that case.

    If this is analog stick, then you are probably dealing with a dead zone. It might never be zero. You could do something like this:

    vel.x = lerp(vel.x, dir.x * accseleration, 0.2)
    vel.z = lerp(vel.z, dir.z * accseleration, 0.2)
    vel *= 0.995

    Which will add a sort of fake friction. You can also check if dir.length_squared() is less than 0.1 or something and only apply the friction in that case.

    w1n7er2171
    Your vel.x == 0 should be vel.x = 0
    Double equals compares without changing, single equals assigns.

    Also, the if line will block all velocities to the left.
    Try this:

    if abs(vel.x) <= 0.01:
    	vel.x = 0

    The abs(vel.x) gives the absolute value of vel.x, so negatives are turned positive. That way any value between -0.01 and 0.01 are clamped to 0.