Search Unity

Resolved Camera client override camera in host - NetCode

Discussion in 'Netcode for GameObjects' started by rogelioae, Apr 26, 2023.

  1. rogelioae

    rogelioae

    Joined:
    May 20, 2022
    Posts:
    19
    I have a camera in my player prefab,
    -Playerprefab
    --CameraHolder
    ----Camera

    When I run the host the camera work fine, but when I run the client the camera client takes over(appropiates) the other camera host.

    Why is this happening?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    You need to disable all cameras except the one that is for the local player. Otherwise you‘ll have n player instances in the game, each with its own camera. Which camera is active in that instance is at best a matter of spawn sequence, but it could also be more or less random, and worst case all four of them draw to the screen (overdrawing each other‘s content).
     
    TitoYayo600 likes this.
  3. edin97

    edin97

    Joined:
    Aug 9, 2021
    Posts:
    58
    When you start the server, you only have YOUR player spawned. so 1 player and 1 camera.

    When your client joins, you have TWO players so TWO cameras, on YOUR side. You client also has TWO players on HIS side ; your player and HIS player, again this means he has TWO cameras too.

    The last camera to spawn always overwrite the others, which is why you see the client's POV. (your player spawns -> you see camera 1 , client's player spawns -> you see camera 2 because it overrides your player's camera.)

    The easy fix is as suggested by the other comment, :
    1) open your playerPrefab
    2) disable the camera, or the GameObject holding the Camera,
    3) Add this script to your playerPrefab (OR add the OnNetworkSpawn() method in one of your scripts.) :
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Netcode;
    3.  
    4. public class OwnerComponentManager : NetworkBehaviour
    5. {
    6.     [SerializeField] private Camera _camera; // This is your camera, assign it in the prefab
    7.  
    8.     public override void OnNetworkSpawn()
    9.     {
    10.         base.OnNetworkSpawn();
    11.         if (!IsOwner) { return; } // ALL players will read this method, only player owner will execute past this line
    12.         _camera.enabled = true; // only enable YOUR PLAYER'S camera, all others will stay disabled
    13.     }
    14. }
    15.  
    16.  
    since you have 2 players on YOUR side (server), this will be run 2 times, BUT only the owner will execute "_camera.enabled = true;" . Same for the client, he has 2 players, but only HIS player will enable the camera on HIS side.

    Now you have only ONE camera ENABLED, which is YOUR player's camera. The other cameras will still be there, but they are DISABLED.

    Also when you spawn your players, you have to spawn them with ownership, if you are not doing something like : NetworkObject player_NO = Instantiate(PlayerPrefab, ..., ...).GetComponent<NetworkObject>(); player_NO.SpawnWithOwnership(clientId); somewhere in your code, the above solution will not work.
    (or maybe you use SpawnAsPlayerObject(clientId); instead of SpawnWithOwnership, for playerPrefab it's the same thing)
     
  4. TitoYayo600

    TitoYayo600

    Joined:
    Aug 21, 2023
    Posts:
    1
    Oh sh*t you have saved me!