Search code examples
c#unity-game-enginemultiplayer

Multiplayer Game with players on separate versions of the same map


I'm making/want to make a bullet-hell game where there are essentially two players, each own their own identical copy of a map competition for the high score. And the game ends whenever a player dies.

I have all of this working in multiplayer, but my approach seems a bit hacky, so I'm wondering if there's a better way for me to accomplish this.

At first I was using just unity's network manager, and in my player Start() function I checked if(!isLocal), and if so I set the gameObject enabled to false. This worked great, but like I said, felt a little hacky.

Example of the code:

if (!isLocalPlayer) {
       gameObject.SetActive(false);
       return;
    }

Next I moved to unity's LobbyManager. This is where things got really sticky. Now on the Host, the game loads fine, but on the client, only one game object is created, and it's set as disabled which leads me to believe that it is the 'enemy' or not local player object.

I slowly figured out that the cause of this was setting the enemy game object active to false. If I left it true, both players would spawn on both screens. My solution now is to not disable the enemy player object, but every component on it so it doesn't get in the way.

Again this feels very hacky, and like it could lead to problems down the road. Is this really the best option, or am I missing something obvious?

Example of the Code:

if (!isLocalPlayer) {
    gameObject.GetComponent<MeshRenderer>().enabled = false;
    gameObject.GetComponent<BoxCollider>().enabled = false;
    gameObject.GetComponent<CharacterController>().enabled = false;
    gameObject.GetComponent<PlayerController>().enabled = false;
    gameObject.GetComponent<Rigidbody>().detectCollisions = false;
    guns = gameObject.GetComponentsInChildren<MeshRenderer>();
    foreach (MeshRenderer gun in guns) {
        gun.enabled = false;
    }

}

Thanks in advance! Sorry this is so long, hopefully it isn't a chore to read.


Solution

  • Make an empty scene with nothing but a score manager and what else you need to transfer.

    Have another scene as an asset in your project folder, this is where the map is.

    When a player joins, have them join the scene with the score manager.

    If they also are the local player, they should load the map scene.

    That way, both players will be in the same scene while also having their own instance of the map.

    You can load scenes in asynchronously and additively, which would be ideal for your situation.