How can i get the tile id and tile position that is colliding with my player?

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

extends KinematicBody2D

var cell
var tile_id

func _ready():
pass

func physicsprocess(delta):

if (collision.collider.name == “TileMap”):
cell = tilemap.worldtomap(position)
cell -= collision.normal
tileid = collision.collider.get_cellv(cell)

print(cell)
#For the if statement i get the error Invalid get index ‘collider’ (on base: ‘Nil’).

You asked this question before - that error means that “collision” is null.

exuin | 2021-04-04 22:21

so what do i do?

javrocks | 2021-04-04 22:49

Do have “collision” declared anywhere or is that all your code

exuin | 2021-04-05 02:43

:bust_in_silhouette: Reply From: exuin

Here is code that works:

for i in get_slide_count():
	var collision = get_slide_collision(i)
	if collision.collider.name == "TileMap":
		var pos = tile_map.world_to_map(collision.position)
		var id = tile_map.get_cellv(pos)
		print(str(pos) + " " + str(id))

Thanks it’s working now

javrocks | 2021-04-06 23:01