0 votes

Greets!

I got this code, thanks to @Bartosz:

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

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

        $Tween.remove_all()
        $Tween.interpolate_property(
            $sprite, "global_position", $sprite.global_position, get_global_mouse_position(), 
            distance,Tween.TRANS_LINEAR,Tween.EASE_IN
            )
        $Tween.start()
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.