+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

in Engine by (408 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

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
by (1,750 points)
selected by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.