0 votes

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
            accelerationTimer = 0
            velocity.x = lerp(velocity.x, 0, friction)
Godot version 3.3.2
in Engine by (139 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.