Skip to content

Commit 62e56a2

Browse files
LPLafontaineBLarah Armstrong
and
Larah Armstrong
authored
fix: tutorial improvements (#840)
* fixing typos and broken links * clarifying the Testing Hello World section * Clarified the usage of OnNetworkSpawn in HelloWorldPlayer * clarified Adding Editor Modes section in GP1 * clarified use of command line helper * Adding high-level explanation of what NetworkManager and UTP are in HelloWorld tutorial, with links to detailed docs * restructured GP1 to have instructions to add script after description of why we add them * adding links to gp_intro Co-authored-by: Larah Armstrong <[email protected]>
1 parent 38604d8 commit 62e56a2

File tree

4 files changed

+81
-92
lines changed

4 files changed

+81
-92
lines changed

docs/tutorials/goldenpath_series/gp_intro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ Next steps will likely involve the existing Golden Path series to be completely
1919

2020
|<div class="buttons-pages">Hello World</div>| <div class="buttons-pages">Golden Path One</div>|
2121
| --- | --- |
22-
| Creating a new project<br/> Installing Netcode<br/> Creating and testing the basic networking building blocks<br/> | Adding scripts to objects<br/> Editor modes (Host Server and Client)<br/> Basic player movement <br/>Basic RPC and Network variable use |
22+
| [Creating a new project](../helloworld.md#create-a-new-project-in-unity)<br/> [Installing Netcode](../helloworld.md#install-netcode)<br/> [Creating and testing the basic networking building blocks](../helloworld.md#create-the-basic-components)<br/> | [Editor modes (Host Server and Client)](gp_module_one.md#adding-editor-modes-to-hello-world)<br/> [Basic player movement](gp_module_one.md#adding-basic-movement-to-the-player-object) <br/>[Basic RPC and Network variable use](gp_module_one.md#some-simple-rpc-use) |
2323
</div>
2424
<div class="table-columns-plain">
2525

2626
| <div class="buttons-pages">Golden Path Two</div>|
2727
| --- |
28-
| Network variables (server-controlled)<br/> Network transforms <br/> More on RPCs|
28+
| [Network variables (server-controlled)](gp_module_two.md#introducing-a-server-controlled-network-variable)<br/> [Network transforms](gp_module_two.md#introducing-network-transform) <br/> [More on RPCs](gp_module_two.md#introducing-rpcs)|
2929

3030

3131
</div>

docs/tutorials/goldenpath_series/gp_module_one.md

Lines changed: 69 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -27,79 +27,15 @@ You should have completed the [Hello World project](../helloworld.md) before sta
2727
1. Open Unity Hub.
2828
1. Select `Hello World` from the list of projects displayed.
2929

30-
## Adding Scripts to Hello World
31-
32-
This section adds some scripts to Hello World that contain the new features covered in the tutorial.
33-
1. Click the **Assets** folder.
34-
2. Open the **Scripts** folder.
35-
36-
### Adding the `HelloWorldPlayer.cs` script
37-
38-
1. Create a new script `HelloWorldPlayer`.
39-
1. Open the `HelloWorldPlayer.cs` script.
40-
1. Edit the `HelloWorldPlayer.cs` script to match the following.
41-
42-
<details open>
43-
<summary>Click to show/hide the Code.</summary>
44-
45-
```csharp
46-
using Unity.Netcode;
47-
using UnityEngine;
48-
49-
namespace HelloWorld
50-
{
51-
public class HelloWorldPlayer : NetworkBehaviour
52-
{
53-
public NetworkVariable<Vector3> Position = new NetworkVariable<Vector3>();
54-
55-
public override void OnNetworkSpawn()
56-
{
57-
if (IsOwner)
58-
{
59-
Move();
60-
}
61-
}
62-
63-
public void Move()
64-
{
65-
if (NetworkManager.Singleton.IsServer)
66-
{
67-
var randomPosition = GetRandomPositionOnPlane();
68-
transform.position = randomPosition;
69-
Position.Value = randomPosition;
70-
}
71-
else
72-
{
73-
SubmitPositionRequestServerRpc();
74-
}
75-
}
76-
77-
[ServerRpc]
78-
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
79-
{
80-
Position.Value = GetRandomPositionOnPlane();
81-
}
82-
83-
static Vector3 GetRandomPositionOnPlane()
84-
{
85-
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
86-
}
87-
88-
void Update()
89-
{
90-
transform.position = Position.Value;
91-
}
92-
}
93-
}
94-
```
95-
</details>
30+
## Adding Editor Modes to Hello World
9631

97-
### Adding the `HelloWorldManager.cs` script
32+
In the HelloWorld project, you created a **NetworkManager** by adding the pre-created **NetworkManager** component. In Play Mode, the NetworkManager shows Editor buttons labeled `Start Host`, `Start Client`, and `Start Server` in its inspector. These call the `StartHost`, `StartClient` and `StartServer` methods of the **NetworkManager** respectively, to initiate a networking session. Inside the `HelloWorldManager.cs` script, we define two methods which mimic this functionality via UI buttons and status labels.
9833

9934
1. Create an empty `GameObject` rename it **HelloWorldManager**.
100-
1. Create a script called `HelloWorldManager`.
101-
1. Open the `HelloWorldManager.cs` script.
102-
1. Edit the `HelloWorldManager.cs` script to match the following.
35+
2. Open the **Scripts** Folder.
36+
3. Create a script called `HelloWorldManager`.
37+
4. Open the `HelloWorldManager.cs` script.
38+
5. Edit the `HelloWorldManager.cs` script to match the following.
10339

10440
:::tip
10541
You can copy the script from here and paste it into your file.
@@ -180,10 +116,6 @@ namespace HelloWorld
180116

181117
1. Add the `HelloWorldManager` script component to the `HelloWorldManager` `GameObject`.
182118

183-
## Adding Editor Modes to Hello World
184-
185-
Inside the `HelloWorldManager.cs` script, we define two methods which mimic the editor buttons inside of **NetworkManager** during Play mode.
186-
187119
<details open>
188120
<summary>Click to show/hide the Code.
189121
</summary>
@@ -208,7 +140,7 @@ Inside the `HelloWorldManager.cs` script, we define two methods which mimic the
208140
```
209141
</details>
210142

211-
`NetworkManager` implements the singleton pattern as it declares its singleton named `Singleton`. This is defined when the `MonoBehaviour` is enabled. This component also contains very useful properties, such as `IsClient`, `IsServer`, and `IsLocalClient`. The first two dictate the connection state we have currently established that you will use shortly.
143+
You can statically access the `NetworkManager` instance from any other scripts via its singleton named `Singleton`. This is defined when the `MonoBehaviour` is enabled. This component also contains very useful properties, such as `IsClient`, `IsServer`, and `IsLocalClient`. The `IsClient` and `IsServer` properties dictate the connection state we have currently established that you will use shortly.
212144

213145
We call these methods inside of `OnGUI()`.
214146

@@ -244,7 +176,67 @@ You will notice the introduction of a new method, `SubmitNewPosition()`. This is
244176

245177
## Adding basic movement to the Player object
246178

247-
The `HelloWorldPlayer.cs` script adds some basic movement to the Hello World player.
179+
Here we will create a `HelloWorldPlayer.cs` script that adds some basic movement to the Hello World player.
180+
181+
1. Open the **Scripts** Folder.
182+
1. Create a new script called `HelloWorldPlayer`.
183+
1. Open the `HelloWorldPlayer.cs` script.
184+
1. Edit the `HelloWorldPlayer.cs` script to match the following.
185+
186+
<details open>
187+
<summary>Click to show/hide the Code.</summary>
188+
189+
```csharp
190+
using Unity.Netcode;
191+
using UnityEngine;
192+
193+
namespace HelloWorld
194+
{
195+
public class HelloWorldPlayer : NetworkBehaviour
196+
{
197+
public NetworkVariable<Vector3> Position = new NetworkVariable<Vector3>();
198+
199+
public override void OnNetworkSpawn()
200+
{
201+
if (IsOwner)
202+
{
203+
Move();
204+
}
205+
}
206+
207+
public void Move()
208+
{
209+
if (NetworkManager.Singleton.IsServer)
210+
{
211+
var randomPosition = GetRandomPositionOnPlane();
212+
transform.position = randomPosition;
213+
Position.Value = randomPosition;
214+
}
215+
else
216+
{
217+
SubmitPositionRequestServerRpc();
218+
}
219+
}
220+
221+
[ServerRpc]
222+
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
223+
{
224+
Position.Value = GetRandomPositionOnPlane();
225+
}
226+
227+
static Vector3 GetRandomPositionOnPlane()
228+
{
229+
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
230+
}
231+
232+
void Update()
233+
{
234+
transform.position = Position.Value;
235+
}
236+
}
237+
}
238+
```
239+
</details>
248240

249241

250242
1. Select the **Player** prefab.
@@ -306,13 +298,7 @@ https://github.com/Unity-Technologies/com.unity.multiplayer.samples.poc/tree/fea
306298
```
307299
</details>
308300

309-
Any `MonoBehaviour` implementing `NetworkBehaviour` can override the Netcode method `OnNetworkSpawn()`. This method is fired when the `NetworkObject` gets spawned. We override `OnNetworkSpawn` since a client and a server will run different logic here.
310-
311-
:::note
312-
This can be overriden on any `NetworkBehaviour`.
313-
:::
314-
315-
On both client and server instances of this player, we call the `Move()` method, which will simply do the following.
301+
In a networked game, the dedicated server (or host) might need to run different functions than the networked player (the client). For example, in a server-authoritative game, the client would handle the player inputs, but the server would handle the movement. All instances of this script in the game, whether running on a server/host or a client, call the `OnNetworkSpawn` method when the `NetworkObject` to which this script is attached has spawned, but only its owner will call the `Move` method. In the case of a [Player Object](../../basics/networkobject#player-objects), Netcode spawns an instance of the player object for every client and host (for which they are the owner). Each of these player objects contain this script. Calling the `Move` method in OnNetworkSpawn instead of, for example, in `Awake` ensures that the NetworkObject has finished spawning, so the check to see whether the player is a server/host or client is valid. The `Move` method can then implement different logic depending on the answer (so that servers do one thing, and clients do another).
316302

317303
<details open>
318304
<summary>Click to show/hide the Code.

docs/tutorials/goldenpath_series/gp_module_two.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Now we check that the Network Transform functions correctly.
149149

150150
1. Select **File** > **Build and Run**.
151151
1. Stop the player.
152-
1. Launch the client and server together in a terminal as shown in [Testing the command line helper](#testing-the-command-line-helper).
152+
1. Launch the client and server together in a terminal as shown in [Testing the command line helper](../helloworld.md#testing-the-command-line-helper).
153153
1. After a brief delay, the client and server will spawn.
154154
1. You should see the player capsule moving in a circle on both the client and the server.
155155

@@ -212,7 +212,7 @@ Now we will test that the client and server are both recieving the RPCs correctl
212212

213213
1. Select **File** > **Build and Run**.
214214
1. Stop the player.
215-
1. Launch the client and server together in a terminal as shown in [Testing the command line helper](#testing-the-command-line-helper).
215+
1. Launch the client and server together in a terminal as shown in [Testing the command line helper](../helloworld.md#testing-the-command-line-helper).
216216
1. After a brief delay, the client and server will spawn.
217217
1. In the console, you should expect to see the client and server sending RPC messages to each other.
218218
1. The client kicks off the exchange in its `Update` call the first time with a counter value of 0.

docs/tutorials/helloworld.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ In this section we will create the basic building blocks of a multiplayer game.
3030

3131
### Creating Network Manager and selecting the Transport
3232

33-
In this section we will add a Network Manager and add Unity Transport (UTP) to our project.
33+
In this section we add a Network Manager and add Unity Transport (UTP) to our project. The [NetworkManager](../components/networkmanager.md) is the component that contains all your project's netcode-related settings. UTP is the transport layer that Netcode uses for communication between the server and the clients. See [here](../advanced-topics/transports.md) for more.
3434

3535
1. Right-click in the **Hierarchy** tab of the main Unity Window.
3636
1. Select **Create Empty**.
@@ -89,7 +89,7 @@ When 'Enable Scene Management' is enabled for the NetworkManager (allowing the s
8989

9090
## Creating a command line helper
9191

92-
This command line helper launches our project outside Unity and can make testing builds easier.
92+
This command line helper will allow us to launch builds with a command line argument that will start a networking session, either as a server, host, or client. This can make testing builds easier.
9393

9494
1. Right-click the **Assets** folder and create a new folder by hovering over **Create** and selecting **Folder**. Name it **Scripts**.
9595
2. Create a script called `NetworkCommandLine` by right-clicking on your **Scripts** folder, hovering over **Create** and selecting **C# Script**.
@@ -290,16 +290,19 @@ import TabItem from '@theme/TabItem';
290290

291291
## Testing Hello World
292292

293-
Now we will test to see if everything works as expected.
293+
Now, to see if everything works as expected we can test starting a host in the editor. A host plays the role of a server and a client at the same time.
294294

295295
1. Click **Play**.
296-
1. Click **Start Host** under **NetworkManager**.
296+
1. Click the **Start Host** button in the **NetworkManager** GameObject's inspector.
297+
1. You should now see your scene with your Player Prefab spawned.
298+
299+
You can also use the command line helper to launch a server and one or more clients to connect to the server. You should see the plane on the server until the first client connects. Then, a new Player Prefab will be spawned by Netcode for each connected client.
297300

298301
## Next Steps
299302

300303
See the following content to continue your journey using Netcode:
301304

302-
* Build on the Hello World project to continue learning about different features of Netcode with the [Golden Path series](../tutorials/goldenpath_series/gp_intro.md).
305+
* Build on the Hello World project to continue learning about different features of Netcode with the [Golden Path series](goldenpath_series/gp_intro.md).
303306
* Check out the educational samples to further explore Netcode and its abilities:
304307
* [Boss Room](../learn/bossroom/getting-started-boss-room.md)
305308
* [2D Spaceshooter Bitesize Sample](../learn/bitesize/bitesize-spaceshooter.md)

0 commit comments

Comments
 (0)