by Antonio, Software Developer at Wovenware
One of my favorite hobbies growing up was playing video games. The truth is that they made my youth a cheerful one, filled with excitement. So, naturally, I wondered about how they work. This is where my adventure into the world of computing started.
Video games are a great medium to tell stories. It is an interactive medium- where you can be part of the experience. Video games revolutionized humanity’s greatest skills, storytelling. If you are reading this, you too probably have a few stories you would love to share interactively or maybe you’re like me, you want to have an interactive experience with others.
The Unity Game Engine
Today, I will teach you how to create a small multiplayer experience using one of the most powerful game engines out there- the Unity game engine. It is a great engine for game making, as it comes with a slew of premade tools that you can mix and match at you’re leisure to craft your game.
A newly integrated tool is the Unity Multiplayer Networking library or MLAPI. This tool allows you to create remote procedure calls with annotations, and it also offers security that was lacking in Unity’s previous networking library.
Let’s Get Started
- Download Unity from the official website, which can be found here: https://unity.com/
- You’re going to create a new project and name it: “MyMLAPITest”. Make sure to select the template of 3D.
- Next, you’re going to install the MLAPI library to the version corresponding to the version of Unity you downloaded. To do this you can follow these instructions https://docs-multiplayer.unity3d.com/docs/migration/install. Now that we have MLAPI set up, our project is ready to go.
Creating our Simple Multiplayer Experience
First, we need to create our network manager for the sample scene in which we will be building our game. Right-click on the hierarchy tab and click on “Create Empty”. Like in the screenshot below:
Now, to this empty Gameobject, we will be adding a component to it. Click on the “Gameobject” we just created.
Next, in the Inspector tab, click on “Add component” and then type “Network Manager”. Add that component to the empty Gameobject.
Next, we need to select a transport for our networking. Click on the “UnetTransport” in the select a transport part of the network manager. Note that you don’t need to know what transport is, just know that it is part of what MLAPI uses for networking.
We are now ready to create the player prefab for when players connect to our server. If you don’t know what a prefab is, it’s a collection of game objects and components that can be stored in a disk and reused in scenes. It is an abstraction that allows us to manipulate complex composite game objects.
Let’s start creating a player prefab by creating a cube as is shown below. Right-click on the scene hierarchy tab and then select “3D Object > Cube”.
Next, we are going to make the cube a network Gameobject by adding to it a component, as we did for the network manager. The difference is this time we are going to add two components: one called NetworkTransform and the other called NetworkObject.
Now, we are going to save our prefab as a file by dragging and dropping the PlayerPrefab Gameobject into the tab below which shows us the project’s files.
Now, we add the prefab to the list of prefabs instantiated when a player joins a server. We do this by clicking on the empty game object we created to have the network manager component. Then, on the NetworkPrefabs part, we click on the “+” icon and drag the player prefab from the file system. Make sure to click on the checkbox “Default Player Prefab”.
Next, we need to download the HelloWorldManager from: https://raw.githubusercontent.com/Apo45ty/GameJam2-GenreMash/master/Assets/Scripts/HelloWorldManager.cs
Download it to the Assets folder inside our current project. Then, add this script as a component to the NetworkManager.
We then add physics to our game. We do this by adding a rigid body to our player prefab. Click on the prefab and then in the inspector tab, adding a component name “RigidBody”.
Here, we can make a point to add material to the PlayerPrefab cube. For more details, you can visit this tutorial: https://www.c-sharpcorner.com/article/how-to-apply-materials-shaders-and-textures-to-objects-in-unity2/
Next, we add terrain upon which our players can fall into. Right-click on the Scene Hierarchy tab and click on “3D Object > Terrain”.
Now, we move our terrain below the position where our players spawn. To do this, click on the terrain object in the scene view and then look at the inspector and edit the values that appear on the transform component to the following:” X:-500, Y:-10, Z:-500″.
Move the camera so that you can see the new players fall on the terrain. To do this, we again click on the scene view, but this time we select the object named Main Camera and then move to the inspector and edit the values that appear on the transform component to the following: “X:0, Y:–10, Z:-10”.
Testing the Game
Let’s compile our game and run it. To do this, go to “File > Build Settings > Player Settings” and in fullscreen mode, select “Windowed” and put in the desired resolution.
Build the game by clicking on “File > Build Settings” and then on “build”. Select the folder where your game will be saved and then open two instances of it.
Once the game is built, open two windows and test that every time a player connects, a new cube is created and dropped. It should look something like this :
In this multiplayer game example, we utilize an authoritative server as our network topology. This means that all clients connect to a central client(Host) who is running the logic for the game. Know that networked games may have other different topologies. Another type of topology would be peer-to-peer, usually used on Real-Time Strategy games.
In this tutorial, we did not have to code because we utilized the build-in code that MLAPI comes with. But, for the more complex games, you can apply a tool that MLAPI comes with. The tool is called remote procedure calls– a powerful way to create network-enabled software.
MLAPI wraps around Unity libraries, allowing you to use NetworkedObjects. NetworkedObjects are the basic building block of all network-enabled game objects. In the body of networked objects, MLAPI allows you to specify remote procedure calls from server-to-single-client, server-to-all-clients, or client-to-server. This allows us to implement any kind of network behavior we wish for our game to have.
Apart from enabling remote procedure calls, MLAPI comes with special access modifiers that allow us to put in place locks and safeguards against hacking or other player modifying an object they should not be able to.
If you want to see more examples of games built with Unity and MLAPI, you can view a game I made in it at: https://apo45ty.itch.io/rocketsoccer. I made this for the Genre Mash mini biweekly Itch.io Gamejam. The source code for it can be found here: https://github.com/Apo45ty/GameJam2-GenreMash.
Games are the reason why I got into computers, and I can truly say I am very happy with my decision. Computer science is a very challenging and changing field and it is constantly growing. Video games are a very good way to get into computer science, as they allow you to visually see your work in action. The knowledge of networking I gain through my participation in game jams helps me better understand some of the technology stacks out there.
Sign up for our Monthly Newsletter: