我正在使用Unet在Unity上开发多人游戏,当我通过在Unity编辑器上玩游戏主持比赛时,一切都按预期进行,但是当我在内置游戏上主持比赛时(点击“构建并运行”),则网络功能不起作用。我不太了解发生了什么,但我想我知道网络服务器有时处于非活动状态。这很奇怪,因为正如我在Unity编辑器中玩游戏来主持比赛时,在网络服务器正常工作之前所说的那样。
//此功能生成敌人的预制件
void Spawn()
{
GameObject enemy = Instantiate(enemyPrefab, spawnPoint.position,
spawnPoint.rotation);
NetworkServer.Spawn(enemy);
}
/ *此功能在播放器脚本中,当敌人或敌人的子弹碰到播放器时,播放器会受到损坏* /
private void OnTriggerEnter2D(Collider2D hitInfo)
{
//Enemy enemy = hitInfo.GetComponent<Enemy>();
if (hitInfo.tag == "Enemy" || hitInfo.tag == "EnemyBullet")
{
RpcTakeDamage(10, "hit by enemy");
}
//Instantiate(impactEffect, transform.position, transform.rotation);
}
[ClientRpc]
public void RpcTakeDamage(int _amount, string _sourceID)
{
if (!isHit)
{
StartCoroutine("HurtColor");
currentHealth -= _amount;
Debug.Log(transform.name + " now has " + currentHealth + " health ");
if (currentHealth <= 0)
{
Die(_sourceID);
}
}
}
/ *此功能在子弹行为脚本中,当子弹碰到敌人时,敌人会被消灭。 * /
private void OnTriggerEnter2D(Collider2D hitInfo)
{
playerMovement player = hitInfo.GetComponent<playerMovement>();
if (hitInfo.tag == "Enemy")
{
player = FindObjectOfType<playerMovement>();
player.bombPower = player.bombPower + UnityEngine.Random.Range(0.1f, 0.5f);
if(hitInfo.tag == "Enemy")
{
//Enemy enemy = FindObjectOfType<Enemy>();
//enemy.currentHealth -= damage;
player.CmdEnemyShot(hitInfo.transform.name, this.transform.name); //change this.transform.name with the player's name
}
Destroy(gameObject);
}
以下是一些错误消息:
/ *当玩家加入比赛时,我收到此警告,我不明白无法发送什么命令,可能是从NetworkBehaviour类* /
"Trying to send command for object without authority."
/ *此错误显示当生成敌人时,该敌人仍然可见,但显然仅在客户端* /
"SpawnObject for EnemyUpRight(Clone) (UnityEngine.GameObject),
NetworkServer is not active. Cannot spawn objects without an active server."
/ *当敌人的子弹击中了长矛时,将显示此错误,并且玩家无法受到伤害。 * /
"RPC Function RpcTakeDamage called on client."
//此警告也会出现
"Did not find target for sync message for 9"
///当敌人被击中时,它不会死并且会显示此错误
"SpawnObject for Marisa_bullet(Clone) (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server."
如果您需要我的项目来理解,here is the link. 代码可能有点混乱。