我從我在做一個多人射擊遊戲這個腳本,我有一個關於使用[命令]的屬性爲什麼我需要[Command]用於NetworkServer.Spawn(),但不用於NetworkServer.Destroy()?
問題這是代碼:
[Command]
public void CmdShoot()
{
//Creat the bullet
GameObject Bullet = (GameObject)Instantiate(bulletPrefab, Barrle.transform.position, Barrle.transform.rotation);
BulletController bc = Bullet.GetComponent<BulletController>();
bc.SetOrigin(this.transform.name);
NetworkServer.Spawn(Bullet);
//Shoot the bullet
Rigidbody rb = Bullet.GetComponent<Rigidbody>();
rb.AddForce(cam.transform.forward * BulletForce, ForceMode.VelocityChange);
}
//Called from the bullet when it hit something
[Command]
public void CmdHit(GameObject other,GameObject _bullet)
{
Debug.Log(other.transform.name);
GameObject bullet = _bullet;
if (other.GetComponent<NetworkIdentity>() != null)
{
//Destroy the coin if you hit it
if (other.transform.name.Equals("Coin"))
{
NetworkServer.Destroy(other.gameObject);
}
//Apply dmg to other player if hit it
else if (other.transform.tag.Equals("Player"))
{
Player playerHit = GameManager.GetPlayer(other.transform.name);
playerHit.TakeDamage(BulletForce);
}
}
//Destroy the bullet if you hit anything
NetworkServer.Destroy(bullet.gameObject);
}
現在,如果我刪除來自CmdShoot的[Command]屬性,遠程播放器無法拍攝,因爲他沒有NetworkServer(據我所知)
我假設它對CmdHit來說是一樣的東西,而遠程播放器將不能夠銷燬子彈或硬幣,因爲他沒有NetworkServer。
但是..即使沒有[Command]屬性,CmdHit也能正常工作,我想知道爲什麼?
謝謝,但我想知道如何從客戶端上運行的函數調用'NetworkServer',而不是在服務器上運行? (如果我刪除命令屬性) –
@RonSerruya調用'NetworkServer'在這個意義上是不同的,因爲即使調用'NetworkServer.Destroy'的代碼只會在客戶端執行,那麼調用的結果(銷燬遊戲對象)將發生在服務器上。 我還沒有在網絡部分做足夠的工作,所以我主要在這裏做出假設,但我*認爲*您當前的代碼意味着服務器將被多次指示銷燬子彈遊戲對象,由每個客戶端。 –
明白了,謝謝你的幫助 –