2

I have managed to sync my ridigbodies real good but I just noticed that when a client (that's not the master) collides with one it doesn't move, it's locked, while reading on the net I had an idea when the object collides with a rigidbody I just transfer the ownership of that object to that client for the moment for this client to process the collision (I have tested this and it works really good, both clients sync perfectly)

is this recommended? performance-wise and security/cheating wise?

Thanks!

Hex
  • 404
  • 7
  • 21

1 Answers1

3

Short answer:

Transferring ownership of a GameObject to a client, when that client needs to control and update the object data, is fine. In cases of detecting cheating possibilities, always have one client (typically the master client or a dedicated server) validate, correct and sync the data to all clients.

I will not make any assumptions. Based on your explanation of the issue, it seems your GameObject Rigidbody IsKinematic was not being synced, causing the collisions having different effects at each client end. But in case that might not be the issue, I have a longer answer hoping it might help for this or other scenarios.


Longer version:

Sync Rigidibody data over the network where it is fundamentally required to be present on the client GameObject. The rest of the Rigidbody data can be created / calculated / simulated on each client individually:

  • This way, you will make sure all parameters of that Rigidbody are correct (e.g. isKinematic is correctly enabled/disabled on all clients)
  • Your games will always have the minimum required data to create/simulate the rest of the events and effects properly, reducing network lags and improving performance.
  • Where data validation is required, the master client will always validate the non-master client data, makes the crucial adjustments and decisions and updates the room data for all clients where necessary. This reduces the chances of players cheating.
  • Developers will find bugs easier when the events that are supposed to take place on each client, do not align with what is expected. This shows itself more often in scenarios where several events should take place at the same time and on each client.

Much longer version:

When I use PUN2 on GameObjects in my games, I only sync Rigidbody where those GameObjects require Rigidbody values to be present on the clients during collisions to perform specific tasks (e.g. using the velocity or direction for dynamic sound/visual effects), or when the master is validating and correcting the data needed for the non-master clients (as a result of network delay or cheat/hack tampering the data on client side).

When using PUN2 (up to this date), I update its class internal code to my needs. For example, PhotonRigidbodyView does not sync the kinematic state of a Rigidbody yet. When I need this in my games, I could override the class with my CustomPhotonRigidbodyView class (and/or updating the PhotonRigidbodyView a bit).

Does it effect the performance when using PhotonRigidbodyView? Yes, when not used with care, it does decrease it when there are too many objects syncing over the network at the same time. The clients will see jagged movements.

Does it allow cheaters to take advantage of this data, manipulate it and send it over to the clients? Yes, if the game is vulnerable to basic cheats. Then again, the topic of security/cheat detection is quite vast, so for each game definition of "basic" cheats are different.

Is it a good idea to transfer ownership momentarily to be processed on client side? Yes in some cases, other cases no:

  • If doing this momentary transfer is the only place you do this and it solves all your problems (except cheating), then use it so you won't spend too much time fixing this one part in a different way.
  • If the Rigidbody or Transform data is being validated and/or synced, either constantly or in periodic intervals by one client (to prevent cheating or to sync client data properly at crucial times), then it is fine to transfer momentarily too.
  • I typically transfer ownership, when the object data must be changed by the client (e.g. grabbing / dragging around / shooting / throwing) and once the action is performed, ownership will be back to the master to be validated and synced for all clients (including the one that just transferred the ownership). This results in more time spent on testing, catching unwanted (possibly unseen) bugs, and reduced cheating possibilities.

In case a developer working on your game has not properly optimized the Collider and/or Rigidbody components for each GameObject yet, please make sure you have done so before syncing their data over the network. This page in Unity doc may be a good reference to check whether your GameObjects need to be static colliders or dynamic colliders:

Once the GameObjects that need a Rigidbody component are identified, then you can decide which ones need to be fully synced over the network or partially synced and/or their ownership transferred when required.

I hope my answer does not confuse any of the readers. If there is anything not clear, please mention in the comment and I will improve my answer for you.



Update: 2019-05-10 (reply to the 2nd comment):

For a pool game involving so many GameObjects, I would not transfer the ownership of all objects to a client. I would:

  • Create a struct holding the necessary information such as:
    • ball_id: indicating which ball is being hit by the pool stick
    • hit_info: a Vector3 representing the position (on the ball surface) and direction of the hit applied to the ball, by the hit stick.
    • hit_force: the required force to apply to the Rigidbody of the ball that is being hit by the hit stick.
    • and any other information that I might need, when the player hits the ball with stick.
    • I can then combine this information and pass them to the AddForceAtPosition method of the Rigidbody of the ball.
  • Dispatch this struct in the room, for all the clients to receive.
  • Once each client receives this message, including the sender, (in OnEvent as EventData containing the Phton Event Data), it will apply this info to the correct ball and will create its own version of physics (small differences will be noticed between each client).

This way:

  • I have the minimum amount of information passed over the network to make the simulations happen on all clients
  • Improved performance of the game by avoiding parsing and applying unnecessary Rigidbody data
  • Reduced the cheating possibilities of a client moving any of the other balls that they are not supposed to, around the table or into the pots.

To increase security as well as making sure the balls on all clients are exactly where they are supposed to be, another struct will be dispatched for example by the master client, indicating the exact position (and rotation) of each ball on the pool table (or in the pot).

I would sync the ball positions periodically (either at pre-defined intervals or at specific moments of my choosing depending on the game state) to all clients to overcome any incorrect positions at any moment in the game. This periodic sync can be done by the master client or a dedicated server.

Armin
  • 1,807
  • 20
  • 21
  • At this moment a tried a lot of ways to simulate and I've been using this: https://assetstore.unity.com/packages/tools/network/smooth-sync-96925 – Hex May 10 '20 at 01:56
  • I'm working on a prototype pool game to learn how pun2 works because bolt is really expensive, so my plan was when the user gets the turn, they get the ownership of every object that needs its collision calculated and when his turn is done the next in line gets the ownership, is this ok? it seems the security of unity and pun2, in general, is really lacking by design. (Basically rotate the ownership between players to calculate the physics and trajectories easily) – Hex May 10 '20 at 02:04
  • 1
    I updated my answer to reply to your 2nd comment. For the first one, `Smooth Sync` is fine, if you only use it for your pool game from one client (e.g. master) to sync the object exact positions periodically, without transferring the ownership. Of course, this applies to a pool game, and not every other game, since each client must know the exact position of the objects at given times. – Armin May 10 '20 at 13:50
  • I see, basically, every client will handle its own physics locally, now here's another idea if I can, I was thinking instead of rotating the ownership just to have an RPC function "add force" for example, then I just call the ball, stick etc and apply the force while using the smooth sync everything will be 100% on sync, on my current test the collisions and everything have been 100% correct across devices, would that be advisable? – Hex May 10 '20 at 14:03
  • 1
    Yes, considering your explanation of your scenarios, it is a good idea to have `RPC` with `SmoothSync`, without rotating the ownership of all objects. For ownership transfer, I tend to do it as less as possible (only do it when it is absolutely necessary, for example in my VR games where throwing velocity needs to be calculated by a client holding an object in their hands, ownership might be temporarily transferred). – Armin May 10 '20 at 17:08