我有这个来自我正在制作的多人射击游戏的脚本,我对 [Command] 属性的使用有疑问
这是代码:
[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 也是一样的,远程玩家无法摧毁子弹或硬币,因为他没有网络服务器。
但是.. CmdHit 即使没有 [Command] 属性也能正常工作,我想知道为什么?