Godot 4.2.2
Question
I’m making a simple physics simulation, and I need to get the closest object to the mouse cursor to delete it. This function is called and the queue_free() is called on the returned node.I’m calling the function but nothing is happening. Here’s my code:
func get_nearest_object():
var mouse_pos = get_global_mouse_position()
var objects = []
objects.append_array(get_tree().get_nodes_in_group("circles"))
objects.append_array(get_tree().get_nodes_in_group("boxes"))
if(objects.empty()):
return null # Failure case
if(objects.size() == 1):
return objects[0]
var near_obj = objects[0]
var near_pos = near_obj.global_position
var near_sqr = mouse_pos.distance_squared_to(near_pos)
for i in range(1, objects.size()):
var obj = objects[i]
var pos = obj.global_position
var sqr = mouse_pos.distance_squared_to(pos)
if(sqr < near_sqr):
near_sqr = sqr
near_obj = obj
return near_obj