0 votes

My unit scene has an Area2D "Sensorium" with a CollisionShape2D. I use the body_shape_entered signal on the Area2D to detect overlaps. This works fine with other units because I can get their positions via the body parameter and make the unit react accordingly.

However, if the CollisionShape2D overlaps with a tile in a tileset, the body parameter is the TileMap node, and gives no information about what specific tile is overlapping.

func _on_Sensorium_body_shape_entered(body_id, body, body_shape, area_shape):
         print(body_id, ", ", body, ", ", body_shape, ", ", area_shape)

When overlapping with a tile, the above code prints:

1221, [TileMap:1221], 3, 0

As you can see, the above signal does not help to get the tile the Sensorium Area2D's CollisionShape2D has overlapped with. It only tells me the tilemap it overlapped with.

I am not interested in physics collision. I am interesting in detecting if the unit is close to tile(s) so the unit can apply a steering force to move it away from the tile(s) before it gets near to the tile(s).

How do I detect which tile an Area2D's collision shape is overlapping with?

[edit: clarity]

Godot version v3.2.3.stable.official
in Engine by (12 points)

1 Answer

0 votes

You can scan for the tile/cell using worldtomap

var unit_location:Vector2 = unit.global_position
var cell:Vector2 = tilemap.world_to_map(touch_location)

You may have to consider the position of your tilemap. See also this tilemap question/answer

by (530 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.