Godot Version
v4.3
Question
Hi, I’m pretty new to Godot. I’m trying to do a drag and drop method that fits to a grid. The drag and drop items are RigidBody2D with CollisionShape2D and Sprite2D. I’m not doing traditional drag functions, I want to click to start the drag, and click again to stop the drag.
I followed this tutorial for just a basic drag and drop: https://www.youtube.com/watch?v=iSpWZzL2i1o
This method works, and when I stop dragging, the item drops to the ground.
extends RigidBody2D
var selected = false
func _physics_process(delta: float) -> void:
if selected:
global_position = lerp(global_position,get_global_mouse_position(),25*delta)
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if Input.is_action_just_released("Click"):
selected = !selectedextends RigidBody2D
Then I followed this tutorial with some changes: https://www.youtube.com/watch?v=H5249Bhmhzo
The drag and drop works, but the item ‘freezes’ and it stays exactly where I placed it, instead of dropping to the ground. I also have this problem where the _on_input_event seems to only still trigger in the same area that the item was placed, so it doesn’t move with the item anymore. That’s why you can see the code got a lot more messy.
extends RigidBody2D
var selected = false
var dragging = false
func _input(event: InputEvent) -> void:
if selected && Input.is_action_just_released("Click"):
selected = false
dragging = true
elif dragging && Input.is_action_just_released("Click"):
dragging = false
elif dragging && event is InputEventMouseMotion:
global_position = snapped_position()
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if Input.is_action_just_released("Click"):
selected = !selected
func snapped_position() -> Vector2:
var mouse_position = get_global_mouse_position()
return Vector2(
snapped(mouse_position.x,64),
snapped(mouse_position.y,64))
So, my two questions are:
- What about using the second method with snapped() to set drag location is ‘turning off’ gravity?
- Why doesn’t the _on_input_event move with the item?
I appreciate any insight on this! Thank you.