Search Unity

Resolved [NGO] client movement

Discussion in 'Netcode for GameObjects' started by VLRSOFT, Oct 31, 2021.

  1. VLRSOFT

    VLRSOFT

    Joined:
    Feb 29, 2020
    Posts:
    12
    I'm starting as a host. The prefab with the player will spawn well, and his management works with a bang. But when I still connect as a client, it also works, but the movement of the client player seems to be blocked, although I output logs and they work, and the object itself remains in place. If you turn off Network Transform on the client, the management on the client starts working, but it does not synchronize accordingly.

    Scripts for player prefab:
    Code (CSharp):
    1. public class PlayerMovement : NetworkBehaviour
    2. {
    3.     CharacterController characterController;
    4.     public Transform cameraTransform;
    5.  
    6.     float pitch = 0f;
    7.     // Start is called before the first frame update
    8.     public NetworkVariable<Vector3> Position = new NetworkVariable<Vector3>();
    9.     public void Start()
    10.     {
    11.        
    12.         if (!IsLocalPlayer) {
    13.             cameraTransform.GetComponent<AudioListener>().enabled = false;
    14.             cameraTransform.GetComponent<Camera>().enabled = false;
    15.             transform.GetComponent<PlayerMovement>().enabled = false;
    16.          
    17.         }
    18.         else {
    19. characterController = GetComponent<CharacterController>();
    20. Cursor.lockState = CursorLockMode.Locked;
    21.         }
    22.        
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.      
    29.        if (IsLocalPlayer) {
    30. MovePlayer();
    31. Look();
    32.        }
    33.      
    34.     }
    35.     void MovePlayer() {
    36.        
    37. Debug.Log("Двигаем игрока! Горизонталь: " + Input.GetAxis("Horizontal") + "Вертикаль: " + Input.GetAxis("Vertical"));
    38. Vector3 move  = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical") );
    39. move = Vector3.ClampMagnitude(move, 1f);
    40. move = transform.TransformDirection(move);
    41. characterController.SimpleMove(move * 5f);
    42.     }
    43.  
    44.     void Look() {
    45.         float mouseX = Input.GetAxis("Mouse X") * 3f;
    46.         transform.Rotate(0,mouseX,0);
    47.         pitch -= Input.GetAxis("Mouse Y") * 3f;
    48.         pitch = Mathf.Clamp(pitch, -45f, 45f);
    49. cameraTransform.localRotation = Quaternion.Euler(pitch,0,0);
    50.  
    51.     }
     
    NCookie likes this.
  2. VLRSOFT

    VLRSOFT

    Joined:
    Feb 29, 2020
    Posts:
    12
    solved the problem. It turns out that the Client Network Transform script needs to be specified on the player's prefab, instead of Network Transform
     
  3. NCookie

    NCookie

    Joined:
    Sep 9, 2020
    Posts:
    1
    Could you explain more about this "Client Network Transform"? I have the same issue that host can move and is seen by client but client itself cannot move.
     
  4. VLRSOFT

    VLRSOFT

    Joined:
    Feb 29, 2020
    Posts:
    12
    where the "player" prefab in MLAPI was enough to add the "Network Transform" component. And the NGO (netcode) uses "Client Network Transform". As I understand it, this is done so that Network Transform is controlled only by the host, and not by the client
     
    NCookie likes this.
  5. Talkyn

    Talkyn

    Joined:
    Apr 13, 2019
    Posts:
    8
    The Client Network Transform lets you use a client authoritative approach, but this isn't suitable for all types of games and has some implications for physics related to object ownership.

    To make things work as expected using Network Transform, you need to use ServerRPCs from the client to update NetworkVariables that the Server can then operate on. Back on the client side, you use these NetVars instead of just local ones generated by your inputs so draw and so on.
     
    Last edited: Nov 13, 2021
    VLRSOFT likes this.