Basic Setup with Cinemachine

Managing a Cinemachine Camera in multiplayer.

This guide will show you one way you can set-up your player camera for multiplayer when you are using the Cinemachine package.

1

Installing Cinemachine

The first step — if you haven't done so already — is to install Cinemachine through the Unity Package Manager.

Cinemachine Installing
2

Adding the Cinemachine Brain

Now that we have Cinemachine installed we can set it up. Add the Cinemachine Brain component to the Main Camera game object in the scene.

Cinemachine Brain added to Main Camera
3

Giving the Player a Camera Holder

Now create an empty Game Object on your Player Prefab and position it where you'd like. This will be our container for the Cinemachine Camera.

Camera Holder created on the Player Prefab
4

Adding the Cinemachine Camera

Now simply add the Cinemachine Camera component to the newly created CameraHolder game object.

The Cinemachine Camera added to the Camera Holder
5

Writing a PlayerCamera Script

Let's now add the following script to the Player Prefab that we will use to take control of our Camera once our player spawns in.

PlayerCamera.cs
using FishNet.Object;
using Unity.Cinemachine;
using UnityEngine;

// This script will be a NetworkBehaviour so that we can use the OnStartClient override.
public class PlayerCamera : NetworkBehaviour
{
    [SerializeField] private CinemachineCamera cinemachineCamera;

    // This method is called on the client after the object is spawned in.
    public override void OnStartClient()
    {
        // Simply enable our local cinemachine camera on the object if we are the owner.
        cinemachineCamera.enabled = IsOwner;
    }
}

This script uses the OnStartClient override method from NetworkBehaviour to enable or disable the Cinemachine Camera component on the player objects depending on if they are our local player or not.

6

Assign your References to the Script

Now select the Player Camera component in your Player Prefab and add drag the Camera Holder game object into the Cinemachine Camera field to assign it the component.

The Player prefab with the Cinemachine Camera field filled in
7

Test the Camera In-Game

With all that set you should be able to run the game and see how the camera from the scene is controlled by only your local player.

Demonstration of the local camera

Download the project files with these completed steps here, or explore the repository:

Source Files Repository

Last updated