简体   繁体   中英

Clients cannot spawn object on server - Unity mirror networking c#

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.

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).

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