The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PvP Collision Based Multiplayer

Macaroon
2018-04-19 10:52:09

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!

Comments

[Deleted User]
2018-04-19 13:53:53

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
2018-04-19 14:52:42

@Christian_Simon wrote:

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

[Deleted User]
2018-04-24 09:00:19

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".

Back to top