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.
public class PlayerController : NetworkBehaviour
{
private Rigidbody2D _rigidbody;
private float _vertical;
private float _speed = 2;
public NetworkVariableVector2 Position = new NetworkVariableVector2(new NetworkVariableSettings
{
WritePermission = NetworkVariablePermission.ServerOnly,
ReadPermission = NetworkVariablePermission.Everyone
});
public override void NetworkStart()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
/*public void Move()
{
if (NetworkManager.Singleton.IsServer)
{
Vector2 position = transform.position;
position.y += Time.deltaTime * vertical;
_r.MovePosition(position);
}
else
{
ChangePositionRequestServerRpc();
}
}*/
[ServerRpc]
void ChangePositionRequestServerRpc(Vector2 position, ServerRpcParams rpcParams = default)
{
Position.Value = position;
}
void Update()
{
//get input of local player
if (IsOwner)
{
_vertical = Input.GetAxis("Vertical");
}
//sync clients with server data
//resetting values ??
transform.position = Position.Value;
}
private void FixedUpdate()
{
Vector2 position = _rigidbody.position;
position.y += _vertical * _speed;
if (NetworkManager.Singleton.IsServer)
{
_rigidbody.MovePosition(position);
}
else
{
ChangePositionRequestServerRpc(position);
}
}
}
how do i solve this?