Using the new Unity input system with MLAPI in a multiplayer game (Part 1)

Brandon Lara
Geek Culture
Published in
7 min readAug 17, 2021
Photo by Hello Lightbulb on Unsplash

MLAPI it’s the new mid-level networking library for unity. This reduces the networking code and removes the repetitive network task by providing high-level abstractions of networking that let us focus on the game development. MLAPI is open source with no attached costs or limitations, all provided for free.

The input system implements a new way to use any input device to control de Unity content. It’s intended to be a more powerful, flexible, and configurable replacement for Unity’s classic Input Manager.

In this guide, I will show every step to develop a working multiplatform control using the new Unity input system, for multiplayer games that use MLAPI.

The guide is divided into 2 parts, the first part shows how to configure the Unity New Input System and the second part shows the multiplayer through MLAPI and the implementation of the movement of network players, this second part can be found at the following link:

You can see in detail the contents of each part of the guide below:

Contents of part 1:

  • Install Input System Package.
  • Create the Input Actions.
  • Create the Player Prefab.
  • Create the Player Movement Script.

Contents of part 2:

  • Install MLAPI Package.
  • Create the Network Manager Object.
  • Create the World Manager Object and Script.
  • Implement the Networking Movement.
  • Testing the game

In addition, you can check the entire Unity project of this guide at the following link:

Install Input system package

First, we have to install the new input system package through Unity’s package manager, which is opened from the menu: Window > Package Manager. We have to select the Packages from Unity Registry in the Packages menu at the top, pick the Input System package from the list, and click Install.

Create the input actions

After installing the input system package, we have to create the input actions asset by right-clicking on the asset windows and selecting Create>Input Actions in the menu:

This creates the input action asset and lets us choose the name of the asset. Double-clicking on this asset in the Project Browser, or select the Edit Asset button in the Inspector for that Asset, opens the Action editor, where you can edit the Actions settings to fit the needs of your Project.

By default, Unity doesn’t save edits you make in the Action Asset window when you save the Project. To save your changes, select Save Asset in the window’s toolbar. To discard your changes, close the window and choose Don’t Save when prompted. Alternatively, you can toggle auto-saving on by enabling the Auto-Save checkbox in the toolbar. This saves any changes to that Asset.

As an example for this guide, the movement of the character will be from left to right on the X-axis using two keys (A and D). To carry out this particular example, in the Action editor I will configure the actions and bindings of the player so that the “A” key returns a positive value and the “D” key always returns a negative value. In this way, we can know which key is being pressed at any given moment and move the character accordingly.

As you can see, I created an action map named player using the + button on the left pane, this action map is only a collection of actions that can be enabled or disabled in bulk. Then I added an action named move using the + button on the centre pane, this name reflects what action is going to do with the binding associated. In this case, this Action has a 1D axis composite associated that has 2 bindings, the A key binding returns a negative value, and the D key binding that returns a positive value.

That's all the binding configuration for one device (Keyboard), but if you want to make it multi-device you have to add one control scheme for each device you want to use in your game. This can be done in the top menu: All Control Schemes > Add New Control Scheme.

I recommend using only one control scheme because it has a problem when you have multiple devices in a multiplayer game. This problem consists of a wrong device assignation when the network manager instantiates each player prefab since each clone has a different device assigned to it when it would be correct that the clones have the same device assigned to them and the device changes are made for all clones so that they always have the same device assigned to them.

Create the player prefab

Next, it is required to create the player if it has not been done yet. In my case for this example, I'm going to create a 2D square prefab as the player.

Now we have to add the Player Input component to the player prefab, and then add the Input Actions asset created before to the Actions property on the Player Input component as shown below:

Also, we have to establish the behaviour property in the Player Input component, here you can choose the most convenient for what you are going to do, in my case, I am going to select the invoke unity events behaviour. This means that certain events of the component will execute the callbacks that we will assign later. These events depend on how we have designed the input actions asset, but other default events will always appear.

Create the player movement script

The next step depends on how are you going to implement the movement of the player, that you are going to need a function for each action you created in the Action Editor, each Action has to be routed to a target method, which through its parameter you can obtain the value of the control that has triggered the action.

As we already know, in my case I only have one action called move that has a 1D axis composite where the A key pull in the negative direction (min value: -1) and the D key pulls in the positive direction (max value: 1). This means that the move action callback method it's going to be triggered when the A or D keys are pressed, and the value obtainable from the parameter it's going to be -1 when the A key was pressed and 1 when the D key was pressed.

With all of this in mind, I created the player movement script asset, named Player.cs, where I implemented the OnMovement method for handling the Move action.

Once we created our movement script and action method handler, we have to add the script as a component to the Player prefab.

Next, I assigned the OnMovement method to the Move player event in the Player Input component of the player prefab:

Now the OnMovement method will be executed every time I press the A or D keys, indicating through the parameter -1 if the A key has been pressed or 1 if the D key has been pressed.

This is all for the first part, now we have to do all the necessary Multiplayer configuration using MLAPI and create the network movement logic using what has already been done with Unity’s new input system, I cover all of this in the second part of this guide which you can access with the following link:

If you have any doubts or suggestions about this part, you can comment to me without any problem😉.

I hope this has been helpful 👍

Cheers.

More information on the topics in this guide can be found in the following links:

Also, the following video tutorial is very good and interesting because it is quite related to this first part of the guide:

--

--