Search Unity

Question MLAPI First time using can't see other players moving around

Discussion in 'Netcode for GameObjects' started by unkopath, May 15, 2021.

  1. unkopath

    unkopath

    Joined:
    Jun 23, 2018
    Posts:
    19
    Hi, I just saw a tutorial and trying to do my own script moving the player but ain't working, can someone tellme what i'm doing wrong? here is my script:


    Code (CSharp):
    1. using MLAPI;
    2. using MLAPI.Messaging;
    3. using MLAPI.NetworkVariable;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : NetworkBehaviour
    7. {
    8.     private Rigidbody rigidBody;
    9.     public float speed = 6f;
    10.  
    11.     private void Awake()
    12.     {
    13.         rigidBody = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         if (IsOwner)
    19.         {
    20.             MovePlayerServerRpc();
    21.         }    
    22.     }
    23.  
    24.     private void MovePlayer()
    25.     {
    26.         Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    27.         rigidBody.MovePosition(transform.position + m_Input * Time.deltaTime * speed);
    28.     }
    29.  
    30.     [ServerRpc]
    31.     private void MovePlayerServerRpc()
    32.     {
    33.         MovePlayerClientRpc();
    34.     }
    35.  
    36.     [ClientRpc]
    37.     private void MovePlayerClientRpc()
    38.     {
    39.         MovePlayer();
    40.     }
    41. }
    42.  
    43.  
     
  2. mitio31

    mitio31

    Joined:
    Apr 17, 2020
    Posts:
    3
    Hello there!
    First of all, using RPCs for player movement is not a good idea. I recommend you using Network Transform component on the player prefab. Network transform replicates network object's transform through the whole network. I suggest you reading the MLAPI documentation about the network components.
    I hope this will help you.
    If you have any further problems, please ask :)
     
  3. cossacksman

    cossacksman

    Joined:
    Dec 2, 2017
    Posts:
    10
    Hello @mitio31, I realise this isn't exactly a recent post but I'd be interested to know how the server remains authoritative on player position if you're using a NetworkTransform and move calculations aren't put through a server RPC? Is there any risk of a client manipulating their transform on their client and it's replicated server-wide? Speed/fly hacks for example.