Command, ClientRpc or... need help

Hi,
Iam new to the networking and unet. What Iam trying to achieve is this. Spawn the cube with rigidbody2d and allow players to push/shoot it by .AddForce(). Problem is that just the host can shoot but not the clients. Dont know if to use Command, ClientRpc or something else.

So my spawn script works great. cubeSpawner have network identity with Server Only checked.

public class cubeSpawner : NetworkBehaviour
{
   
    public GameObject cube;
    public Transform cubeSpawn;

    void Start ()
    {
        CmdSpawnTheCube (cubeSpawn.position, cubeSpawn.rotation);
    }

    [Command]
    void CmdSpawnTheCube (Vector3 pos, Quaternion rot)
    {
       
        GameObject _cube = (GameObject)Instantiate (cube, pos, rot);
       
        NetworkServer.Spawn (_cube);
    }
}

cube have attached network identity, network transform and rigidbody2d.

player can charge energy and shoot the cube when is near, my code

public class playerController : NetworkBehaviour
{
public bool haveCube;
public Transform cube;

public float regenerationForce;
float relativeForce = 0f;
bool isCharging;

void Update ()
    {
        if (!isLocalPlayer) {
            return;
        }

if (Input.GetButtonDown ("Fire1") && cube) {
            isCharging = true;
        }
        if (Input.GetButtonUp ("Fire1")) {
            isCharging = false;
        }
if (isCharging) {
            relativeForce += Time.deltaTime * regenerationForce;
            relativeForce = Mathf.Clamp01 (relativeForce);
        } else if (relativeForce > 0.1f) {
            CmdFire ();
        }

void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.tag == "cube") {

            haveCube = true;
            cube = other.transform;
        }
    }

    void OnTriggerExit2D (Collider2D other)
    {
        if (other.gameObject.tag == "cube") {
            haveCube = false;
            cube = null;
        }
    }
}
[Command]
    void CmdFire ()
    {
        Vector3 sp = Camera.main.WorldToScreenPoint (transform.position);
        Vector3 dir = (Input.mousePosition - sp).normalized;

        if (cube != null) {
            cube.GetComponent<Rigidbody2D> ().AddForce (dir * (relativeForce * 700));
        }
        relativeForce = 0f;
    }
}

This works on host but not on clients. Should I use ClientRpc or is there another error?

Thanks so much for the answer.

Hi !
Do you use the NetworkTransform component on the cube ?
If not you can send an RPC to the client to apply the force too but the physic in unity is not deterministic and you will be lucky if you get the same result !

1 Like

hi, thank you for your respond. Iam using NetworkTransform on the cube, sure. I also tried [ClientRpc] RpcFire() instead [Command] CmdFire() but with more or less the same result.

The position of the cube is not replicated ?

It is normal the CmdFire is just called on the server but the cube position should be replicated by the NetworkTransform component.

Iam sorry, I probably explained it all wrong:) position of cube IS replicated BUT client cant shoot so I assume that .AddForce is not used/applied by clients.

Hope it is clearer now. Thanks

Alright ! in the Update function on the client isLocalPlayer is true ?

Yes, it is. Ok, I just added debug.log

if (isCharging) {
            relativeForce += Time.deltaTime * regenerationForce;
            relativeForce = Mathf.Clamp01 (relativeForce);

            forceText.text = relativeForce.ToString ();
        } else if (relativeForce > 0.1f) {
            Debug.Log ("hit fire");
            CmdFire ();
            Debug.Log ("firing");
        }

and when on client “hit fire” and “firing” is shown in loop, no respond from CmdFire ();

So the server doesn’t receive the command ?
You can try to set the LogFilter to debug to get more information.

Also you can add debug.log to be sure cube is not null on the server.

Iam lost with all this on server, no on server:)) What do you mean by that?

I will look to LogFilter and hopefully it will help.

Sorry :stuck_out_tongue:
I mean if you put a breakpoint in CmdFire, do you reach it ?
Command are only called on the server side that why I asked to check if the variable cube is not null on the server.

If you are willing to help me, I will send you PM.:slight_smile:

Sur I can try to help you