r/howdidtheycodeit • u/Skycomett • Mar 15 '21
Question Unity, How would I go about detecting if gameobject is behind a wall.
I'm trying to have my AI do "X" if the AI cannot see the player.
currently I use the method below to detect if the AI is in the player cameras frustum.
public bool IsVisibleFrom(CapsuleCollider collider, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, collider.bounds);
}
But this detect through walls.
How would I go about detecting if walls are obstructing the view of the enemy towards the player?
Would I just add a simple raycast which is always facing towards the player but only let the AI do "X" if it actually reaches the player?
22
u/tilodry Mar 15 '21
Sounds like the right way, but what you could do is combining the two ways and raycast once your "isVisibleFrom" detects the player, this way you only raycast when you need to and you gain process time by not spaming your raycast, might be better for your fps. If i were you i would try the 2 ways and check perfomances difference.
5
u/johnnydaggers Mar 15 '21
Sure, that sounds fine.
4
u/TheCharon77 Mar 15 '21
Raycast.
3
u/[deleted] Mar 15 '21
Yeah, that sounds about right
2
u/Inverno969 Mar 15 '21
Each AI could have it's own Sensor script that either every frame or every x seconds checks for the Player within a radius around them. An easy way to do this would be to use Physics.OverlapSphere (Physics2D.OverlapCircle for 2D) with a Layermask that only contains the Players layer. The radius of the OverlapSphere test is effectively the AI's VisionRadius.
If the AI Sensor detects the player within it's Vision Range, send a Raycast out from the enemy's position to the Player's position. The Raycast should have a LayerMask that only effects Terrain/Wall objects.
If the Raycast hits something, the Player is obviously not somewhere the AI would be able to see OR Be seen by the player. Return back to checking the Vision Radius.
If the Raycast doesn't hit something, you can then do a Field of View check from the AI to the player. If the Player is within the AI's Field Of View, the AI can "see" the Player. Otherwise the player is still technically ABLE to be seen, but may be behind the AI. You can do some math checks with the facing direction of the enemy to determine if the player is behind them using dot product.
You can use all this info to set a state on you AI, for example :
There are dozens and dozens tutorials and guides for different ways to handle the Field of View for AI in Unity so definitely look around if you are looking for ideas and tricks on how to implement it.
1
u/mrbesen_ Mar 15 '21
i would try to make a ray and a intersection test with every object, that can obstruct the view.
Then i would save the last object that obstructed the view last, to be faster at checking, if this object is still in the way, or a new complete computation is needed.
Note: i have no idea about unity, i only created a CPU ray tracer once.
0
u/[deleted] Mar 15 '21
[deleted] — view deleted comment