Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lan Multiplayer Scene Change

I am new to unity and currently trying to make a LAN multiplayer RPG game.

FYI, I have followed the official unity lan multiplayer guide and everything went well. https://unity3d.com/learn/tutorials/topics/multiplayer-networking/introduction-simple-multiplayer-example

So far I got the players to load in and they are able to move. I wrote the following code below (under the void update routine) so that when the player is moving, it will randomize a number between 1 & 50 every 1 second and if the number is 25, we have randomly "encountered an enemy". When any player encounters an enemy I made it so everyone on the network goes to the "battle scene".

if (Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right"))
    {
        if (Time.time > NextActionTime)
        {
            NextActionTime = Time.time + Period;
            EnemyEncounter = Random.Range(1, 50);
            if (EnemyEncounter == 25)
            {

                NetworkManager.singleton.ServerChangeScene("Scene2");
            }
        }
    }

The code above works fine but I am not sure how, instead of loading everyone, to load only certain players into the battle scene.

For example:
Players enter a name before Hosting/Finding LAN game


Player 1 = Joe
Player 2 = Bob
Player 3 = Billy
Player 4 = Jim

On a preset label/text that loads the text in it saying "Joe,Billy". Now when ANY player finds an encounter, I want to ONLY load the players name "Joe" and "Billy" to the next scene while the others do not.

Is this possible? Any kind of help will be greatly appreciated.

Thanks All

like image 577
NoobProgrammer Avatar asked May 16 '18 19:05

NoobProgrammer


1 Answers

I was trying different ideas and I got 2 different approaches:

1-As I already tell on the comments, try to nest lobbyManagers

2-"Fake" the scene split on lobbys

1.Nested Lobbys

Concept:

  • First Scene, MainLobby, 4 players enter, and go to the second scene

  • Second Scene, MainGame+SecondLobby, there are 4 players that comes from the first scene, but now 2 of them wants to go the third scene, so they use the SecondLobby to matchmake again.

  • Third Scene, SecondGame.

This is the best aproach I think if we are talking about performance, but it's elaborate cause:

-Actual Unity NetworkLobby uses singleton pattern, so you need to code the singleton parts again.

-LobbyManagers are builded with the DontDestroyOnLoad, so you were charging another lobby on your next scene.

-I don't really know if you can go back from third scene to second one :S

2.Fake Scenes

Well, welcome to "dirty tricks", the second concept is to:

  • First Scene, MainLobby, 4 players enter, and go to the second scene

  • Second Scene, MainGame, there are 4 players that comes from the first scene, but now 2 of them wants to go the third scene.

  • Third Scene, SecondGame.

But instead of "matchmake" again, what we do is to add the scene as an additive scene, but on different coordenates, and move the 2 players that wants to battle on the thirdScene. So the players will think that they are on different scene, but they aren't they are just being move. Thinks to get in mind:

-Maybe you don't really need to use the additive scene, just build on the same scene, on different coordenates. (https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html)

-Think that they still be 4 networked players on the same scene, so maybe you want to "disable" some networkMessages, to only affect certain players on certain "scenes". (https://docs.unity3d.com/ScriptReference/Networking.NetworkClient.Send.html)

But if you achieve some other approach let me know, it really gives interesting game design stuff! :D

like image 164
Lotan Avatar answered Sep 20 '22 18:09

Lotan