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

Multiplayer Logic

Discussion in 'Editor & General Support' started by Ves, Oct 22, 2012.

  1. Ves

    Ves

    Joined:
    Apr 6, 2011
    Posts:
    23
    I'm having difficulties getting my head around the networking in Unity. Really, I'm just struggling with the conceptual process of changing my codes. I'm "converting" a project to be multiplayer, but I feel like I can't grasp how the server and client communicate, and was hoping for just some conceptual clarification.

    The difference between RPC and State Synchronization

    I've read quite a few articles and looked at the Unity networking example, as well as the M2H PDF file. What I don't quite understand is the difference between RPC and State Synchronization.

    I see that RPC calls a function that already exists on all clients (or Other, or Server). But what is it really doing? For a single client playing in the multiplayer scene, is it telling the client to perform that function on every network player in that scene that has that function? So for another client that was killed, I would call an RPC function called "Die()" on all Network Players... does this instruct every client to perform "Die()" on their particular instance of the client that has been killed?

    For State Synchronization... what does this actually tell the server? If, like the example, I have a "health" variable, and I stream that value to the server, is this updating that value for that client for every other client that is connected to the server? And for animations, I would stream a value that represents a state to the server, I am, in essence, instructing every click to tell their particular instance of another network player to be in that state?

    Authoritative Servers

    So my game is not very complicated. Players have a handful of pre-determined stats (i.e. particular prefabs with built-in stats), and then they fight in a small arena-type map. I don't want it to be easy for players to cheat, so I was hoping to do authoritative networking. To do this, conceptually, I believe I would have to send information like damage values, position values, and stats to the server to handle. How would this be done? Position, for example, would I send a Vector3 through an SerializedNetworkView? How would I check if the movement is possible? Also, for damage calculation, would I send the intent of a player to attack to the server, and the server decide whether or not that attack hits another player, and if it does, apply a damage based on the state of the attacking player? Would this be sent through a SerializedNetworkView as well?

    Thanks in advance for the time taken to respond to any of this!
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Yes, if you execute the RPC command with RPCMode.All, it will execute on all instances. Altough your example doesn't make sense.

    A cube with a networkview ( a player ) has a script attached to it and that script calls an RPC.
    The RPC ( if RPCMode.All ) will execute on all those cube instances on all client connections.

    A client sends it's input to the server, not the actual Vector3 positions using RPC's.
    The server processes/moves the client objects and then updates the clients through OnSerializeNetworkView.
     
  3. Ves

    Ves

    Joined:
    Apr 6, 2011
    Posts:
    23
    Thank you very much; that definitely helped.

    For this, how would I do it? I see in the Network example a script that reads user inputs and calls an RPC function that sends those inputs to the server. Once the server has those inputs, how does it go about moving that particular player and then sending the new position back to the player?

    Thanks again!
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    The server knows from which client the inputs come, it moves the player object and then sends the Vector3 and Quaternion values through the OnSerializeNetworkView method.