prevent enemy see through walls

Godot Version

GODOT4.4

Question

im trying to make enemy vision, i have made area 2d that when the player enter it the enemy will chase the player and it worked the problem is the enemy can detect the player through tilemap, tried raycast but it also go through tilemap even though i made sure that collision layer and mask layer the same, is there any way to make the raycast not go through tilemap or is there better way for enemy vision?

raycasts will not go through tilemaps if set to the appropriate layer/mask, so maybe it has more to do with your code, how did you implement the Raycast to stop chasing?

What I do is a combo of both. The Area2D triggers on the player. While the player is in range, I do a Raycast to determine if the enemy has line-of-sight. By doing this, the Raycast can collide with everything. If it doesn’t bring back the player as the first thing it hits, there’s no line of sight.

If raycast supposed not go through tilemap im gonna recheck the setup of it

I have tried 3 methods to make enemy vision, first one by area 2d and cancel it cause enemy see through walls,

second method by add Node2d called (raycast_node) and then add raycast as child to raycast_node and added to the node script look_at(player) and when the raycast collide with player its gonna follow it but the problem is that the raycast was go through the tilemap and as u mentioned if it should not go through the tilemap im gonna recheck the setup of the raycast

Also tried area2d and raycast method but still the raycast have the same problem

I aslo tried this but the problem is the raycast go through the tilemap, im gonna do recheck of the setup of the tilemap

Make sure that your Tiles are in their correct physics layer. You can make a new physics layer for your TileSet, set its collision layer, then adjust the RayCast mask to that layer. That way the RayCast will actually detect the walls and you can add a condition to only chase the player if no walls are currently colliding with the RayCast.

If you want maximum accuracy, you can check the local tile coordinates of the TileMapLayer and compare tiles between the enemy and the player to see if any are wall tiles. This works, but it’s more tedious and heavier on performance.