0

我目前正在开发多人 fps 射击游戏,但我被困在车辆上。

我尝试了这些“ClientRpc”的东西,“Command”等等。所以 Player 有 Controll 脚本,这个脚本有 OnControllerColliderHit 函数,所以如果发生这种情况我调用 void Disable();

在这种方法中,我禁用了所有对撞机和一些我不需要的组件,例如:射击、移动、相机等...

基本上我需要的是:当他上车时禁用一些播放器组件。脚本在单人游戏中完美运行,但在多人游戏中看起来很奇怪。

我也在 Unity 的答案上问过这个问题,但没有得到任何答案: http: //answers.unity3d.com/questions/1235436/ (有脚本)

ps(如果您需要更多信息或脚本供我发布,请发表评论。)

4

1 回答 1

0

我认为问题在于您仅启用/禁用服务器中的组件。命令仅在服务器上调用,因此您可能希望使用RPC在客户端中执行相同的操作。

 void Update()
 {
     //if there is driver in car then we can controll it. (it's a paradox for me, if there is driver and i am passanger i also can controll it lol.)
     if(hasDriver)
     {
         GetComponent<carController>().enabled = true;
     }
     else
     {
         GetComponent<carController>().enabled = false;
     }
 }

 //command function for sitting. in car.
 [Command]
 public void CmdSit(string _player)
 {
     //i increase how many people are in car
     sitPlayers++;

     cam.enabled = true;
     //find player who sat there.
     GameObject player = GameObject.Find(_player);



     //i think you will get it >>
     if (hasDriver)
     {
         player.transform.parent = Sitter.transform;
         player.transform.position = Sitter.transform.position;
         cam.GetComponent<AudioListener>().enabled = true;


     }
     else
     {
         player.transform.parent = Driver.transform;
         hasDriver = true;
         cam.GetComponent<AudioListener>().enabled = true;
         player.transform.position = Driver.transform.position;             
     }

     RpcSit(_player, hasDriver);

 }

[ClientRpc]
public void RpcSit(string _player, bool _driver)
{
     cam.enabled = true;
     //find player who sat there.
     GameObject player = GameObject.Find(_player);

     //i think you will get it >>
     if (_driver)
     {
         player.transform.parent = Sitter.transform;
         player.transform.position = Sitter.transform.position;
         cam.GetComponent<AudioListener>().enabled = true;
     }
     else
     {
         player.transform.parent = Driver.transform;
         cam.GetComponent<AudioListener>().enabled = true;
         player.transform.position = Driver.transform.position;
     }
}
于 2017-07-04T19:03:56.010 回答