Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer Bots / AI

Discussion in 'FPS.Sample Game' started by Kyunh, Dec 2, 2018.

  1. Kyunh

    Kyunh

    Joined:
    Dec 23, 2016
    Posts:
    5
    I'd like to create a Co-op vs. AI mode, how could I go about it?

    Current thoughts:
    1. Instantiate character on server (as if another client joined)
    2. Simulate input on server side (as if the client is playing)

    Would this work somehow efficient or does each bot/AI take up 1 of 16 player slots? Where can I find the corresponding scripts for 1. and 2.?

    Kind regards
     
  2. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    Howdy Kyunh, that sounds like one way, and how I have thought about implementing AI.

    Have another "AI server" as a client, that perhaps receives less network updates than normal clients from the main server. Allowing just AI calculations on AI-Client, to then send consolidated movement data to the main host who accepts it as authoritative. .. and then replicates out to all the human-player clients those updates plus the client update data.

    This would allow for AI, and anything else, to be stripped off the host server to other applications/servers... but then you start to battle latency! Latency on AI calculations would not be due ONLY to the server load, but rather the server load, plus network load, plus network latency, and then plus the AI server load as well. All this can be adjusted for... but will be needed to.

    An AI-Host-Client would take up a server "slot" but the player "slots" do not have a hard-coded limit of 16. You can adjust the player max above 16 via the console and also configs files, the recommendation is around 16 clients, due to the current network implementation and what gets sent to all clients (all data from all clients.)

    Unity mentioned working on improving this shortly, to allow for much more than 16 players with good performance, by limiting what server updates are seen/sent to each connected client to just what they need to see/update. Instead of send ALL updates to all connected clients. This hopefully will allow for area-of-affect network updates, and so perhaps, unlimited player count... but with a soon network update.

    So I am working a bit more on other items, waiting on this network update, until I have to...

    Otherwise, you could run a script in the client/server to act as AI... All depends what you need to do. 1000 AI agents, or just 2-3 AI... could drive different implementations. A script on the server/client would then just update AI actions off some type of network update to sync the AI movements. Which is something we will need to figure out how to mod-into the network updates sent to all clients. Really, firing a gun and syncing a projectile has the basic needs for AI. Its checking if something happens constantly (input/collisions, ect,) and its up to you/that script, to twist and modify that basic system, to make people think the gun is thinking about shooting them, not just running a script to shoot them when triggers are hit...

    Which is all it really is, triggers.

    Good Luck, Have Fun.
    Micah
     
    Kyunh likes this.
  3. Kyunh

    Kyunh

    Joined:
    Dec 23, 2016
    Posts:
    5
    Thanks for your reply, AggressiveMastery.

    For 1. I found PlayerModuleServer.CreatePlayer() which should solve this one. For simulating input on server side (2.) it was much more complicated. The only hints purely on server side for movement or inputs like in the InputSystem.cs was the deserialization of the UserCommands in the ServerGameLoop.cs. Also did I not manage to find enough information to do pathing calculations without using the client side.

    Hence I think your idea of creating a second type of client - the AI server - will turn out to be a more realistic solution to achieve. Regarding the battle for latency I'm too inexperienced in authoritative networking to see if this will cause trouble. But setting up the AI server (client) on the same machine as the server and connect it via localhost the only important bottlenecks I think will be the speed of the ai calculations and their transfer to the clients. This sounds fairly harmless to me.
     
    AggressiveMastery likes this.