Search Unity

Question can't see players control by network

Discussion in 'Netcode for GameObjects' started by Jicefrost, Apr 18, 2022.

  1. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Hi all.
    Using MLAPI first time as many of us here :)
    2d game, I got player prefab with network object + rigidbody2d + PlayerController.cs
    Player did not get its own RigidBody2d component
    so If im using
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Netcode;
    3.  
    4. public class PlayerController : MonoBehaviour  // (!!!!!!!!)
    5. {
    6. [SerializeField] private Rigidbody2D _rigidbody2D;
    7. void Start()
    8.     {
    9.         if(IsLocalPlayer)
    10.         {
    11.    
    12.             _rigidbody2D = GetComponent<Rigidbody2D>();
    13.         }
    14.  
    15. .
    16. .
    17. so on
    18. }
    Connected players have on Rigidbody2D _rigidbody2D component.
    and If using PlayerController : NetworkBehaviour


    only first host receive rigidbody2D component and no clients at all. how so? So players spawned and they are not "localPlayers"?
    I really stuck with it.
    How May I set rigidbodies for players ?
     
    Last edited: Apr 18, 2022
  2. AggroBadger

    AggroBadger

    Joined:
    Feb 19, 2022
    Posts:
    16
    If the Unity editor window you're showing there is the host, then "IsLocalPlayer" will be True for the first player spawned (as the player hosting the game is a local player in the Unity editor ) but will be False for all other players spawned (as they are not local players to your host player's game).

    This means that for the host:
    Player 1 (Host) - IsLocalPlayer = True = Spawn rigidbody
    Player 2 (Player connecting to the host) - IsLocalPlayer = False = don't spawn rigidbody for host

    For player 2, it's the other way around:
    Player 2 - IsLocalPlayer = True = Spawn rigidbody
    Player 1 - IsLocalPlayer = False = don't spawn rigidbody
     
  3. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Yes thx for help. Just brought to the screen IsLocal variable, and it same as you say

    but still there is no RigidBody2d finds :(

     
    Last edited: Apr 18, 2022
  4. AggroBadger

    AggroBadger

    Joined:
    Feb 19, 2022
    Posts:
    16
    Try gameObject.GetComponent<Rigidbody2D>() - I'm not familiar with using just "GetComponent" from within a MonoBehaviour but it's the gameObject that has a Rigidbody2D attached, not the MonoBehaviour.
     
  5. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Found trouble. Need to do that on Start() now on Awake() thx for help