Search code examples
c#unity-game-enginemultiplayerunity3d-unet

Unet. Multiplayer Vehicles


I am currently developing multiplayer fps shooter, and i am stuck on vehicles.

i tried these "ClientRpc" stuff, "Command" and etc. So Player has Controll script, and this script has function OnControllerColliderHit, so if this happens i call void Disable();

in this method, i disable all colliders and some components i don't need like: shooting, moving, camera etc...

basically all i need is: Disable some player components when he gets in car. script works perfectly in singleplayer, but in multiplayer it looks really weird.

I have also asked this on Unity answers, but didn't get any: http://answers.unity3d.com/questions/1235436/ (scripts are there)

ps(if you'll need some more info or scripts for me to post, just comment. )


Solution

  • I think the problem is that you are only enabling/disabling components in the server. Commands are called on the server only, so you might want to use RPC to do the same in the clients.

     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;
         }
    }