Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

A* Pathfinding in Multiplayer?

Discussion in 'Multiplayer' started by ARares, Jul 13, 2017.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
  2. fvde

    fvde

    Joined:
    May 15, 2014
    Posts:
    19
    What keeps you from doing it? Ideally you could consider to centralize AI decisions on the hosting machine (server or hosting player) in order to prevent different behavior on the clients. Then you could sync the resulting routes between players.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,297
    You surely can. We're using it for our multiplayer game.
     
  4. Leoo

    Leoo

    Joined:
    May 13, 2013
    Posts:
    96
    AI should only be loaded, activated or used on the Server/Host side, Zombies would move towards players on the Server/Host Scene, and each should sync position and actions to all clients.
     
    fvde likes this.
  5. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    you could also sync destination of navmesh every one sec, so it will do pathfinding on both client and server, it will be smoother than syncing position 10 time per sec
     
  6. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    I'd be careful about this. Nav mesh is not deterministic and lots of funky stuff can happen when agents bump into each other. Its probably fine-ish though
     
  7. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    yes its been used in ummorpg and it work fine while it save lot of BW
     
  8. PHambone06

    PHambone06

    Joined:
    Mar 15, 2021
    Posts:
    1
    How did you get it to follow multiple targets? is there a tag or something I would apply to all players so it chases the closest one?
     
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,297
    There's nothing built-in like that, but everything can be implemented

    This was back in 2017, so as far as I remember, implementation ended up like this:
    - Server overlap checked via physics query. (Technically, even distance check should work);
    - Server calculated path, sent results to the client;
    - Client interpolated results, plus applied error correction (force set some position, rotation etc);

    So basically everything detection wise should be on the server, and client only received results.
     
  10. Sicknostalgia

    Sicknostalgia

    Joined:
    Jul 29, 2021
    Posts:
    1
    tutorial pls.
     
  11. grimunk

    grimunk

    Joined:
    Oct 3, 2014
    Posts:
    285
    This is generally true, but there is more to it than just having server-side pathfinding. A* can be a very expensive operation and that needs to be taken into consideration.

    We've been working with different approaches that decouple to use of expensive algorithms from the server tick: We employ a world state server that manages client connections and syncs entities, and then use one or more external clients (often headless Unity) to drive AI functions like pathfinding for NPCs via RPCs. We found that it performs quite well, and issues such as a an AI running way too slowly never affect game performance, only the performance of the node in question. The result is that the gameplay remains smooth.

    We have also extended this to put clients to work - when the server needs a path calculated, it chooses a client and requests it to do the calculation. The client computes and returns the path, which the server can then verify using physics queries. These solutions do inject latency from the time the path is requested to the time it is generated, however that latency is much more predictable than if the server is handling everything, and the pathfinding calculation can't interfere with server functions by using too many resources for too long.