Player acceleration with a curve?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Wurzelpilz

I would love to use a curve for acceleration but there is so little information about this topic. I wrote a code but it looks extremely nasty… works somehow, with one hiccup. Sometimes it does not reset to zero if I switch the direction… any improvement tips?

var speed : float = 810
var dir : int = 0
export var accelerationCurve : Curve
var acceleration : float = .07
var accelerationTimer : float = 0.0
var rate : float = .1

    func _physics_process(_delta):
    	
    	dir = 0
    	
    	if Input.get_action_strength("right"):
    		accelerationTimer += acceleration * rate
    		dir += 1	
    	if Input.get_action_strength("left"):
    		accelerationTimer += acceleration * rate
    		dir -= 1
    	
    	accelerationTimer = clamp(accelerationTimer, 0, 1)
    	
    	
    	if dir != 0:
    		velocity.x = accelerationCurve.interpolate(accelerationTimer) * speed * dir
    	else:
    		accelerationTimer = 0
    		velocity.x = lerp(velocity.x, 0, friction)

I would also love to know this, people say that we should use curves for stuff like this but then there’s the lack of documentation for that haha. Does someone have a tutorial? one that uses kinematic body 2d’s move and slide

goodymind | 2022-07-20 11:16