I’m making an online game where you’re a sphere with a rigidbody and you can only move on the x and y axis. Basically you can add a force to your sphere by holding mouse 1 and the longer you hold the button the greater the force gets applied when you release. My problem is: How do I smoothly sync the rigidbodies realtime and still get the same results on all clients? I’ve tried just adding a network transform to the player prefab and playing around with the settings but the collisions between the players sometimes acts like one of them hits a wall while the other one keeps slowly rolling like nothing happened. What would be the best way to handle networking in my situation?
Hey there,
what you encounter there is one of the fundamental problems in online games…
Think about it like this: You move your objects based on physics. Every player does so on his own client and will handle collisions based on a local version of your game. When you add your network transform the object you sync is like a kinematic body for everyone else. It’s an object that can not be moved by someone else since the position updates will only happen in one direction - from owner to everyone else.
This leaves you basically with 2 options:
1: Have a masterclient/server handle all collisions. In this variant everyone will only see a synced version of the players objects that get actually moved at the masterclient/server. To move them you simply send the playerinputs to the masterclient/server and have it apply the forces there. Advantage: Fairly easy to implement. Con: might be laggy for everyone. Even when you are moving your own object.
2: Implement some “Custom collision handling”. Whenever you hit other player objects you can calculate the forces that would be applied if the other object was a normal rigidbody in your scene. Then you send a message to the other player to apply forces to his object. Problem is at this point: When you hit his object in your scene, the other player doesn’t nessessarily hit’s your object in his scene due to network delay. So sometimes this might look like you collide with another player and instead of both players getting hit back due to the collision only one of them might “feel the impact”.
Advantages: Can be kind of lagfree. Cons: Can lead to weird/unwanted behaviour. Might be more difficult to implement depending on your setup.
This is pretty much how i’d tackel this problem. In case you have more questions about this or something was unclear feel free to ask.