How to make my player move at a certain distance at a specific speed

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

is there a way to make my player move a certain distance when a button is just pressed but at a controlled speed in 2d

thank you so much in advance

:bust_in_silhouette: Reply From: Thomas Karcher

You didn’t specify a direction, but if you simply want to move your player to the right, this should work:

extends KinematicBody2D

var distance = 0
var speed = 80

func _process(delta):
    var step = speed * delta if distance > speed * delta else distance	
    move_and_collide (Vector2 (step, 0))
    distance -= step

func _on_Button_pressed():
    distance = INITIAL_DISTANCE

Thank you so much for your help :slight_smile:
although i ended up like adding a destination coordinates calculated based on players current coordinate and pinning the player when he move pass it

LoneDespair | 2019-08-23 11:41