Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice

FreeLook Camera not working with multiplayer

Discussion in 'Cinemachine' started by Asasapatata, Jan 4, 2022.

  1. Asasapatata


    Dec 8, 2021
    I am working on a multiplayer game with Photon. It is working good but I am not satisfied by the camera. Now I am using a simple way (using photonView) to detect if I am the player and, in this case, move the camera to an abstract point behind the character (it is like a 3d person camera).
    Now, I want to switch to the Cinemachine using the FreeLook Camera but the results are bad.

    I have all the player prefabs in my resource folder. When the game start, a casual avatar is selected and a new player is attached to the scene.
    The player is created in the PlayerSpawner script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    6. public class PlayerSpawner : MonoBehaviour
    7. {
    9.   public static PlayerSpawner instance;
    11.   private void Awake() {
    12.     instance = this;
    13.   }
    15.   public GameObject playerPrefab;
    16.   public GameObject player;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         if(PhotonNetwork.IsConnected) {
    22.           SpawnPlayer();
    23.         }
    24.     }
    26.     public void SpawnPlayer() {
    27.       Transform spawnPoint = SpawnManager.instance.GetSpawnPoint();
    29.       player = PhotonNetwork.Instantiate(, spawnPoint.position, spawnPoint.rotation);
    30.     }
    31. }
    Then I control the character in the PlayerController script. In this script I originally implemented the camera transformation (not the Cinemachine) and now I am trying to manage also the FreeLook camera to attach it to the player.
    Basically, this is the process I make.
    Code (CSharp):
    2. public GameObject playerModel;
    3. private CinemachineFreeLook freeLookCam;
    4. void Start()
    5.     {
    6.       // Cursor.lockState = CursorLockMode.Locked;
    7.       var freeLookCam = GetComponent<CinemachineFreeLook>();
    9.       playerModel.GetComponent<Renderer>().material = allSkins[photonView.Owner.ActorNumber % allSkins.Length];
    10.       setupCamera();
    11.     }
    13.     void setupCamera() {
    14.       freeLookCam.Follow = playerModel.transform;
    15.       freeLookCam.LookAt = playerModel.transform;
    16.     }
    When it runs, I get the following error
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.setupCamera () (at Assets/Scripts/PlayerController.cs:65)
    PlayerController.Start () (at Assets/Scripts/PlayerController.cs:59)

    which is related to this line freeLookCam.Follow = playerModel.transform;

    The FreeLook camera is inside the main Scene. I cannot directly attach the player to the follow and lookat because the player is created by the PlayerSpawner. If I run the game and then attach manually the created player to follow and lookat, it seems to work (even if I do not know what happens when a new player joins the game)

    Thank you for your help

    Attached Files:

  2. gaborkb


    Unity Technologies

    Nov 7, 2019
    It seems like either freeLookCam or playerModel is null.

    Maybe your script is not attached to a freelook vcam:
    Code (CSharp):
    1. var freeLookCam = GetComponent<CinemachineFreeLook>();
    Adding Follow and LookAt at run time from a script should not be a problem.