Instantiating a Local Camera
Using a Camera Prefab to instantiate your local player's camera.
With this method of camera management we will instantiate a camera from a prefab we create and assign it to our local player object when it spawns in.
1
2
3
Writing a PlayerCamera Script
Let's now add the following script to the Player Prefab that we will use to instantiate our Camera Prefab once our player spawns in.
using FishNet.Object;
using UnityEngine;
// This script will be a NetworkBehaviour so that we can use the
// OnStartClient override.
public class PlayerCamera : NetworkBehaviour
{
[SerializeField] private Camera cameraPrefab;
[SerializeField] private Transform cameraHolder;
// This method will run on the client once this object is spawned.
public override void OnStartClient()
{
// Since this will run on all clients that this object spawns for
// we need to only instantiate the camera for the object we own.
if (IsOwner)
Instantiate(cameraPrefab, cameraHolder.position, cameraHolder.rotation, cameraHolder);
}
}
This script uses the OnStartClient override method from NetworkBehaviour to instantiate the camera prefab for our local player.
4
Last updated