Search Unity

FreeLook Camera not working with multiplayer

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

  1. Asasapatata

    Asasapatata

    Joined:
    Dec 8, 2021
    Posts:
    1
    Hi,
    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;
    5.  
    6. public class PlayerSpawner : MonoBehaviour
    7. {
    8.  
    9.   public static PlayerSpawner instance;
    10.  
    11.   private void Awake() {
    12.     instance = this;
    13.   }
    14.  
    15.   public GameObject playerPrefab;
    16.   public GameObject player;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         if(PhotonNetwork.IsConnected) {
    22.           SpawnPlayer();
    23.         }
    24.     }
    25.  
    26.     public void SpawnPlayer() {
    27.       Transform spawnPoint = SpawnManager.instance.GetSpawnPoint();
    28.  
    29.       player = PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, spawnPoint.rotation);
    30.     }
    31. }
    32.  
    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):
    1.  
    2. public GameObject playerModel;
    3. private CinemachineFreeLook freeLookCam;
    4. void Start()
    5.     {
    6.       // Cursor.lockState = CursorLockMode.Locked;
    7.       var freeLookCam = GetComponent<CinemachineFreeLook>();
    8.      
    9.       playerModel.GetComponent<Renderer>().material = allSkins[photonView.Owner.ActorNumber % allSkins.Length];
    10.       setupCamera();
    11.     }
    12.  
    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

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    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.