Movin a sprite toward the mouse click.

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

Greets!
How to do that please?

:bust_in_silhouette: Reply From: Bartosz
  1. on mouse click check if cursor is inside of sprite

  2. mark sprite as dragged e.g. node_responsible_for_draggin.dragged = sprite

  3. in _processmethod set sprites global position to mouse global position sprite.global_position = get_global_mouse_position())

  4. on mouse release unmarked dragged sprite

Update
to move sprite toward mouse click you can use tween and global_mouse_position()
simple script:

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

here demo in my godot-recipes project

Oops, fear that I’ll need more study of script, my actual knowledge bein limited to the copy/paste of some lines… Any tutos that I may check, usin that mouse movement?

Syl | 2018-03-25 19:43

I’m not aware about tutorial covering dragging sprites. here answer how to detect mouse clicking on sprite

Bartosz | 2018-03-25 20:12

Thxs, but I don’t want to click on sprite, juste movin the sprite toward the click.

Syl | 2018-03-25 20:28

then please update your question to reflect that and in the mean time I’ll prepare answer for you

Bartosz | 2018-03-25 20:41

Okokok. Done and edited.

Syl | 2018-03-25 20:50