1
votes

I've been struggling with this for a day or two now. For some reason NetworkServer.Spawn() isn't seeming to work. To my understanding, NetworkServer.Spawn() can be called from a client to spawn and object on the server, and from the server, the object is spawned on all of the clients. Currently, when a client shoots, it only appears on that client, (NOT on the server) and when the host shoots, the projectile is spawned on the host and the clients.

There is a using Mirror tag at the top of the code and the script derives from Networkbehaviour. This piece of code below is called on the client:

void Shoot()
{
        //Spawn porjectile on local machine
        GameObject shot = Instantiate(projectile) as GameObject;

        //Set shot projectile position
        shot.transform.position = projectilePoint.position;
        shot.transform.rotation = transform.rotation;

        //Set projectile component of the shot projectile
        Projectile shotProjectile = shot.GetComponent<Projectile>();

        //Set properties of shot projectile
        shotProjectile.damage = damage;
        shotProjectile.direction = projectilePoint.position - transform.position;
        shotProjectile.speed = projectileTravelSpeed;
        shotProjectile.team = team;
        shotProjectile.shotBy = gameObject;

        NetworkServer.Spawn(shot);
    
}

Does anyone have an idea of why the projectile isn't spawned on the server from a client? A code example (in c#) would also be very helpful.

1
Looks like Network Server is a 3rd party protocol. Cannot find any documentation for Spawn. I did find a pdf document online : lora gateway to network server interface definition :thethingsnetwork.org/forum/uploads/default/original/1X/…jdweng

1 Answers

2
votes

NetworkServer.Spawn() can be called only on the server. It makes the GameObject to be sent to all clients, so they can see it, interact with it and so. Therefore, you need to call, from the client, a function on the server, that will Spawn() your object for you.

[Client]
void Shoot()
{
    //Call spawn on server
    CmdShoot(projectile, projectilePoint.position, transform.rotation);
}

[Command]
void CmdShoot(GameObject projectile, Vector3 position, Quaternion rotation)
{

    GameObject shot = Instantiate(projectile) as GameObject;

    //Set shot projectile position
    shot.transform.position = projectilePoint.position;
    shot.transform.rotation = transform.rotation;

    //Set projectile component of the shot projectile
    Projectile shotProjectile = shot.GetComponent<Projectile>();

    //Set properties of shot projectile
    shotProjectile.damage = damage;
    shotProjectile.direction = projectilePoint.position - transform.position;
    shotProjectile.speed = projectileTravelSpeed;
    shotProjectile.team = team;
    shotProjectile.shotBy = gameObject;

    NetworkServer.Spawn(shot);
    RpcOnShoot();
}

[ClientRpc]
void RpcOnShoot()
{
    //Called on all clients
}

Or call NetworkServer.SpawnWithClientAuthority(), if you wish the instantiated object with client authority (the client needs to set shotProjectiles properties then).