I'm working on making a draggable object, snap to a set location when it gets near. For the life of me, I can not get the draggable object to take the position of the stationary object.
extends KinematicBody2D
var dragging = false
onready var root_node = get_parent()
onready var spot = root_node.get_node("Spot1").global_position
signal dragsignal;
func _ready():
connect("dragsignal",self,"_set_drag_pc")
func _process(_delta):
if dragging:
var mousepos = get_viewport().get_mouse_position()
self.position = Vector2(mousepos.x, mousepos.y)
func _set_drag_pc():
dragging=!dragging
func _on_KinematicBody2D_input_event(_viewport, event, _shape_idx):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
emit_signal("dragsignal")
elif event.button_index == BUTTON_LEFT and !event.pressed:
emit_signal("dragsignal")
elif event is InputEventScreenTouch:
if event.pressed and event.get_index() == 0:
self.position = event.get_position()
func _on_SpotArea_area_entered(_area: Area2D) -> void:
print(self.global_position, " ", "self")
print(spot, " ", "Spot")
At the bottom here when the draggable object enters the "spot" area I want it to snap immediately to that location. Any suggestions would help and be greatly appreciated. Thank you.