+1 vote

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

asked May 24 in Projects by LoneDespair (23 points)

1 Answer

0 votes
Best answer

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
answered May 26 by Thomas Karcher (770 points)
selected Aug 23 by LoneDespair

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

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.