Godot Version
Godot Engine v4.2.1.stable.official.b09f793f5
Question
Hey there everybody. I’m trying to make topdown movement work with acceleration and deceleration. Unfortunately I’m extremely new and even a simple mechanic for moving turned out to be a million lines long. My questions are:
-
How can I write this information in a better, shorter way?
-
Because of the way I set up the movement in the beginning, it makes the character stop instantly when I stop pressing the key. How do I get around that?
-
I want the character to slide before stopping completely. How should I approach coding that?
Thanks in advance.
func _process(delta):
position.x -= currentSPEED * delta
position.x += currentSPEED * delta
position.y -= currentSPEED * delta
position.y += currentSPEED * delta
if Input.is_action_pressed("ui_left"):
if velocity.length() > 1:
velocity.normalized()
currentSPEED += Acceleration * delta
position.x -= currentSPEED * delta
if currentSPEED > maxSPEED:
currentSPEED = maxSPEED
elif Input.is_anything_pressed() == false :
currentSPEED -= Deceleration * delta
if currentSPEED < 0:
currentSPEED = 0
if Input.is_action_pressed("ui_right"):
if velocity.length() > 1:
velocity.normalized()
currentSPEED += Acceleration * delta
position.x += currentSPEED * delta
if currentSPEED > maxSPEED:
currentSPEED = maxSPEED
elif Input.is_anything_pressed() == false :
currentSPEED -= Deceleration * delta
if currentSPEED < 0:
currentSPEED = 0
if Input.is_action_pressed("ui_up"):
if velocity.length() > 1:
velocity.normalized()
currentSPEED += Acceleration * delta
position.y -= currentSPEED * delta
if currentSPEED > maxSPEED:
currentSPEED = maxSPEED
elif Input.is_anything_pressed() == false :
currentSPEED -= Deceleration * delta
if currentSPEED < 0:
currentSPEED = 0
if Input.is_action_pressed("ui_down"):
if velocity.length() > 1:
velocity.normalized()
currentSPEED += Acceleration * delta
position.y += currentSPEED * delta
if currentSPEED > maxSPEED:
currentSPEED = maxSPEED
elif Input.is_anything_pressed() == false :
currentSPEED -= Deceleration * delta
if currentSPEED < 0:
currentSPEED = 0