Posted November 08, 2024 by Board Gamers
#AI #Unreal
In my last post I described the process behind my new enemy AI targeting system that I developed. This system has lead to much more dynamic gameplay as players now have to deal with enemies spreading out randomly as they approach. I mentioned at the end of my last post that I had some kinks to iron out and while I did fix some smaller issues, there were two much larger considerations that came up only after playtesting with the new system.
As a brief summary of how the system works, essentially the enemy AI chooses a random point in a range around the player and as they get closer and closer, that point uses the same angle and a proportional distance to eventually give us a unique point in melee distance around the player. That point is now what enemies move towards rather than moving directly to the player, and this works great to achieve the desired separation. The first major issue I ran into when testing however was in the case where the player moves and the fixed point starts hovering over an area that does not have a nav mesh. This is an issue because if the point is not in a navigable area, the enemies will rightly believe they can't move to that point and so instead they will stop moving altogether. The second issue is again about the placement of the point, which is if there is an obstacle in the way of the path between the player and the point. In this case for instance if there was a wall between the point and the player, the enemy will go behind the wall and will not be heading towards the player at all which again leads to them getting either stuck or walking back and forth in front and behind the obstacle.
The desired behavior that would fix these problems would be for the point to dynamically adjust, making sure it stays on the nav mesh and also that there are no obstacles between the player and the point. I first decided to handle the nav mesh problem as it was the most severe. Within the Unreal Navigation System, there is a handy function for just this case called ProjectPointToNavigation. This function allows us to send in a position and a radius to check if it is within a navigable area. Once we know whether our point is on the nav mesh or not we can either set the point to be that location or in the case that its not on the mesh, we can incrementally move the offset actor towards the player and check again until it is.
This check worked better than expected and as you can see, now the point will remain just on the border of the nav mesh and is now unable to be in any area that is not navigable by the AI. This effectively solved that problem but I still needed to address the second issue. To make sure that there are no obstacles between the point and the player we use a similar method. In this solution we need to use a Line Trace, which will essentially cast a ray from a starting point to an end point and give information on anything it collides with on the way. So we send in the points position and the players position and we look for any collision with static objects. Now we can tell if there is an obstacle in the way, so again we can move towards the player until the line trace passes and there are no more obstacles. This worked decently well but there was one problem with it. The actor itself did not need to fully clear the obstacle for the line trace to pass, meaning that the actor would end up mostly inside the actual obstacle. The solution for this last issue is to do a sweep, which is essentially the same as a line trace but it is in a shape rather than a line. I chose to do a sphere sweep in a small radius around the point which now means that the actor needs to be clear of the obstacle before the location is considered valid.
With all of these new checks and adjustments in place, the point that my enemies track now stays within the navigable area on the nav mesh and makes sure that if there are obstacles in the way that it stays on the side of the obstacle that is closest to the player. I am having a few issues still with getting that final sphere sweep to be reliable but regardless this system is now drastically improved and should work great on the new map we have planned.