+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 (16 points)

1 Answer

0 votes

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

extends KinematicBody2D

const INITIAL_DISTANCE = 100
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 (592 points)
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.