Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNET Client Cant Send Server Commands, Host Can

So as the title suggests, I'm having a problem where commands being sent by the client are not triggered.

The basic functionality I'm trying to get working is that when an enemy is in front of the player and I click, that player will be momentarily stunned. Works fine if I'm host, and both sides perfectly register.

If I'm playing as client, I get to the point where the command "should" be sent, but I notice I get a warning that says "Trying to send command for non-local player". As well, nothing happens on either end. Obviously I must be doing something wrong client side, but have no idea what other way to go about this.

The "problem" code.

            if (Input.GetButtonDown("Fire1")) {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit = new RaycastHit();
            Physics.Raycast(ray, out hit, 20f);
            if (hit.transform != null) {
                if (hit.rigidbody != null) {
                    PlayerController controller = hit.rigidbody.GetComponent<PlayerController>();
                    if (controller != null) {
                        if (!controller.stunned) {
                            // Send the stun command to the server
                            controller.CmdStun();
                        }
                    }
                }
            }
        }

The method calls

[Command]
public void CmdStun() {
    // Report that the stun was sent
    UnityEngine.Debug.Log("Stun Sent");
    RpcStun();
}

[ClientRpc]
public void RpcStun() {
    // Activate the stun timer.
    stunTimer.Start();
    // Track the players original color.
    normalColor = manager.color;
    // Make the players bot look like its shut down.
    manager.InitiateColorChange(Color.black);
    // Other code will check against this before trying to send another stun command.
    stunned = true;
}

Edit: Upon request heres the two scripts in their entirety.

http://pastebin.com/mr4A9ZgH

http://pastebin.com/Qg0AjCCD

Player configuration in unity:

https://gyazo.com/400c5b3a95c1a58f9b6e930b0c3c853b

https://gyazo.com/c17a7317767a00e2150ff34b03a03e8f

https://gyazo.com/322731aefbe69f9567d2b395523b8f2a

Full Warning Message

Trying to send command for non-local player. UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String) PlayerController:CallCmdStun() ObjectInteractor:Update() (at Assets/Scripts/ObjectInteractor.cs:58)

like image 817
Sebastian Thomas Edward Lawe Avatar asked Oct 20 '22 03:10

Sebastian Thomas Edward Lawe


1 Answers

You must assign script to the root of the OBJECT. I was Having same problem. There are few constrains you must consider.

  1. A NetworkIdentity must be on the root game object of a spawnable prefab
  2. NetworkBehaviour scripts must be on the same game object as the NetworkIdentity, not on child game objects
  3. Prefabs can’t be registered with the NetworkManager unless they have a NetworkIdentity on their root object
like image 55
tarun khatri Avatar answered Nov 04 '22 21:11

tarun khatri