Lan Multiplayer Scene Change
Asked Answered
P

1

10

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

Pin answered 16/5, 2018 at 19:46 Comment(7)
Question aside: When the battle players enter a battle, what are the other players doing during the time?Bushman
@LeonWillens Sorry, i fell asleep. To answer your question, Let's say if there are 3 players, two will enter a new "battle" scene and the other player will stay on the the current scene.Pin
Just looking through the API reference for NetworkManager and came across the onlineScene property. The reference states "This scene will be switched to when a network session is started - such as a client connect, or a server listen." It's possible that this might be your solution.Gangue
Which network system are you using? Unet? Photon? Seems like you want to make a "nested network manager", so in case you are using Unet, maybe you need to re-think the lobby manager, cause it won't be "dondestroyonload" anymore, and make another lobby manager on the second scene, which leads to the third. Am I clear? It's a little bit confusing..Ainsley
@Ainsley I guess I am using Unet. Basically I followed the instructions on the link I provided above. So are you saying I might need separate lobby managers on each scene or will these lobby managers be connected somehow?Pin
Yes, If you are following the tutorial you're using Unet. The idea (I don't really know if Unity allows this) is to "fake" one lobby inside another. So you have your main lobby, and 4 players join this lobby and go together to the first scene (scene1). On that Scene1 you do the same lobby-logic again, so the players who match the second-lobby conditions, will change to the Scene2.Ainsley
@Ainsley Ahh I see what you mean now. I will play around with this idea in mind when I get the chancePin
A
3

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

Ainsley answered 2/7, 2018 at 9:58 Comment(1)
Faking the scene might work for me if nothing else works.Pin

© 2022 - 2024 — McMap. All rights reserved.