Trying to only show enemies in light but they're visible through walls

Godot Version

4.7.2

Question

I’m making a 2D game where the player has a flashlight. I want enemies to be invisible outside of that flashlight. I have succeeded in that. The issue is, they become visible, even through walls. I already verified that my TileSet blocks the correct layers (1 & 2). The TileSet blocks the light from hitting the enemies, so they end up visible but darker as they’re in shadow until they come around the wall.

I’ve already verified:

  • Enemies have Light Mask and Visibility Layer set to Layer 2
  • Flashlight has Range → Item Cull Mask on Layer 1 and Layer 2
  • Flashlight has Shadow → Item Cull Mask on Layer 1 and Layer 2
  • TileSet has an Occlusion Layer with Light Mask on Layer 1 and Layer 2
  • TileMapLayer has Occlusion Enabled turned on.

I can’t find any way to prevent the enemies from becoming visible through the wall short of raycasting every enemy within a given radius and manually flipping visibility if they’re behind a wall.

That’s, to the best of my knowledge, the best solution. Have each enemy have a raycast based at their position and a target of the player’s position. Then set it’s mask to the collision of your walls and if it’s colliding hide the enemy