This tutorial uses Unity (v2022.3.6f1) and the new input system (v1.6.3)
Hey there! I am going to try to explain how to add local multiplayer to your unity game.
With that out of the way, let’s begin!
You are going to need a ‘Lobby’ and ‘Game’ scene.
Create an empty gameobject named ‘Lobby Manager’, add the script named PlayerInputManager.cs
to it. (This script is included with the new input system)
Set the join behaviour to whatever you like.
As you can see, we need to specify a player prefab. Create an empty gameobject named ‘Player Brain’ and add the PlayerBrain.cs
script to it. The current state should be ‘Lobby’.
The player brain will receive messages from the PlayerInput.cs
script (This script is included with the new input system) and send call functions on the current player. Add it to the gameobject and add an input action asset. Check out this link for more information. Make sure the behaviour is set to ‘Send Messages’.
There are 2 types of players that we need to specify, a player that will be spawned in the lobby scene and a player that will be spawned in the game scene.
Create two empty gameobjects and add PlayerInputHandler.cs
to both of them. This script will handle the input that other scripts on the player can read.
This is how your Lobby Manager should look like:
This is how your Player Brain should look like:
Now it’s your turn to hook the PlayerInputHandler.cs
script into your movement code and create a system to switch all player brains to the game scene.
An update to these script will follow in (hopefully) the following weeks, until then, good luck!