Search Unity

Third Party Multiplayer game with PUN2

Discussion in 'Multiplayer' started by Tom-Kazansky, Oct 24, 2020.

  1. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    Hello!
    I need help with making multiplayer game with PUN2.

    Brief about the game
    - it's a 2D mobile game
    - the genre is "battle royal"
    - there would be 4 players in a room (maybe more)
    - a number of "mobs" (neutral hostile) that will respawn if killed

    What I have
    basic flow:
    - connect to master
    - connect to lobby
    - create a room if there is no rooms available
    - join an existing room

    gameplay:
    - spawn an "actor" for each player
    - the master client will spawn mobs
    - user inputs (move, use ability) will call respective RPC functions (target is AllViaServer)
    - the actor will auto attack enemies in range
    this scan will be processsed on photon view owner's (user's machine for player actor and master client for mobs)
    if a target is found, call RPC function with target's photon view ID as parameter.
    on all the clients: find the respective photon view and command the actor to attack the target​
    - random element in game:
    the server (master client) will generate a list of random numbers and send to all clients
    the client will save these numbers in a list, newest numbers will be appended to the end of the list
    when client want to use random number, they just get the first number in the list.​
    - all actors have IPunObservable to sync location and rotation.

    - some elements such as: projectile, visual effect, sounds,.. are manipulated locally.
    player A move actor in range of player B's actor
    these actors start firing at each other
    projectiles are created locally and move at respective direction
    collisions of the projectiles are checked locally as well
    damage are also calculated locally (with some "random element" in it)​

    The problems that I have
    actors movement looks... choppy and jittery depends on lag time
    stuffs are out of sync, especially the random stuff
    in above case when player A's actor and player B's actor firing at each other, sometimes the "random" is out of sync and the player A's actor die on player B's game screen but not on player A's game screen.​

    I know that the amount of data being synced is inadequate, reasons:
    - I'm afraid the amount of data would be too much for a mobile game
    - I'm not sure which data I should sync.

    I'm confused about some stuffs
    - the world should be simulate by the server and the server will send "snapshot" to client
    what does "snapshot" mean?
    - is it data of all the actors (location, rotation, state, animation,...) ?
    - do I have to store these data in every frame? or every "server tick" ?
    - must I "cache" several snapshots in case the client need to render the game "in the past" ?​

    - Entity interpolation and lag compensation
    the client should process interpolation between snapshots received from the server, do I need only 2 snapshot? "past" and "present" ones?

    - to have the "snapshot" of everything: so I need to instantiate even the stuffs that're processed locally before, like: projectile, visual effect,... ?



    I will appreciate any helps.
    Thank you for reading.
     
  2. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    can anyone help me? :(
     
  3. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    Not sure what your question is exactly, but from what you're "confused about" I would say:

    -You could create your own type of "snapshots" by using a List of GameObjects with PhotonViews; you'd just have to be a little creative with how you accomplish what you want.

    -As far as lag compensation I think you're referring to interpolating or lerping things like positions via OnPhotonSerializeView which is implemented by IPunObservable scripts.

    I would highly recommend reading Photon's documentation on these matters, it is very helpful.
     
    tobiass and Tom-Kazansky like this.
  4. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    Hi kopf, thank for the reply!

    I'm starting to implement my own "snapshot", I'm worry that the way I'm implementing the snapshot, it might become too big for mobile network to handle. I guess I have to run and analyze this then make appropriate modifications.

    I will go read Photon's documentations again, I might have missed something crucial. :)
     
  5. qbvbsite

    qbvbsite

    Joined:
    Feb 19, 2013
    Posts:
    83
    The questions you are asking really are no Photon related but more game mechanics. Are you setting up your game with a headless server? or is one of the clients going to have authority over the game?

    In the instance of a headless server (maybe even a client acting as a server) it would play out like this:
    • Client Send Input to Server (I'm moving 1 tick in this direction) with an identifier (timestamp or tick number). Then you store the move and execute it on the client.
    • Server receives the input and does the move on the next tick
    • Server then stores the result (with timestamp/tick) and sends the resulting position to all players (including the player who sent it)
    • When the player who sent the request gets the result it simulates all inputs it has stored ahead of the result (this is where you use the identifier you sent the server). For example, server sends you the result for tick 6, you then discard any input tick 6 or lower and reply ticks 7+ starting from the result the server gave you for tick 6.
    • Once you have simulated the new result if it differs from your current position you either SetMove to the new position or use something like Lerp to smooth out the difference over many frames.
    Now as far as actions (like attack) you have to do something similar on the server. When the server receives an attack input from a client it needs to recreate the world as the client sees it when he made the action. This is done by using the stored results of all players and rewind their positions based on when the clients made the action. With these rewound positions you can see if the attack is valid or not and send the appropriate response.

    Hope that makes sense and helps you out.

    --James
     
    Tom-Kazansky likes this.
  6. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    Hi James, :)
    Thank you for the help.
    this cleared things up a bit for me.

    I actually want the headless server implementation, but for now I will let the "host" (creator of the room) have the authority over the game, this host will only observe, not participate in the game.

    I will continue with the implementation.
     
  7. qbvbsite

    qbvbsite

    Joined:
    Feb 19, 2013
    Posts:
    83
    No problem, if you run into any issues let me know. I'm also making a 2D networked game and just recently finished up the networking code for client prediction. I personally went with the fully authoritative console server implementation but the concept of client prediction is the same.

    --James