CharacterMovement::CmdFight() called on NetworkCharacterContainer without authority error

134 Views Asked by At

I have been creating a multiplayer game using Mirror. I have coded an entire Fight function, and I turned it to a command function since i want it to be run on a server (because I think it's easier to calculate who you shot on server then on a client), but I get an error:

Command Function System.Void CharacterMovement::CmdFight() called on NetworkCharacterContainer(Clone) without authority

(The error on the title appears on host and I don't know why).

Here is the code:

public void HandleFighting()
{
    if(Input.GetMouseButtonDown(0))
    {
        Debug.Log(this);
        CmdFight(); //! We want to fight!
    }
}

[Command]
private void CmdFight()
{
    if (!isLocalPlayer)
    {
        Debug.LogWarning("Command Function CmdFight() called on CharacterMovement without authority.");
        return;
    }

    //! If we clicked on left mouse button
    if (WeaponData != null)
    {
        RaycastHit raycastHit;

        if (Physics.Raycast(CharacterPosition, Input.mousePosition, out raycastHit, WeaponData.Range))
        {
            //!We hit a player
            GameObject ho = raycastHit.collider.gameObject;
            Health health;

            if (ho.TryGetComponent<Health>(out health))
            {
                //! We hit a player
                health.SubstactHealth(WeaponData.damage);
                health.AddEffects(WeaponData.effects);
            }
        }
    }
}

I am spawning player normally and on network server (like this):

public override void OnServerAddPlayer(NetworkConnectionToClient conn)
{
         //TODO Read the code and fix it
        GameObject Player = Instantiate(playerPrefab,transform.position,transform.rotation); //! create a player
        NetworkServer.Spawn(Player);

If anybody can help, please write it down below.

I have asked ChatGP and it told me like 7 steps, i did them all and nothing worked.

Some of these steps were:

  • Make sure that the object that is calling the CmdFight() function has a NetworkIdentity component. You can add it by clicking on the object in the Unity editor, and then in the "Add Component" menu, searching for "Network Identity".

or

  • Make sure that the NetworkIdentity component is correctly set up. In the inspector, check that the "Local Player Authority" option is enabled. This option allows the object to execute command functions as a local player object.

or even

Make sure that the object was instantiated using the NetworkServer.Spawn() method or a NetworkIdentity component with the Local Player Authority option selected.

1

There are 1 best solutions below

0
Solidus_Terminal On

'I BELIEVE' the problem is that you aren't spawning the player objects with client authority. By default when an object is spawned on the network the network host (server) has authority over that object. But in order to send a Command to the server, you have to send the command from a component on an object you have authority over on the client.

You can assign authority when the object is spawned by using these 2 lines:

GameObject go = Instantiate(prefab);
NetworkServer.Spawn(go, connectionToClient);

Or you can assign authority to an object that is already spawned using: identity.AssignClientAuthority(conn);

It's all explained at this url (where the lines of code were copied from).

https://mirror-networking.gitbook.io/docs/manual/guides/authority