Hello Unity Community - I’m trying to make a hiding function for my AI.
I want the AI to find a position - that has at least 2 connecting walls - (Corner) - Which should be a valid place for it to hide in.
I have illustrated what I’m trying to achieve in the attached picture.
I Would be much appreciative if anyone could provide me with some code for this.
No, people are unlikely to just do the work for you. Instead you should try and explain what you have done yourself to try and figure out the problem and where you are stuck. People can then help you get to the next step/steps and that way you will actually learn, instead of being unable to act independently.
So what have you done so far that did not work? How far did you get, what specifically are you stuck on?
is the map procedurally generated or handmade?
if handmade, can place the location manually for best results…
if need to find from script, one idea:
loop all meshes, get their bounds,
from each bounds edge, check overlapsphere in 8 directions (within some radius),
if empty, then check 4 raycasts to each direction from that position,
if hit 2 or more walls, its probably good position.
If the map is tile based as you have shown in your picture (all your walls could easily be on a grid) then I would recommend splitting it into cells and doing a grid based check. You can very quickly work out corners etc from that and basically generate a “score” per cell that the AI can then pick from
You can then include distance from the AI into the score
This is basically combining A* with some additional checks, for pathfinding and is very well documented in tutorials all over the place
It does not have to be a tile based map for this to work, although it is much easier
If the map is a polygonal soup with perpendicular walls, then…
Fire a raycast at a wall.
This will give you a hit position and normal (light blue).
Displace hit position using normal, by amount determining how far the wall should be from hiding position.
From that point fire two more raycasts in opposite directiosn - perpendicular to floor and parallel to the wall. See if they hit anything.
.
If you have a hit, displace from the hit position by the same amount and check if there’s actually a wall nearby.
That’s one possible approach, and honestly, it will be easier to precompute those zones, even in case of procedural geometry.
Very nice! - But I also see the problems it can have/give, in case of more complex geometry.
I have also considered the predefined zones of hiding.
The only problem I’ve seen with that approach, is if multiple agents clog up at the same position - But I guess that’s the easiest solution for implementing this function.
Many thanks for all of your answers - It was very good illustrations!
In case of more complex geometry you could voxelize the scene and for each voxel calculate coverage, then seek clusters. You won’t be able to do that dynamically, of course and will need to preprocess the level. This won’t work with dynamic geometry either, meaning if you, say, drop physically controlled cargo container into the level, the NPCs won’t hide behind it (and will try to force their way through it)
Like I said, it is also possible to fire dense raycasts in radial area and use those to reconstruct simplified 2d graph of walls in vicinity. Then use that to determine where to hide. But that solution will not be simple either.