Hide things which aren't in line of sight from another position

I created a top down game using the preset. I want to introduce a functionality like this game has, where certain things (enemies, etc.) are hidden if they’re not in line of sight from my character, but the remaining stuff is still rendered.

How would I go about doing that in UE5? I thought maybe use an opacity mask on the materials of the enemy characters. There I can use a node to get the world coordinates, but how would I find out if my character can see the point? I don’t know if linetracing is possible in material graphs, but I guess that would be way too resource intensive anyways. Or does anyone have an idea on how I could do this in a different way?

Line trace would be the answer :slight_smile:

If you can trace from the player to the enemy, then draw them, otherwise not :slight_smile:

In fact, now I’m thinking about it, it would make more sense to trace from the enemies to the player. If they can’t see the player, they make themselves ‘hidden in game’.

Yes, the problem is just that I can’t line trace in the material, so I can’t make a smooth version (only one that does a single linetrace for the entire enemy, neglecting specific spots).

I solved this by doing a line trace from my character in all directions, then the only challenge was to get the data into the material I’m trying to hide. I used a really elaborate workaround, desribed in this Reddit post. Then I could write a material function, which checks whether the distance on the xy-Plane from the specific point on the enemy to the player is smaller than my line trace result (thus visible by the player) and set the opacity mask to 1, else to 0.

Implementing that feature was quite a journey, but it worked out well!

2 Likes

Ah, PART of the enemy… I didn’t get that bit… :slight_smile:

Nice.