How to set continual the speed of a mouse controlled sprite?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl

Greets!

I got this code, thanks to @Bartosz:

extends Node2D
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
$Tween.interpolate_property($sprite, “global_position”,$sprite.global_position, get_global_mouse_position(), 1,Tween.TRANS_LINEAR,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?

:bust_in_silhouette: Reply From: estebanmolca

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()

That works perfectly! Cheeers! :slight_smile:

Syl | 2020-01-18 14:36