Search Unity

Question I honestly can't understand why it doesn't work ( IsLocalPlayer )

Discussion in 'Netcode for GameObjects' started by Qyuto, Mar 5, 2022.

  1. Qyuto

    Qyuto

    Joined:
    Oct 4, 2021
    Posts:
    2
    Hi all.
    This may seem like a dumb question to most people, but I can't figure out what my mistake is.
    (This is probably the first time in my life that I have decided to ask for help).

    What the problem is:
    I recently started using "Netcode for GameObjects".
    And for the sake of testing, I decided to do a simple player move.
    As I understand it, to check if a player is local or not, there is a variable: "IsLocalPlayer"
    And it works but the player who is connected can't move although islocalplayer == true


    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2.     {
    3.         [SerializeField] private TextMeshProUGUI inputFText;
    4.         [SerializeField] private TextMeshProUGUI inputRText;
    5.         [SerializeField] private TextMeshProUGUI islocalText;
    6.  
    7.  
    8.        
    9.         private Rigidbody _rigidbody;
    10.         private float speed  =5f;
    11.         private void Awake()
    12.         {
    13.             _rigidbody = GetComponent<Rigidbody>();
    14.            
    15.         }
    16.  
    17.  
    18.         private void Update()
    19.         {
    20.             islocalText.text = IsLocalPlayer.ToString();
    21.  
    22.             if (!IsLocalPlayer)
    23.                 return;
    24.            
    25.             float moveF = Input.GetAxisRaw("Vertical");
    26.             float moveR = Input.GetAxisRaw("Horizontal");
    27.                
    28.             inputFText.text = $"InputF: {moveF}";
    29.             inputRText.text = $"InputR: {moveR}";
    30.                
    31.             Vector3 direction = transform.forward * moveF + transform.right * moveR;
    32.             _rigidbody.velocity = direction * speed;  
    33.         }
    34.     }

    I displayed the data of the two variables on the game screen.
    And if you believe them, the player connected when you press the button responsible for: Input.GetAxisRaw("Vertical"); ( moveF )
    The variable moveF = 1 || moveF = -1 but the player does not move
    https://imgur.com/a/g3PBxe3
     
  2. Tauntastic

    Tauntastic

    Joined:
    Sep 24, 2020
    Posts:
    17
    You have no NetworkVariables<T> that sync with the Server/Host. Everything you are doing is not syncing anywhere. I recommend you go watch some tutorials.
     
    Qyuto likes this.
  3. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    If you want the client movement to be synched and want the client to have authority over movement (as your code implies), note that that means the client needs a ClientNetworkTransform component. If you have a NetworkTransform instead, only the server can change the transform, which would explain the player not moving.
     
    Ikshura and Qyuto like this.
  4. Ikshura

    Ikshura

    Joined:
    Jan 4, 2022
    Posts:
    2
    Thanks man, using ClientNetworkTransform saved me
     
    CosmoM likes this.