Enemy Targeting


Author: Antonio Roldan

Desired Feature:

I wanted the enemy AI to have the ability to target the nearest enemy to it since we are creating a multiplayer game. This feature would make enemies target the closest player to them rather than always targeting the server host. By default with the following behavior I made the AI would use a pre-established function to identify the player controller which would always grab the server's player controller which is an issue for having a cooperative experience as you would obviously want the enemies to attack every player and not just the server host. 

Some issues I had to solve were how to actually get the active list of players as well as how to determine which user was the closest to the enemy. I also had to deal with how this would replicate for a multiplayer experience.



Solution:

So the first thing I needed to do was rather than just grabbing the server's player controller by default I needed a list of player controllers in the scene. Luckily one of my fellow developers was already keeping a list of active players to be used in the mini-map functionality. All I needed to do was get the current game mode which was holding a list of active player controllers. Once I did this the next thing I needed to do was find out which player was the closest. I did this by going through the array of player controllers and iteratively getting their location and comparing it with the my enemy AI's location to get the distance. I then kept track of which distance was the shortest and once the loop was finished we sent a reference to that player to my blackboard to be the key for which my AI would execute it's Move To task.

That solved my issue and I was able to implement and test that this was working by adding a delay for this and moving away as the server player and my AI would successfully target the closest player which then was the client player. This also displayed another issue which was that the attack animations for my enemies weren't replicating to clients. I solved this by multicasting the attack functions for my enemies and then calling them in my attack task nodes via another server attack function that called the multicast attack function. After I did this I tested to make sure it was working and it was successful. 

During my implementation of the player finding functionality one issue that I ran into was that I was initially trying to get the location of the player controller. This doesn't work because while the player controller is in the scene, it isn't actually reflective of where the player is. This was solved by changing that logic to get the owning pawn of the controller and get the location of that pawn which worked like a charm. 

Leave a comment

Log in with itch.io to leave a comment.