PvP Collision Based Multiplayer

Options
Hi folks,

I'm fairly new to network programming, and especially PUN having only downloaded it a couple of days ago. I was hoping to get a little advice as to how I might go about achieving my game mechanic, as from searching around a bit I get the impression this one of those "tricky ones."

The game is a 2D PvP last man standing, where for the sake of simplicity each player is a bouncy circle. The only user input for now is to click around your player to apply a small push. The aim is to collide with other players to bump them out the playable area.

You'll likely anticipate the problem I'm having already. Syncing movement through PUN is lovely and smooth and works for multiple players, but the problem comes in when colliding with others. Sometimes the player we collide with doesn't move at all, sometimes they do but very delayed. From other discussions I understand this to be down to the "ownership" of the RigidBody2Ds on each client.

I'm having a hard time sorting out in my head what my best course of action is, is it simply a case of doing some lag compensation on each object to ensure the RigidBodies are better in-sync? Or will I need to do something more clever like transferring ownership to MasterClient or disabling collisions on non-Masters?

Any help would be much appreciated, apologies if I haven't been clear enough!

Answers

  • Hi @Macaroon,

    I'm having a hard time sorting out in my head what my best course of action is, is it simply a case of doing some lag compensation on each object to ensure the RigidBodies are better in-sync?


    This might already be helpful in terms of synchronization. Since you are already talking about Lag Compensation, I assume that you have already taken a look at the related documentation page, right?

    Or will I need to do something more clever like transferring ownership to MasterClient or disabling collisions on non-Masters?


    Ownership Transfer might be not a good idea in this case as it will add some visible lag to the game. I think you have two other options. The first one, is to handle collision only on the MasterClient. When the MasterClient detects a collision between two objects, he can send a RPC to both objects applying some force into a certain direction for example. However this might also add some visible lag to the game, guess you would have to check this out. The second option is, that each client handles the collision for his object and only his object. Whenever the client detects a collision, he can apply some force into a certain direction on his object locally. It will be synchronized automatically. This would require adding Lag Compensation before to make sure, that all clients are running more or less the same simulation.
  • Macaroon
    Options

    The second option is, that each client handles the collision for his object and only his object. Whenever the client detects a collision, he can apply some force into a certain direction on his object locally. It will be synchronized automatically. This would require adding Lag Compensation before to make sure, that all clients are running more or less the same simulation.

    Hi Christian,

    Thanks a lot for your reply, this approach seems interesting. As it is a competitive game lag will be quite critical. I've read the documentation page on Lag Compensation and I'm pretty comfortable with the concept, and tried using it on the RigidBody2Ds of the objects to no avail in getting everything smooth.

    The way I understand your suggested approach, I'd try this:

    - In the player's OnCollisionEnter2D, check if photonView.isMine
    - If so, check if collision is another player
    - In this case, analyze the other player's velocity and do some physics calculations to apply a force
    to the local rigidbody
    - RigidBody2D/Transform synchronization would still take place on the object, and this would synchronize the change in velocity to the other clients.

    Is this what you had in mind? Also, would I look to remove Unity's physics from the simulation? ie change all non-local client colliders to triggers? The reason I ask is that the "bounces" are currently not done by applying forces to the rigid bodies, but rather Physics Materials with high bounciness applied to the player colliders. I could anticipate some conflict between the two if Unity is still trying to resolve collisions itself locally
  • Is this what you had in mind?


    Yes, exactly. I guess you have to try different setups at least. For example as you already mentioned you can change all non-local clients to Triggers (or change the local client to a non-Trigger) and see, if you can workaround those "bounces".