Hello i did a basic drag-drop system and added an offset . its works fine . but the 2d is glitching when i drag the object. what is the problem? btw i am new at the engine
and the visual looks like this Imgur: The magic of the Internet
Hello i did a basic drag-drop system and added an offset . its works fine . but the 2d is glitching when i drag the object. what is the problem? btw i am new at the engine
Hi,
Try replacing is_action_pressed
by is_action_just_pressed
.
Checking pressed only will be true every frame, so here you might be changing global_position
every frame, as well as changing the offset every frame (in _process
) which results in a strange result.
Also, you’re not using the dragging
boolean. What you could do is:
1/ On input just pressed, set dragging
to true, and store the mouse offset.
2/ On input released, set dragging
to false.
3/ In _process
, if dragging
equals true, then update the global position based on the stored offset.
Let me know if that helps.
Hello!thank you for the reply, i tried but let me know if i did something wrong with ur guide. still doesnt do the job. also it holds the texture in the middle
Move the line 23 offset = global_position - mouse_pos
in _on_input_event
, where you set dragging to true (between line 34 and 35). You need to calculate the offset to the mouse only once when you click, to then be able to reply it from the mouse when you drag.
did it now but still holds it from middle i don’t really understand the problem at this moment
Yeah sorry, that’s on me, replace offset = global_position - mouse_pos
by offset = global_position - get_global_mouse_position()
, to get the actual mouse position instead of your variable that may not be set at that time.
(If you still have trouble, please consider pasting your code directly as text.)
still doesn’t work
extends Area2D
var draggable = false
var dragging = false
var mouse_pos : Vector2
var offset : Vector2
func _ready() -> void:
pass
func _process(delta: float) -> void:
if dragging == true:
mouse_pos = get_global_mouse_position()
global_position = mouse_pos - offset
func _on_area2d_mouse_entered() -> void:
draggable = true
func _on_mouse_exited() -> void:
draggable = false
dragging = false
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if Input.is_action_just_pressed("click") and draggable:
offset = global_position - get_global_mouse_position()
dragging = true
if Input.is_action_just_released("click"):
dragging = false
What I would do is
extends Area2D
var draggable = false
var dragging = false
var last_mouse_pos : Vector2
func _process(delta: float) -> void:
if dragging == true:
last_mouse_pos = mouse_pos
mouse_pos = get_global_mouse_position()
global_position += mouse_pos - last_mouse_pos
func _on_area2d_mouse_entered() -> void:
draggable = true
func _on_mouse_exited() -> void:
draggable = false
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if Input.is_action_just_pressed("click") and draggable:
dragging = true
if Input.is_action_just_released("click"):
dragging = false
Try by adding the offset instead of subtracting it when dragging:
func _process(delta: float) -> void:
if dragging == true:
mouse_pos = get_global_mouse_position()
global_position = mouse_pos + offset
EDIT: posting the full code as this has been marked as the solution:
extends Area2D
var draggable = false
var dragging = false
var mouse_pos : Vector2
var offset : Vector2
func _ready() -> void:
pass
func _process(delta: float) -> void:
if dragging == true:
mouse_pos = get_global_mouse_position()
global_position = mouse_pos + offset
func _on_area2d_mouse_entered() -> void:
draggable = true
func _on_mouse_exited() -> void:
draggable = false
dragging = false
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if Input.is_action_just_pressed("click") and draggable:
offset = global_position - get_global_mouse_position()
dragging = true
if Input.is_action_just_released("click"):
dragging = false
i litteraly copy paste this
OH WELL, THIS WORKED!! Thanks a lot man!
You’re welcome
here’s in game!