Home > Blockchain >  Client moves only when im the host
Client moves only when im the host

Time:04-17

I have a Player (the client) with a Network Identity, Network Transform and Network Rigidbody 2D all checked with client authority.

I want to apply force to the rigidbody in the server, but the command (CmdAddForce) only works when im the host, when im the client the command dont execute and i cant move.

This is the code:

using UnityEngine;
using Mirror;

public class Player : NetworkBehaviour
{
    private Rigidbody2D rb;
    private float force = 12;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        // Solo aplicar el codigo localmente
        if (!isLocalPlayer)
            return;

        CmdAddForce(new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical") * force));
    }

    [Command]
    void CmdAddForce(Vector2 force)
    {
        rb.AddForce(force);
    }
}

CodePudding user response:

You are applying the force on the server. But you checked the client authority on Network Rigidbody component; try to disable it, so it will be synced from server to clients.

  • Related