Search Unity

player prefab with camera follow spawning multiple players

Discussion in 'Netcode for GameObjects' started by langqing429, Oct 4, 2022.

  1. langqing429

    langqing429

    Joined:
    Aug 6, 2022
    Posts:
    4
    I'm new to multiplayer thing, I am struggling with player prefab. I have player prefab in my assets and I am trying to spawn player like three or four. The thing is I use cinemachine camera config and I also want to make camera follow along the player. I tried to parent camera to the player in prefab. But when I spawn two player prefabs, the error occurs that mouse positioning is controlling both players. And i Spawn with netcode.
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    One way to do this is to have a check on the script of each player object that checks for
    IsLocalPlayer
    in
    OnNetworkSpawn
    . If it's not the local player object, then disable the camera and do not accept input from the player. That way, each player only sees the camera on their local player object and is only able to control that player.
     
    RikuTheFuffs and langqing429 like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    I split up my player prefab into a local and remote part. Depending on whether IsLocalPlayer I disable one of these two trees, thus the remote player will not have a camera or respond to input, while the local player isn't seeing his floating-above-head name tag when looking up.
    upload_2022-10-4_12-11-21.png
     
    codeBatt likes this.
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    It's not best practice to make the vcam a child of the player. Instead, if you want to include a vcam in the prefab, make it and the player both children of a common parent object.

    You can have the vcam inactive by default, an activate it only if you are spawning the local player.
     
    codeBatt and CodeSmile like this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Was wondering about that actually because most examples had it outside of the character's hierarchy. I just moved it in there to see if it would work, and it did. What can be some issues of having the vcam as a child?
     
    codeBatt likes this.
  6. codeBatt

    codeBatt

    Joined:
    Feb 12, 2020
    Posts:
    40
    I am now making this in that way and try to disable the objects for non-local players. Do you have any advices or advancements through the process may I ask?
    upload_2023-3-26_23-43-59.png