Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Playing offline with bot(s)

Discussion in 'Multiplayer' started by ZeWitcher, Aug 16, 2015.

  1. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    Hello,

    I am working on a game and using the new networking system. Currently, I'm only focusing on having the game work with real players over a network (i.e multiple instances running that communicate with each other). However, later on I plan to implement playing with offline "bot" players. Is that something I should be worried about early in the project? Or is that an easy task at any time?

    In my mind, the ideal way of implementing AI "bots" is having the server create and "connect" Player objects (the ones that are able to run Commands), just as if they were real players. BUT, instead of having a real person control them from a separate instance, they would be controlled by the server, again, without the need of extra instances of the application running. If that is possible, then I would not need to worry now, at all. But the question is, that possible to do in Unity with the new net system?

    Any help is much appreciated :)
     
  2. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    I've used that system with the old Unity networking without issues, can't see any reason it wouldn't work with the new networking system.
     
  3. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    Thanks for the reply! So how can I add a bot player to the server in code? I just need a simple example to get started.
     
  4. Glabrezu

    Glabrezu

    Joined:
    Aug 30, 2014
    Posts:
    24
    An example would be an abstract class Unit that would have most of the general stuff. Then you'd have two classes: Player and Bot and both would inherit from Unit. Most of the stuff such as movement, etc would be in Unit. Bots would have AI behavior, while Player would behave based on human user's input.
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    To some extent, you can make the bots just game objects that run on the host of the same type of prefab as real players. In this scenario, the functionality shared by bots and players can be "wrapped" by commands so that remote players can access them.

    Code (CSharp):
    1. // used by local bots on host
    2. public void Attack(Vector3 pos)
    3. {
    4.     //do attack
    5. }
    6.  
    7. //used by real players (local and remote)
    8. [Command]
    9. void CmdAttack(Vector3 pos)
    10. {
    11.     // player specific validation
    12.     Attack(pos)
    13. }