Golden Path Module Two
In this Guide we will be covering the following:
- Network variables (client and server-controlled)
- Network transforms
- RPCs
#
RequirementsYou should have completed the foundation module here before starting this tutorial..
#
Create a Clone 'GoldenPath' projectimportant
The clone does not support rollback so it will only work on the unity version that it was originaly created for. Attempting to open the clone on a newer or older version of Unity may cause unexpected funcationality.
We create a clone of the Golden Path foundation project that you created, if you have not yet created a GoldenPath
project then you should follow the steps here to create one.
tip
Working from a clone of the initial project will help you learn about new features and concepts without having to repeat previous work.
#
Create a clone of GoldenPath- Navigate to where you Golden Path project is stored. For this guide we used a Mac and our path was
~/dev/mlapi-golden-path/
. On a Windows machine your path may be slightly different. - Create a new folder.
- Rename the new folder
Golden Path_<modulenumber>
.tip
For the purposes of this series I will replace
modulenumber
with the current module number of the series. You do not have to though you may find it makes following the series easier. - Open the
Goldenpath
folder. - Select Copy.
- Select the folder containg your Golden Path project.
- Select Paste.
tip
I moved my new folder to keep my file structure clean its not a required step, but it does look nice.
#
Add the clone to Unity Hub- Open Unity Hub.
- Click Add.
- Navigate to where the clone was saved.
- Select
GoldenPath_<modulenumber>
. - Click Open.
The Clone should now appear in the project list of Unity Hub as GoldenPath_<modulenumber>
.
#
Open GoldenPath_Two- Open Unity Hub.
- Select
GoldenPath_Two
from the list of projects displayed.
#
Introducing a Server-controlled Network VariableThis section adds a Server-controlled Network Variable to the project.
- Open the Scripts Folder.
- Create a script called
NetworkVariableTest
. - Click the Player prefab.
- In the Player prefab Inspector tab, click Add Component.
- Click Scripts, and add the
NetworkVariableTest.cs
script you created earlier. - Open the
NetworkVariableTest.cs
script. - Edit the
NetworkVariableTest.cs
script to match the following.
Click to show/hide the Code.
- Save your scene.
#
Testing Server-controlled Network VariablesNow we will test the Server-controlled Network Variable works as we intended.
- Select File > Build and Run.
- Stop the player.
- Launch the client and server together in a terminal as shown in Testing the command line helper.
- After a brief delay, the client and server will spawn.
- You should see the following in the console, showing that the server and client are sharing the variable:
note
Since the printing to the terminal does not happen on every tick, the numbers will not match up perfectly.
#
Introducing Network TransformThis section adds a Network Transform component that will move the player.
- Click Player prefab.
- Click Add Component in the Inspector Tab.
- Select MLAPI from the list shown.
- Select the Network Transform component from the list shown.
- Open the Scripts Folder.
- Create a script called
NetworkTransformTest
. - Click the Player prefab.
- In the Player prefab Inspector tab, click Add Component.
- Click Scripts, and add the
NetworkTransformTest.cs
script you created earlier. - Open the
NetworkTransformTest.cs
script. - Edit the
NetworkTransformTest.cs
script to match the following.
Click to show/hide the Code.
- Save your scene.
#
Testing Network TransformNow we check that the Network Transform functions correctly.
- Select File > Build and Run.
- Stop the player.
- Launch the client and server together in a terminal as shown in Testing the command line helper.
- After a brief delay, the client and server will spawn.
- You should see the player capsule moving in a circle on both the client and the server.
#
Introducing RPCsThis section adds some basic RPCs to the project.
- Open the Scripts Folder.
- Create a script called
RpcTest
. - Click the Player prefab.
- In the Player prefab Inspector tab, click Add Component.
- Click Scripts, and add the
RpcTest.cs
script you created earlier. - Right Click Player prefab.
- Open the
RpcTest.cs
script. - Edit the
RpcTest.cs
script to match the following.
Click to show/hide the Code.
- Save your scene.
#
Testing RPCsNow we will test that the client and server are both recieving the RPCs correctly.
- Select File > Build and Run.
- Stop the player.
- Launch the client and server together in a terminal as shown in Testing the command line helper.
- After a brief delay, the client and server will spawn.
- In the console, you should expect to see the client and server sending RPC messages to each other.
- The client kicks off the exchange in its
Update
call the first time with a counter value of 0. - It then makes an RPC call to the server with the next value. The server receives this and calls the client. In the console view, you will see:
Congrats!
Congratulations you have learned how to use some of the basic building blocks of Multiplayer game creation.
#
Next StepsFor more information on the relevant concepts introduced here please refer to the following sections of the documentation:
Special Thanks
This guide would not have been possible without the hard work and support of Matt Walsh, Unity.