Skip to main content

Golden Path Module Two

In this Guide e will be covering the following:

  • Network variables (client and server-controlled)
  • Network transforms
  • RPCs

Requirements#

You should have completed the foundation module here before starting this tutorial..

Create a Clone 'GoldenPath' project#

important

Cloning your Foundation GoldenPath project does not support rollback, so it will only work on the Unity version that it was originally 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 for further Golden Path modules, if you have not yet created a GoldenPath project then you should follow the steps here to create one. 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#

  1. Navigate to where your Golden Path project is stored.

    note

    For the video guide we used a Mac and our path was ~/dev/mlapi-golden-path/. On a Windows machine your path may be slightly different. As one of our writer's stepped through the process in both Mac and Windows, these are her comparative paths:

    • Mac: ~ GoldenPath
    • Windows: C:\Users\sarao\GoldenPath :::
  2. Create a new folder and name it Golden Path_<modulenumber>.

    tip

    For the purposes of the Golden Path 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.

    Our comparative paths of the new folder for module one:

    • Mac in video: ~/dev/mlapi-golden-path/Golden_Path_One
    • Mac: ~GoldenPath_One
    • Windows: C:\Users\sarao\GoldenPath_One
  3. Open your GoldenPath folder from the Foundation module, select all the content within to Copy, then Paste into your new Golden Path_<modulenumber> folder.

From the Video

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#

To add your clone project in Unity Hub version 3.0.0+: 1. Open Unity Hub. 1. From the dropdown arrow between **Open** and **New project**, click **Add project from disk**. 1. Navigate to where the clone folder is saved, select `GoldenPath_<modulenumber>`, and click **Open**/**Select Folder**.

The clone project should now appear in your projects list of the Unity Hub as GoldenPath_&lt;modulenumber&gt;.

To add your clone project in previous Unity Hub versions: 1. Open Unity Hub. 2. Click **Add**. 3. Navigate to where the clone folder is saved, select `GoldenPath_<modulenumber>`, and click **Open**/**Select Folder**.

The clone project should now appear in your projects list of the Unity Hub as GoldenPath_&lt;modulenumber&gt;.

Open GoldenPath_Two#

  1. Open Unity Hub.
  2. Select GoldenPath_Two from the list of projects displayed.

Introducing a Server-controlled Network Variable#

This section adds a Server-controlled Network Variable to the project.

  1. Open the Scripts Folder.
  2. Create a script called NetworkVariableTest.
  3. Click the Player prefab.
  4. In the Player prefab Inspector tab, click Add Component.
  5. Click Scripts, and add the NetworkVariableTest.cs script you created earlier.
  6. Open the NetworkVariableTest.cs script.
  7. Edit the NetworkVariableTest.cs script to match the following.
Click to show/hide the Code.
using Unity.Netcode;
using UnityEngine;
public class NetworkVariableTest : NetworkBehaviour
{
private NetworkVariable<float> ServerNetworkVariable = new NetworkVariable<float>();
private float last_t = 0.0f;
public override void OnNetworkSpawn()
{
if (IsServer)
{
ServerNetworkVariable.Value = 0.0f;
Debug.Log("Server's var initialized to: " + ServerNetworkVariable.Value);
}
}
void Update()
{
var t_now = Time.time;
if (IsServer)
{
ServerNetworkVariable.Value = ServerNetworkVariable.Value + 0.1f;
if (t_now - last_t > 0.5f)
{
last_t = t_now;
Debug.Log("Server set its var to: " + ServerNetworkVariable.Value + ", has client var at: " +
ClientNetworkVariable.Value);
}
}
}
}
  1. Save your scene.

Testing Server-controlled Network Variables#

Now we will test the Server-controlled Network Variable works as we intended.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. You should see the following in the console, showing that the server and client are sharing the variable:
Server's var initialized to: 0
Server set its var to: 0.1, has client var at: 0
Server set its var to: 3.099999, has client var at: 2.6
Server set its var to: 6.099997, has client var at: 5.599997
note

Since the printing to the terminal does not happen on every tick, the numbers will not match up perfectly.

Introducing Network Transform#

This section adds a Network Transform component that will move the player.

  1. Click Player prefab.
  2. Click Add Component in the Inspector Tab.
  3. Select Netcode from the list shown.
  4. Select the Network Transform component from the list shown.
  5. Open the Scripts Folder.
  6. Create a script called NetworkTransformTest.
  7. Click the Player prefab.
  8. In the Player prefab Inspector tab, click Add Component
  9. Click Scripts, and add the NetworkTransformTest.cs script you created earlier.
  10. Open the NetworkTransformTest.cs script.
  11. Edit the NetworkTransformTest.cs script to match the following.
Click to show/hide the Code.
using System;
using Unity.Netcode;
using UnityEngine;
public class NetworkTransformTest : NetworkBehaviour
{
void Update()
{
if (IsServer)
{
float theta = Time.frameCount / 10.0f;
transform.position = new Vector3((float) Math.Cos(theta), 0.0f, (float) Math.Sin(theta));
}
}
}
  1. Save your scene.

Testing Network Transform#

Now we check that the Network Transform functions correctly.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. You should see the player capsule moving in a circle on both the client and the server.

Introducing RPCs#

This section adds some basic RPCs to the project.

  1. Open the Scripts Folder.
  2. Create a script called RpcTest.
  3. Click the Player prefab.
  4. In the Player prefab Inspector tab, click Add Component.
  5. Click Scripts, and add the RpcTest.cs script you created earlier.
  6. Right Click Player prefab.
  7. Open the RpcTest.cs script.
  8. Edit the RpcTest.cs script to match the following.
Click to show/hide the Code.
using Unity.Netcode;
using UnityEngine;
public class RpcTest : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
if (IsClient)
{
TestServerRpc(0);
}
}
[ClientRpc]
void TestClientRpc(int value)
{
if (IsClient)
{
Debug.Log("Client Received the RPC #" + value);
TestServerRpc(value + 1);
}
}
[ServerRpc]
void TestServerRpc(int value)
{
Debug.Log("Server Received the RPC #" + value);
TestClientRpc(value);
}
}
  1. Save your scene.

Testing RPCs#

Now we will test that the client and server are both recieving the RPCs correctly.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. In the console, you should expect to see the client and server sending RPC messages to each other.
  6. The client kicks off the exchange in its Update call the first time with a counter value of 0.
  7. 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:
Server Received the RPC #1
Client Received the RPC #1
Server Received the RPC #2
Client Received the RPC #2
Server Received the RPC #3
Client Received the RPC #3
...
Congrats!

Congratulations you have learned how to use some of the basic building blocks of Multiplayer game creation.

Next Steps#

For 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.