Get a tile's metadata.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

Okay, I’ve been pulling my hair out on this for a little while now. I need to try and pull a tile’s ID that my player (a KinematicBody2D) is colliding with. I’ve been told that I can use get_collider_metadata() to pull said information, but I have no idea how to pull that off. Please help.

:bust_in_silhouette: Reply From: kidscancode

Tiles do not have this. TileMaps work by batching all of the tile textures and collision polygons into one object.

When your KinematicBody2D collides with a TileMap, the collider that is returned is the TileMap itself. To detect what actual tile you collided with, you must translate the collision point to map coordinates, at which point you can retrieve the tile ID using get_cellv(). See the Tilemap and TileSet docs for details on the available methods.

You can also see a detailed tutorial on this here:
http://kidscancode.org/godot_recipes/2d/tilemap_collision/

Or, if you prefer video:
https://youtu.be/OzgK__VowVs

This works perfectly. Thank you so much.

9BitStrider | 2019-05-10 21:08