简体   繁体   English

客户端无法在服务器上生成 object - Unity 镜像网络 c#

[英]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.由于某种原因 NetworkServer.Spawn() 似乎不起作用。 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.据我了解,可以从客户端调用 NetworkServer.Spawn() 以在服务器上生成 object,从服务器上调用 object 在所有客户端上生成。 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.代码顶部有一个 using Mirror 标签,该脚本派生自 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.一个代码示例(在 c# 中)也将非常有帮助。

NetworkServer.Spawn() can be called only on the server. NetworkServer.Spawn()只能在服务器上调用。 It makes the GameObject to be sent to all clients, so they can see it, interact with it and so.它使GameObject被发送到所有客户端,因此他们可以看到它,与之交互等等。 Therefore, you need to call, from the client, a function on the server, that will Spawn() your object for you.因此,您需要从客户端调用服务器上的 function,这将为您生成Spawn()您的 object。

[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).或者调用NetworkServer.SpawnWithClientAuthority() ,如果你希望实例化的 object 具有客户端权限(客户端需要设置shotProjectiles属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM