客户端无法在服务器上生成对象 - Unity 镜像网络 c#
Clients cannot spawn object on server - Unity mirror networking c#
我已经为此苦苦挣扎了一两天了。由于某种原因 NetworkServer.Spawn() 似乎不起作用。据我了解,可以从客户端调用 NetworkServer.Spawn() 来生成服务器上的对象,然后从服务器调用对象在所有客户端上生成。目前,当客户端射击时,它只会出现在该客户端上(不会出现在服务器上),当主机射击时,弹丸会在主机和客户端上生成。
代码顶部有using Mirror标签,脚本源自Networkbehaviour。下面这段代码在客户端调用:
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);
}
有谁知道为什么弹射物没有从客户端生成在服务器上?代码示例(在 c# 中)也将非常有帮助。
NetworkServer.Spawn()
只能在服务器上调用。它使 GameObject
被发送到所有客户端,这样他们就可以看到它,与之交互等等。因此,您需要从客户端调用服务器上的一个函数,该函数将为您 Spawn()
您的对象。
[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
}
或调用NetworkServer.SpawnWithClientAuthority()
,如果您希望实例化对象具有客户端权限(客户端需要设置shotProjectiles
属性)。
我已经为此苦苦挣扎了一两天了。由于某种原因 NetworkServer.Spawn() 似乎不起作用。据我了解,可以从客户端调用 NetworkServer.Spawn() 来生成服务器上的对象,然后从服务器调用对象在所有客户端上生成。目前,当客户端射击时,它只会出现在该客户端上(不会出现在服务器上),当主机射击时,弹丸会在主机和客户端上生成。
代码顶部有using Mirror标签,脚本源自Networkbehaviour。下面这段代码在客户端调用:
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);
}
有谁知道为什么弹射物没有从客户端生成在服务器上?代码示例(在 c# 中)也将非常有帮助。
NetworkServer.Spawn()
只能在服务器上调用。它使 GameObject
被发送到所有客户端,这样他们就可以看到它,与之交互等等。因此,您需要从客户端调用服务器上的一个函数,该函数将为您 Spawn()
您的对象。
[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
}
或调用NetworkServer.SpawnWithClientAuthority()
,如果您希望实例化对象具有客户端权限(客户端需要设置shotProjectiles
属性)。