Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't move client player

Discussion in 'Netcode for GameObjects' started by XPZz, Feb 9, 2023.

  1. XPZz

    XPZz

    Joined:
    Dec 30, 2022
    Posts:
    2
    Hello,

    I have 3 scripts, CameraController, MoveCamera, and PlayerMovement. I am able to start the server on one process and on another I can join with a 2nd client. The host is able to move around, but the client is unable to move. Now I think the issue is in PlayerMovement, but I'm not sure what is causing this? I've been scratching my head for a while trying to fix it.

    PlayerMovement.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : NetworkBehaviour
    7. {
    8.     [Header("Movement")]
    9.     public float moveSpeed;
    10.     public GameObject CameraHolder;
    11.     [SerializeField] private float maxSpeed = 2f;
    12.     [SerializeField] private float jumpForce = 10f;
    13.     public Camera cam; // Drag camera into here
    14.     private NetworkVariable<int> randomNumber = new NetworkVariable<int>(1);
    15.  
    16.  
    17.     public Transform orientation;
    18.  
    19.     float horizontalInput;
    20.     float verticalInput;
    21.  
    22.     Vector3 moveDirection;
    23.  
    24.     Rigidbody rb;
    25.  
    26.     private void Start()
    27.     {
    28.         if(!IsOwner)
    29.         {
    30.             cam.enabled = false;
    31.         }
    32.  
    33.        
    34.         rb = GetComponent<Rigidbody>();
    35.         rb.freezeRotation = true;
    36.  
    37.       //  GameObject mCamera = Instantiate(CameraHolder);
    38.     }
    39.  
    40.     private void FixedUpdate()
    41.     {
    42.  
    43.  
    44.  
    45.  
    46.     }
    47.  
    48.     private void MyInput()
    49.     {
    50.         horizontalInput = Input.GetAxisRaw("Horizontal");
    51.         verticalInput = Input.GetAxisRaw("Vertical");
    52.  
    53.         Debug.Log(OwnerClientId + "; " + randomNumber.Value);
    54.  
    55.         if (Input.GetKeyDown(KeyCode.T))
    56.         {
    57.             randomNumber.Value = Random.Range(0, 100);
    58.         }
    59.        
    60.  
    61.     }
    62.  
    63.     private void MovePlayer()
    64.     {
    65.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    66.  
    67.        
    68.  
    69.         if (rb.velocity.magnitude > maxSpeed)
    70.         {
    71.             rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
    72.         }
    73.  
    74.         if (Input.GetKeyDown("space"))
    75.         {
    76.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); // Add an upward force to the Rigidbody
    77.         }
    78.     }
    79.  
    80.     void Update()
    81.     {
    82.      
    83.         if (!IsOwner) return;
    84.        
    85.         MyInput();
    86.         MovePlayer();
    87.     }
    88.  
    89. }
    Here is my Inspector for Player Prefab

    upload_2023-2-9_8-5-19.png
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    If I am not wrong, you can't just add velocity to the rigidBody as a client, you need to call a ServerRPC function to move it on the server.
     
  3. XPZz

    XPZz

    Joined:
    Dec 30, 2022
    Posts:
    2
    I tried to implement ClientRPC and ServerRPC and I still don't seem to have it working.



    Code (CSharp):
    1.  
    2.     private void FixedUpdate()
    3.     {
    4.         if (!IsOwner) return;
    5.         //Debug.Log(IsOwner);
    6.         MovePlayerClientRpc();
    7.     }
    8.  
    9.     private void MyInput()
    10.     {
    11.         horizontalInput = Input.GetAxisRaw("Horizontal");
    12.         verticalInput = Input.GetAxisRaw("Vertical");
    13.  
    14.         Debug.Log(OwnerClientId + "; " + randomNumber.Value);
    15.  
    16.         if (Input.GetKeyDown(KeyCode.T))
    17.         {
    18.             randomNumber.Value = Random.Range(0, 100);
    19.         }
    20.        
    21.  
    22.     }
    23.  
    24.     [ClientRpc]
    25.     private void MovePlayerClientRpc()
    26.     {
    27.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    28.  
    29.         rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    30.         MovePlayerServerRpc();
    31.  
    32.         if (rb.velocity.magnitude > maxSpeed)
    33.         {
    34.             rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
    35.         }
    36.  
    37.         if (Input.GetKeyDown("space"))
    38.         {
    39.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); // Add an upward force to the Rigidbody
    40.         }
    41.     }
    42.  
    43.     [ServerRpc]
    44.     private void MovePlayerServerRpc()
    45.     {
    46.         rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    47.     }
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    clientRPC executes the code in all clients. You don't need to use it because the code needs to be executed only in the current client. Try doing:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5. public class PlayerMovement : NetworkBehaviour
    6. {
    7.     [Header("Movement")]
    8.     public float moveSpeed;
    9.     public GameObject CameraHolder;
    10.     [SerializeField] private float maxSpeed = 2f;
    11.     [SerializeField] private float jumpForce = 10f;
    12.     public Camera cam; // Drag camera into here
    13.     private NetworkVariable<int> randomNumber = new NetworkVariable<int>(1);
    14.     public Transform orientation;
    15.     float horizontalInput;
    16.     float verticalInput;
    17.     Vector3 moveDirection;
    18.     Rigidbody rb;
    19.     private void Start()
    20.     {
    21.         if(!IsOwner)
    22.         {
    23.             cam.enabled = false;
    24.         }
    25.    
    26.         rb = GetComponent<Rigidbody>();
    27.         rb.freezeRotation = true;
    28.       //  GameObject mCamera = Instantiate(CameraHolder);
    29.     }
    30.     private void FixedUpdate()
    31.     {
    32.     }
    33.     private void MyInput()
    34.     {
    35.         horizontalInput = Input.GetAxisRaw("Horizontal");
    36.         verticalInput = Input.GetAxisRaw("Vertical");
    37.         Debug.Log(OwnerClientId + "; " + randomNumber.Value);
    38.         if (Input.GetKeyDown(KeyCode.T))
    39.         {
    40.             randomNumber.Value = Random.Range(0, 100);
    41.         }
    42.    
    43.     }
    44.     private void MovePlayer()
    45.     {
    46.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    47.    
    48.         if (rb.velocity.magnitude > maxSpeed)
    49.         {
    50.             MovePlayerServerRpc();
    51.         }
    52.         if (Input.GetKeyDown("space"))
    53.         {
    54.             MovePlayerServerRpc(); // another function
    55.         }
    56.     }
    57.     void Update()
    58.     {
    59.  
    60.         if (!IsOwner) return;
    61.    
    62.         MyInput();
    63.         MovePlayer();
    64.     }
    65.  
    66.  
    67.     [ServerRpc]
    68.     private void MovePlayerServerRpc()
    69.     {
    70.         rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    71.     }
    72. }
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    426
    Hi @XPZz , have you already had a look at the examples in the official ClientDriven sample?I

    f not, you can download it here.

    Its examples show how to use the third person controller asset in a multiplayer environment for movement and picking objects up