Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer Woes

Discussion in 'Multiplayer' started by RoboZombie, Feb 4, 2010.

  1. RoboZombie

    RoboZombie

    Joined:
    Jan 19, 2010
    Posts:
    23
    Right now there is one very irritating problem I am having. How would you go about using multiple camera in a scene (1 per player). Using the stock First Person Controller, the viewpoint seems to be flickering between the two players, and I honestly dont have any clue how to solve this. All I am doing in code is the following
    Code (csharp):
    1. var playerPrefab : Transform;
    2.  
    3. function Update()
    4. {
    5.     if(Input.GetKeyDown("n"))
    6.     {
    7.         Network.InitializeServer(32, 25000);
    8.         networkView.RPC ("SpawnPlayer", RPCMode.AllBuffered);
    9.     }
    10.     if(Input.GetKeyDown("m"))
    11.     {
    12.         Network.Connect("127.0.0.1", 25000);
    13.         networkView.RPC ("SpawnPlayer", RPCMode.AllBuffered);
    14.     }
    15. }
    16.  
    17. @RPC
    18. function SpawnPlayer()
    19. {
    20.     Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    21. }
    Is there anyway that my problem can be solved ?
     
  2. tbuczek

    tbuczek

    Joined:
    Jan 20, 2010
    Posts:
    11
    Your Code confuses me a little bit.

    First, you don't need a camera object for every prefab. You only need 1 per game. You could have more than 1 if you want... but each client will have their own copy of the one camera when the game starts.

    If you really want to to keep the camera per prefab you need to Disable them at design time, then enable the camera for the active prefab you instantiate. This way, you do not have an active camera for every Network Instanciated object

    Second, You are calling an RPC and in the call doing a network instanciate.

    What that will do is all connected computers will spawn a Network prefab...

    Sooo....
    Server Starts, Calls RPC to everyone (only himself)
    1 Prefab is created on the network.
    Client A Starts, Gets Buffered RPC Call, Creates a Net Instanciated version of the Serves prefab
    2 Prefabs
    Client A then calls the Spawn RPC on everyone (Himself and the server). Both call Network.Inst
    4 Prefabs
    Client B Starts, Gets Buffered RPC Call, Creates a Net Instanciated version of the Server prefab, and the clients rpc prefab
    6 Prefabs
    Client B then calls the Spawn RPC on everyone (Himself, the server, and the other client).
    9 Prefabs Prefabs

    I think I said that right... I am at work, so I can't check now. In short, every client that connects will cause all connected clients and the server to spawn another networked prefab

    Try this instead:

    Code (csharp):
    1. var playerPrefab : Transform;
    2.  
    3. function Update()
    4. {
    5.    if(Input.GetKeyDown("n"))
    6.    {
    7.       Network.InitializeServer(32, 25000);
    8.      SpawnMyPlayer()
    9.    }
    10.    if(Input.GetKeyDown("m"))
    11.    {
    12.       Network.Connect("127.0.0.1", 25000);
    13.      SpawnMyPlayer()
    14.    }
    15. }
    16.  
    17.  
    18. function SpawnMyPlayer()
    19. {
    20.    Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    21. }
    OR
    Code (csharp):
    1.  
    2. var playerPrefab : Transform;
    3.  
    4. function Update()
    5. {
    6.    if(Input.GetKeyDown("n"))
    7.    {
    8.       Network.InitializeServer(32, 25000);
    9.       networkView.RPC ("SpawnPlayer", RPCMode.AllBuffered);
    10.    }
    11.    if(Input.GetKeyDown("m"))
    12.    {
    13.       Network.Connect("127.0.0.1", 25000);
    14.       networkView.RPC ("SpawnPlayer", RPCMode.AllBuffered);
    15.    }
    16. }
    17.  
    18. @RPC
    19. function SpawnPlayer()
    20. {
    21.    GameObject.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    22. }
     
  3. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    I can tell you how I do it in Multi-player Baseball Online.
    I have a script that turns off all the cameras in the scene and then turns on the one that the player is supposed to be using.
    Later I will turn back on the camera on his player controller if I want the client to use that camera.
    Then I turn off the rest of them.

    I do this by enabling or disabling the cameras like this.

    CameraObject.GetComponent("Camera").enable=false/true;