我对团结网络和网络本身还很陌生。
游戏:,我有两个玩家,多人游戏,每个玩家都可以射击。
问题:
代码1使两名玩家在主机玩家按空格键时射击(仅在主机游戏中)。客户玩家不能从客户的游戏中射击。
注意:if (Input.GetKeyDown (KeyCode.Space))在CmdShoot方法中。
代码2正确执行。主机可以在主机游戏中射击,客户可以从客户的游戏中射击。
注意:if (Input.GetKeyDown (KeyCode.Space))在代码2的CmdShoot方法之外。
问题:代码1中的,为什么客户端不能拍摄,为什么主机同时拍摄?
代码1:
// Update is called once per frame
void Update () {
if (!isLocalPlayer) {
return;
}
CmdShoot ();
}
[Command]
void CmdShoot(){
if (Input.GetKeyDown (KeyCode.Space)) {
GameObject force_copy = GameObject.Instantiate (force) as GameObject;
force_copy.transform.position = gameObject.transform.position + gameObject.transform.forward;
force_copy.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * force.GetComponent<Force>().getSpeed();
NetworkServer.Spawn (force_copy);
Destroy (force_copy, 5);
}
}代码2:
// Update is called once per frame
void Update () {
if (!isLocalPlayer) {
return;
}
if (Input.GetKeyDown (KeyCode.Space)) {
CmdShoot ();
}
}
[Command]
void CmdShoot(){
GameObject force_copy = GameObject.Instantiate (force) as GameObject;
force_copy.transform.position = gameObject.transform.position + gameObject.transform.forward;
force_copy.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * force.GetComponent<Force>().getSpeed();
NetworkServer.Spawn (force_copy);
Destroy (force_copy, 5);
}发布于 2016-06-28 05:25:21
[Command]标记告诉客户端希望在服务器上执行此函数。
在代码1的情况下,代码调用CmdShoot()在服务器上执行,然后检查服务器是否按住了空间(因此只有主机能够拍摄所有的东西,而客户端根本无法拍摄)。
在代码2中,您的代码首先检查本地程序是否按住了空格键(独立于主机或客户端)。如果本地程序持有空格键,则调用服务器上的CmdShoot()。
https://stackoverflow.com/questions/38066978
复制相似问题