Search Unity

unable to move player due to transform.position resetting in Update()

Discussion in 'Multiplayer' started by IceDune, Apr 19, 2021.

  1. IceDune

    IceDune

    Joined:
    Aug 10, 2019
    Posts:
    1
    I am trying out MLAPI in a simple 2D game (just testing the player movement rn). but i'm unable to move the object as transform.position in Update(), which is used for updating data in all the clients, is resetting the position. if i remove it, the the player in host moves but all other client players cannot.

    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2.     {
    3.  
    4.         private Rigidbody2D _rigidbody;
    5.         private float _vertical;
    6.         private float _speed = 2;
    7.  
    8.         public NetworkVariableVector2 Position = new NetworkVariableVector2(new NetworkVariableSettings
    9.         {
    10.             WritePermission = NetworkVariablePermission.ServerOnly,
    11.             ReadPermission = NetworkVariablePermission.Everyone
    12.         });
    13.  
    14.         public override void NetworkStart()
    15.         {
    16.             _rigidbody = GetComponent<Rigidbody2D>();
    17.         }
    18.  
    19.         /*public void Move()
    20.         {
    21.             if (NetworkManager.Singleton.IsServer)
    22.             {
    23.                 Vector2 position = transform.position;
    24.                 position.y += Time.deltaTime * vertical;
    25.                 _r.MovePosition(position);
    26.             }
    27.             else
    28.             {
    29.                 ChangePositionRequestServerRpc();
    30.             }
    31.         }*/
    32.  
    33.         [ServerRpc]
    34.         void ChangePositionRequestServerRpc(Vector2 position, ServerRpcParams rpcParams = default)
    35.         {
    36.             Position.Value = position;
    37.         }
    38.  
    39.         void Update()
    40.         {
    41.             //get input of local player
    42.             if (IsOwner)
    43.             {
    44.                 _vertical = Input.GetAxis("Vertical");
    45.             }
    46.  
    47.             //sync clients with server data
    48.             //resetting values ??
    49.             transform.position = Position.Value;
    50.         }
    51.  
    52.         private void FixedUpdate()
    53.         {
    54.             Vector2 position = _rigidbody.position;
    55.             position.y += _vertical * _speed;
    56.  
    57.             if (NetworkManager.Singleton.IsServer)
    58.             {
    59.                 _rigidbody.MovePosition(position);
    60.             }
    61.             else
    62.             {
    63.                 ChangePositionRequestServerRpc(position);
    64.             }
    65.         }
    66.     }
    how do i solve this?
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    When you move the object on the server you should still manually set the value of the NetworkVariable like this:

    Code (CSharp):
    1.             if (NetworkManager.Singleton.IsServer)
    2.             {
    3.                 _rigidbody.MovePosition(position);
    4.                 Position.value = _rigidbody.Position;
    5.             }
    Also usually when you use rigidbodies you don't want to work with transform.position but assign the position to the rigidbody directly and do so in FixedUpdate.