Enemies shoot through walls

I’m not sure if anyone has found this, but the enemies can shoot through walls. I tested the exe file in Resources and it behaves the same. I tried again using the complete project from GitLab and, again, same outcome. Is there an obvious fix that you see? Thanks for the great course.

3 Likes

The enemies were never programmed to know about walls. There is nothing preventing them from shooting through the wall.

What you can do is to cast a ray from the enemy’s ‘eye height’ towards a target and see if it hits a wall. You would also have to do this for each position that the enemy AI considers when determining the best path, otherwise it will find a great position to go to, but then be unable to shoot from there.

1 Like

While this is quite effective (I set it up in my own project), it has one distinct disadvantage… When running through the AI, between the pathfinding and the raycasting, things can slow down to an uncomfortable crawl.

For fixed levels, it may be worth considering making a GridVisibility class that serializes a list of locations that are “visible” from a given tile… The downside of this involves tiles that are things like doors, where opening a door would expose tiles to shooting visibility.

I’ll add this to my “ideas” list… something psuedocode like this:

  • Serialize a list of grid positions visible from this GridPosition
  • Create a custom editor that displays the list, allows additions, and autoscans using ray tracing to autogenerate a list
  • OnDrawGizmosSelected would highlight all GridPositions “visible” from that location.
  • Classes like ShootAction can get that list, and find all positions from the visible list that contain enemies.

It would mean some extra overhead in the inspector, including the custom editor having access to the LevelGrid in edit mode, to get the tile size for that level.

Of course, all of this falls apart with temporary obstacles like doors or if you want to avoid shooting through your teammates.

1 Like

Hey
You should check the raycast in shoot action or obstacle layer in walls
I have similar with Door, but now everything works properly

Yep, a Raycast is the most straightforward solution. Just bear in mind that when testing the AI for a dozen possible move locations each with a dozen possible shoot locations, this can sometimes lead to slow performance.

Thank you to all who replied, I appreciate it. The GetBestEnemyAIAction() seems (to me) would be the place to check, but I’m still too new to Unity to completely understand how this method interacts with the various Actions. As written, it seems like the friendly character should not be able to shoot through walls, and it cannot, but the enemy AI seems to be missing a check. I reduced the weapon range to 4 and the enemy correctly navigates to the door and takes a shot, but in this case the LOS is not blocked so it works as expected. I’m a noob taking an intermediate course, so this was to be expected. I like the teaching style and learn a great deal. I was hoping for a functional sandbox to play around in. Solving this problem is still beyond my understanding. I’ll keep picking at it and (hopefully) figure it out. Thanks again.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms