Hello, I would like to know how to sync a soccer ball for all connected players in the Netcode for GameObjects.
The only possible solution is to predict the ball position on the server side, but I don’t clearly understand the Netcode structure, because it says that Physics already simulated on server side if I will add NetworkRigidbody2D component to the object. But at the same time I see that it is not working at all and ball position is controlled by the first player who created the session/room.
Is there any simple solution for the simple soccer game?
I need only ball to be synced and all the players can interact with it, without changing the Ownership…
In the documentation, we can read that Netcode approach allows “server-authoritative physics where the physics simulation only runs on the server”.
As it is explained below this, you can add a NetworkRigidbody to your rigidbody, which will run the physics simulation on the server, and clients will set the rigidbody as kinematic and move the object based on positions that the server send them.
You can also use a NetworkRigidbody in an owner-authoritative way, meaning that not the server but the owner of the rigidbody will be running the physics simulation, and send the position to other clients (going by the server)
You are indeed asking for Rigidbody prediction : when clients interact with a rigidbody, they move it locally instantly and send the movement to the server, which will run the simulation to see if the prediction is correct or not (if not, rollback is to be expected on clients). This is currently not available inside Netcode for GameObjects, but I do believe this is available inside Netcode for Entities. As it might not be what you want to go for, you can find prediction inside Fish-Net for example, a free alternative solution for networking. There might be other networking solutions that could offer client-side prediction.
If you want to stick with Netcode, without using their physics approach, you might want to implement networked physics interactions by yourself, or find help on Github.
If I’m not mistaken, Unity’s development team for Netcode is trying to merge both of Netcode for GameObjects and Netcode for Entities, which might include client-side prediction available at a later date, but I don’t know when would that be.
Not really a good news for you, but I hope it guides you to what is best for your project
Honestly for a 2D soccer game I wouldn’t even use a physics engine, especially not when networking as networked physics for gameplay is incredibly challenging to get right.
Assuming an arcade style soccer game, all the physics you need for the ball is a distance check, a damping of linear velocity over time and depending on whether the ball is on ground, and perhaps some amount of circular motion depending on some sort of spin that I leave up to you to determine.
Ultimately, you’ll be more in control of the feel of the game feel and you only need to sync the ball’s transform position.
What’s a “room system”? Perhaps it’s just a matter of terminology.
Usually the system where players meet up and find and agree to play a match is called Lobby or Matchmaking or both in combination. Lobby is where (friend) players meet, matchmaking is where players get matched with (friend or anonymous) players and a reservation for the game session is made with a server instance.
Unity’s Game Hosting Server allows for multiple server instances per machine, but as usual, those are pretty limited. With 2 cores and 8 GiB of RAM I would expect the upper server instance limit to be no more than 10 to 20 - provided the game is heavily optimized. The machine will be either CPU or even more likely limited by its RAM. Consider that each server instance can use no more than 500 MiB of memory if you were to support 16 server instances but a typical server build with little code and assets in it will already consume maybe half of that memory!
Naturally, because the host controls the physics simulation, thus the remote player’s latency determines the lag imposed upon his physics interactions while the host has no latency at all.
Like I said earlier, for this sort of play you don’t use a physics engine at all. You can do the collision detection and motion of the ball yourself since it’s extremely simple to handle, and then you also have more control over how the game plays.
Then you can assign the ownership of the ball to the player who last touched the ball so that the “owning” player will have immediate feedback on the ball’s play (ball motion is always simulated on the owner’s machine and synchronized to the other player) while the “defending” player would first need to take control back of the other player.
How you implement that is up to you but you have to have that sort of system for a competitive game if you want to go with client-hosted sessions. Otherwise the host will have a significant competitive advantage due to having no latency in the game. This is true for all networking frameworks btw.
This is largely 90% work that you have to do yourself, the network frameworks will only provide a little more or less benefits in certain areas but ultimately this is not a game where you need to go hunting for the “right” network framework. Any will do, and then you’ll have to add your rules and behaviours to it.