3
\$\begingroup\$

I am working on a multiplayer project that involves loading into different scenes and playing some kind of mini-game with any other players that happen to be in the scene at that time. These mini-games will contain AI that needs to be controlled by a dedicated server. Additionally, there is simulation that needs to happen in these scenes, even if no player is currently in one.

My first idea to take a crack at this was to have my server on startup, load all of the scenes in the game using LoadSceneMode.Additive This way, on the server side, each scene would be in one place and available to be simulated.

I made a little sample project with three scenes, a base world, and two other worlds. When the server is started from the Base World scene, it loads World1 and World2 into its scene. From my server, I could spawn a cube in World1 by pressing the 1 key, and I could spawn a cube in World2 by pressing the 2 key.

I used the following code to spawn the cube:

    GameObject cube = (GameObject)Instantiate(myPrefab, new Vector3(0, 10, 0), Quaternion.identity);
    NetworkServer.Spawn(cube);

The problem is, if a cube is spawned in World1, clients in World2 still spawn the cube. In hindsight this makes sense because the documentation for NetworkServer.Spawn states

Spawn the given game object on all clients which are ready.

My next idea was that I could designate one player in the minigame as a psuedo "host" and his machine would be responsible for running the AI. However, with this method, I really cant simulate things going on when nobody is in the scene.

Is there a better way I should go about solving this problem? How can I create a dedicated server which can simulate multiple scenes at once? Should each scene be handled by its own server? Am I better off just trying to run everything in one scene?

\$\endgroup\$
0
\$\begingroup\$

Unity's built in networking honestly isn't meant for this type of stuff, its meant for for stuff like RedAlert, StarCraft, can't remember some of the others that use the peer to peer system but you should get the point with those 2. its more of a peer to peer system although unity can run an authorative server but really only 1 scene at a time. I would suggest looking into Photon Server for this. if you get the on premise version instead of the cloud version you can run your own server/servers with authorative logic and even connect them together in a cluster with a master relay server and then run the scenes that way, although you need something like bullet physics to control the objects server side because it doesn't load the actual unity scene but a serialized export of the scene geometry. Photon can even handle a mmo type of game and they have a small example of it using the old Unity Island Demo scene.

Photon servers are also coded in C# as a class library(dll) that the main system loads. Its a pretty nice system.

Just an idea of something to look into for you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.