Raycast2D seems to be colliding with empty tiles in a TileMap instead of Player

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

Hello, i am a newbie at Godot and i am making a 2D top-down shooter. I was implementing a sight mechanic for the enemies, where the enemy is supposed to raycast toward the player when it enters his range, and if the raycast is not interrupted by any walls, the player is “seen” and they start chasing, but i’ve ran into a where the Raycast2D never returns the player, only “TileMap”. This is what the code currently looks like:

extends "res://AI.gd"

onready var line = $Line2D

var hitting_player: bool = false
var player_in_range
var player_in_sight
var first_time = true

func run():
	sight_Check()
	
	if player_in_sight:
		var direction = body.global_position.direction_to(player.global_position)
		direction *= actor_values.speed
		body.move_and_slide(direction)

	if hitting_player:
		player.handle_hit(actor_values.strength)

func _on_Area2D_body_entered(body):
	if body == player:
		hitting_player = true

func _on_Area2D_body_exited(body):
	if body == player:
		hitting_player = false

func _on_Vision_body_entered(body):
	if body.is_in_group("Player"):
		player_in_range = true
		player = body
		print("player in range")

func _on_Vision_body_exited(body):
	if body.is_in_group("Player"):
		player_in_range = false
		print("player out of range")

func sight_Check():
	if player_in_range:
		var space_state = get_world_2d().direct_space_state
		var sight_check = space_state.intersect_ray(position, player.position, [body], body.collision_mask)
		if sight_check:
			if first_time:
				line.add_point(body.global_position)
				line.add_point(sight_check.collider.global_position)
				first_time = false
			
				print("spotted" + sight_check.collider.name)
			if sight_check.collider.is_in_group("Player"):
				print("player in sight")
				player_in_sight = true
			else:
				player_in_sight = false

The code works just fine when i delete all collision layers from the TileMap, but when i put them again, the enemies don’t detect the player even if he’s not behind a wall. While debugging, print("spotted" + sight_check.collider.name), only returns “spottedTileMap” no matter what, and the lines i draw point to positions that i don’t understand where they came from, and i have no idea on what could be wrong now.