All Questions

Community

G
Greaby

Another solution for acceleration/decceleration

I used the lerp function to do acceleration and decceleration.

I can easily play with the friction variables, for example if the player is on an ice floor.

var air_friction: = .04 
var ground_friction: = .4 
var acceleration_force: = .6 

# idle physics
new_velocity.x = lerp(velocity.x, 0, owner.ground_friction)

#walk physics
new_velocity.x = lerp(velocity.x, input_direction.x * owner.max_speed, owner.acceleration_force)
  • Nathan Lovato replied
    Nice! Note that if you want precise controls and great game feel, you'll want to avoid using lerp for movement the way you're doing as the ratio of acceleration or deceleration changes every frame and the character will never reach its minimum or maximum speed this way. You also want to use the delta value to account for time variations between frames: `owner.ground_friction * delta` for the lerp factor in the first case. Also, when you interpolate with a constantly evolving value with just delta * factor, the interpolation amount becomes dependent on the framerate if you use the _process callback. In this case, you'd want to use 1 - factor ^ delta instead.