NOTE: This technique only works in a Non-Authoritative networking situation
While working on a current project which is an online coop game I was running into significant lag issues which made the gameplay not very fun. Basically a master client or server controlled the AI for all AI components in the game, but when these AI controlled characters would attack a remote client, the remote client would get hit at times when on their screen it seemed like the AI controlled character was much too far away to actually hit them. In shooting games this isn’t as apparent as it is in a game like I am doing right now, where the AI controlled characters needs to physically be close enough to hit you.
The Problem
Here is an example of the problem:
The remote player sees themself at T0 which is now. The server receives the remote players position at T1 which is 100ms behind because of latency. The server could just display the character at T1 but that would look jerky as these updates could come in at less than 10 times per second, so the server interpolates the character from T2 to T1 over 100ms, making the player position on the server 200ms behind in time from where the remote player sees himself.
Remote Character: T2------100ms------>T1----100ms------->T0 (Now)
Now in a coop game this is pretty detrimental because in 200ms the remote character could be well out of range of any attacks that might be done by a server AI controlled character, thus making it look very strange if they are hit.
Using Dynamically Controlled AI
The way I was able to solve this issue was by using what I’m calling “Dynamically Controlled AI” ( I have no idea if other people use this technique or what it should actually be called). With dynamically controlled AI, whenever an AI character determines who his target should be, the AI player checks if the target is owned locally or by a remote player. If the target is owned locally, we continue to let the AI do its thing, HOWEVER if the AI determines the target is a remote player, we then send an RPC to all players in the game to tell them that AI is now being controlled by the target player, instead of by the owner of the AI character.
Now what this does is makes whoever the AI should be attacking, the controller of the AI, which in turns makes the latency 0ms between the AI controlled character and the remote character, as the remote character now controls him. Now the all other players besides the controlling remote character see the AI controlled character with lag, but this is inconsequential as they are not currently being attacked.
By doing this whoever the AI is attacking will notice no lag, which is important as they are the ones who will be receiving hits from the AI.
I hope this idea can help some other people struggling with AI controlled characters over a networked game, as it really improved the gameplay of my current project!