0 votes


I got this code, thanks to @Bartosz:

extends Node2D
func input(event):
if event is InputEventMouseButton and event.button
index == BUTTONLEFT and event.pressed:
property($sprite, "globalposition",$sprite.globalposition, getglobalmouseposition(), 1,Tween.TRANSLINEAR,Tween.EASE_IN)

to make my sprite/player move toward a mouse click. But, trouble is, it's movin faster the further the click is, and i want it to be at the same speed, wherever the mouse click.
What should i add in the script please?

in Engine by (321 points)

1 Answer

0 votes

You have to make the tween speed dependent on the distance between the mouse and the sprite. This is one way:

extends Node2D
var vel=200
func _input(event):

    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        var distance=get_global_mouse_position().distance_to($sprite.global_position) / vel

            $sprite, "global_position", $sprite.global_position, get_global_mouse_position(), 
by (2,060 points)

That works perfectly! Cheeers! :)

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.
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 webmaster@godotengine.org with your username.