Golden Path Module Two
Golden Path Two continues on the work from Hello World and Golden Path One to add a few more features.
This guide covers:
- Network variables (client and server-controlled)
- Network transforms
- RPCs
The videos on this page were removed because they were out-of-date and caused more confusion than help. All videos in the Hello World and Golden Path series will be recreated and added back at a later time.
Prerequisites
You should have completed the Hello World project and Golden Path One before starting this tutorial.
Open your Hello World project
- Open Unity Hub.
- Select
Hello World
from the list of projects displayed.
Introducing a Server-controlled Network Variable
This 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.
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);
}
}
}
}
- Save your scene.
Testing Server-controlled Network Variables
Now 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:
Server's var initialized to: 0
Server set its var to: 0.1
Server set its var to: 3.099999
Server set its var to: 6.099997
Since the printing to the terminal does not happen on every tick, the numbers won't match up perfectly.
Introducing Network Transform
This section adds a Network Transform component that will move the player.
- Click Player prefab.
- Click Add Component in the Inspector Tab.
- Select Netcode 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.
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));
}
}
}
- Save your scene.
Testing Network Transform
Now 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 RPCs
This 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. - Open the
RpcTest.cs
script. - 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 (!IsServer)
{
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);
}
}
- Save your scene.
Testing RPCs
Now 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:
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
...
Next Steps
See the following content to continue your journey using Netcode:
- Check out the educational samples to further explore Netcode and its abilities: