Search code examples
spritegodotgdscript

How can I get a 2D sprite to slow to a stop?


I'm trying to create a playable sprite in Godot using GDScript. I've got my character moving left and right and then coming to a halt when no input is being pressed.

However, instead of coming to a dead stop, I want the sprite to slow down. How would I write that?

extends KinematicBody2D

var motion = Vector2()



func _physics_process(delta):

if Input.is_action_pressed("ui_right"):
    motion.x = 250


elif Input.is_action_pressed("ui_left"):
    motion.x = -250

else:
    motion.x = 0

move_and_slide(motion)

pass

Solution

  • Okay I figured it out:

    extends KinematicBody2D
    
    var motion = Vector2()
    
    func _physics_process(delta):
    
        if Input.is_action_pressed("ui_right"):
            motion.x = 250
        
        
        elif Input.is_action_pressed("ui_left"):
            motion.x = -250
        
        else:
            motion.x = motion.x * .9
        
        move_and_slide(motion)