This document is about: FUSION 1
SWITCH TO

This page is a work in progress and could be pending updates.

2 - Scene and Player

Overview

Fusion 102 Shared 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 > Setup > Networking.

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

Check Destroy When State Authority Leaves on the component. This setting causes Fusion to despawn the object when the client who controls it (the StateAuthority) leaves. For many games this is not intended behavior since the player object can be kept around and re-assigned to a player once they reconnect, but it will be used in this sample.

Uncheck Allow State Authority Override. This setting allows other clients to take control over a NetworkObject. Since this is the player object we always want the same client to stay in control of it. Keep all other settings as is.

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

Next add a NetworkTransform component to it.

Currently, the player object does not contain any visuals yet. In Fusion everything related to visuals is put on a separate GameObject that is a child of the NetworkObject. This GameObject is called the Interpolation Target. This ensures that moving the object looks smooth.

The Interpolation Target object should contain anything related to displaying the player object. So components like MeshRenderers, Animators, Audiosources and Particle Systems.

Right-click on the Hierarchy and select 3d Object > Capsule. Rename the GameObject to Interpolation Target and remove the Capsule Collider from it.

Add Networking

On the NetworkTransform of the PlayerCharacter object set the InterpolationTarget field to the Interpolation Target GameObject.

Add Networking

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, player); 
        }
    }
}

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 player prefab to it.

With this, player spawning is fully setup. Enter play mode and press Start Shared Client to start Fusion in Shared mode. 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.

Next Shared Mode Basics 3 - Movement and Camera

Back to top