Easy Drag and Drop System in Godot

preview_player
Показать описание
- If you have any question, join my Discord server:

- We are hosting a game jam! Join here:
Рекомендации по теме
Комментарии
Автор

- If you have any question, join my Discord server:

- We are hosting a game jam! Join here:

damios
Автор

if you change the function to:

func followMouse():
position = get_global_mouse_position() + mouse_offset

and in the selection code do:

if event.pressed:
# Mouse Held
mouse_offset = position - get_global_mouse_position()
selected = true

It will drag the sprite from where you clicked it, rather than teleporting the center of the sprite to your mouse. This is good if you want to have large sprites that you can click on anywhere.

farkler
Автор

For Godot 4 it should be:
var selected = false
var mouse_offset = Vector2(0, 0)

func _process(delta):
if selected:
followMouse()

func followMouse():
position = get_global_mouse_position() + mouse_offset

func _on_area_2d_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
mouse_offset = position - get_global_mouse_position()
selected = true
else:
selected = false

mrussoart
Автор

Simple and straight to the point. Thanks!

OpusMagnoCards
Автор

Omg thanks even after 2 years it still helped me so much!
You earned a sub for your hard work

lolbahlol
Автор

In case you have a coloredRect or another control node - they mess with the detection. however, the control node has "gui_input" as well, with "event" working like it does in the video.
that way you can use a control node for everything shown in the video, and avoid the problem with area2d vs control node mouse detection.

Luckyluk
Автор

for Godot 4.2 ->
extends Node2D

var selected = false
var mouse_offset = Vector2(0, 0)

func _process(delta):
if selected:
followMouse()

func followMouse():
position = get_global_mouse_position() + mouse_offset

func _on_area_2d_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
mouse_offset = position - get_global_mouse_position()
selected = true
else:
selected = false

Aserenesnow
Автор

for anyone that is working with Control nodes you can do the same thing but override the _GuiInput function

NEscape_
Автор

Thank you allot for making this video. An Additional request: Can you please make a video where you make the draggable 2D objects not be capable to be placed onto each other? That would be very cool for a Citybuild game.

lifesymbiont
Автор

How do I create priority to whatever is on the top layer? I’m having an issue where when multiple objects are on top of each other it grabs both of them

Rynd_
Автор

has bugs
if u move ur mouse too fast and unclick it will still follow because you're only setting selected when you're in range of the sensor.

aaronmark
Автор

Hai can you make tutorial on TileMap that can be drag when i play the godot and drag it around the screen

szccornish
Автор

Can you do the same by childing a collision shape to a control-parent?

deynamooder
Автор

Same code, but with offset:

extends Node2D

var selected = false
var offset = Vector2(0, 0)

# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if selected:
followMouse()
else:


func followMouse():
position =

func _on_area_2d_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
selected = true
else:
selected = false

OutrunCitizen
Автор

hola, como solucionas los errores que te marca con amarillo? son 3, yo tengo los mismo y si no los soluciono no puedo exportar mi juego... saludos desde Chile!!

superbigo
Автор

how would i implement that but for mobile? Please can you reply I really need your assistance

PPV
Автор

@damios7858 if I change the scale of the camera2d then it is not following the mouse accurately. It is following the mouse but the speed of the mouse pointer and the sprite is so much different The sprite is going ahead of the mouse pointer.

tanmayupadhyay