Godot Version
4.1.1.stable
Question
Hello, its my first time posting here! I am having a hard time understanding tilemaps and now, I ran into a problem that I just dont understand how to solve.
I want to detect all the tiles that are in a shapecast2D, store them in an array and then get the distance between the tiles and the mouse cursor for each tile in the array. With that information I want to do something with the tile that is the closest to the mouse cursor.
I already implemented this feature for nodes instead of tiles and it works.
The problems with the tilemap are, that I just get the tilemap node as a value for the array and that I dont know how to get acces to the tiles itself that are in the shapecast2D. I already got some answers regarding this problem, but I still dont know what to do with them, so it would really help me if you could explain in detail how all of the possible solutions work.
Thank you to everyone who is willing to help me, if you need any further information, just ask!
Here is the function that should perform this logic:
func get_nodes_from_shapecast() -> Array:
shapecast.force_shapecast_update() #Update the shapecas2D collisions
number_of_nodes = shapecast.get_collision_count() #Store the collisions
nearest_distance = Vector2(100000, 100000) #Reset the nearest distance
var nodes_in_shapecast = []
for i in range(number_of_nodes): #Loop through all collided objects
var node = shapecast.get_collider(i) #Get the objects one at a time
if node.is_in_group("static_tree") or node.is_class("TileMap"):
var distance_between
if node.is_in_group("static_tree"):
distance_between = mouse_pos - (node.global_position + Vector2(0, 16))
elif node.is_class("TileMap"):
pass #What should I do here?? I cant find a solution
if distance_between.x < 0: #Calculate if object is the nearest to the mouse
distance_between.x *= -1
if distance_between.y < 0:
distance_between.y *= -1
if distance_between < nearest_distance:
nearest_distance = distance_between
nearest_node = i
highlight_box_visibility = true
nodes_in_shapecast.append(node)