This document is about: FUSION 2
SWITCH TO

2 - Scene and Player

Overview

Part 2 explains how to set up a simple scene and how to create a player prefab. At the end of this section you will have a working networked scene that spawns a player object for each connected client.

Setting Up the Scene

Start from an empty Unity scene. Right-click on the Hierarchy tab in Unity and select Fusion > Scene > Setup Networking in Scene.

add networking

This adds a Prototype Runner and a Prototype Network Start to the scene.

add networking
  • Prototype Runner contains the Network Runner which is the core component running the Fusion simulation. The Network Events scrips allows you to quickly link up your own functions to a variety of network events such as players joining or leaving the session.
  • Prototype Network Start is a prototyping component that contains a bootstrap GUI for quickly joining into a Fusion room.

Next, add a floor to the scene, right-click on the Hierarchy tab in Unity and choose 3D Object > Plane. Rename the GameObject to Floor and reset its position to (0, 0, 0).

add networking

With that the scene setup is complete.

Creating the Player Prefab

Next create the player object. Right-click on the Hierarchy and select 3d Object > Capsule. Rename the GameObject to PlayerCharacter.

Add a NetworkObject component to it. This component gives the object a network identity so that all peers can reference it.

Add a CharacterController component. This will later be used by movement code to move the player character.

Finally, add a NetworkTransform component to it. NetworkTransform automatically synchronizes the position of the object to other clients.

Drag the PlayerCharacter into the Project window to create a prefab and delete it from the scene. With that the player character is ready to be spawned.

Player Spawning

Unlike in single player games it is not sufficient to just have a single player object in the scene. For multiplayer it is needed to spawn a player object for each player that joins the session. For that a custom script is needed. Create a PlayerSpawner script and open it. Add the following code to it:

C#

using Fusion;
using UnityEngine;

public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
{
    public GameObject PlayerPrefab;

    public void PlayerJoined(PlayerRef player)
    {
        if (player == Runner.LocalPlayer)
        {
            Runner.Spawn(PlayerPrefab, new Vector3(0, 1, 0), Quaternion.identity); 
        }
    }
}

The spawn code is very simple. The IPlayerJoined interface has a PlayerJoined function which gets called whenever a player joins the session if the behaviour is on the same GameObject as the runner. This happens for our own player but also for any other player joining from a different device. It is only needed to spawn a player object for our own player. When an object gets spawned with Runner.Spawn it gets automatically replicated to all other clients.

SimulationBehaviour is used to get access to the NetworkRunner which contains all information about the current session including the player ID of the local player.

Add the PlayerSpawner component to the Prototype Runner GameObject and assign the PlayerCharacter prefab to it.

With this, player spawning is fully setup. Enter play mode and press Start Shared Client to start Fusion in Shared mode.

the start menu

A player capsule will appear in the scene. The player is not able to move or interact with anything yet. In the next chapter we will bring the player to life by implementing player movement.

player capsule in the scene

Next Shared Mode Basics 3 - Movement and Camera

Back to top