简体   繁体   中英

Client moves only when im the host

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);
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM