Search Unity

looking for help on a simple network demo

Discussion in 'Multiplayer' started by jg225106, Nov 20, 2008.

  1. jg225106

    jg225106

    Joined:
    Nov 20, 2008
    Posts:
    8
    in my quest to understand the magic of networking, i've decided to try and build a simple network demo of my own. what im trying to do is essentially remake the third person network demo, sans Lerpz. just objects moving around in the same world. right now, i dont even need the ability to search for servers, just typing in an ip and port is good.

    what ive got now are buttons that let me create a server or connect to a specific ip. they appear to work fine. however, when a client connects and spawns a player, there are obviously major issues with deciding which machine controls which player. it appears that everyone connected, the server included, trys to control every player in the game. when the first client spawns, the cameras in the each player switch, so that the host is looking through the newly created player's camera and the new client is looking through the host's original camera.

    ive obviously missed a step (or twelve) somewhere, but for the life of me i cant figure it out. i'd greatly appreciate it if someone could tell me the dumb mistake i made or the flaw in my understanding of how this all works.

    heres the code im using:
    Code (csharp):
    1.  
    2. var IP = "000.000.00.0";
    3. var Port = "25000";
    4.  
    5. function OnGUI () {
    6.    
    7.     IPa = GUI.TextField (Rect (Screen.width - 90, 80, 85, 20), IP);
    8.     Port = GUI.TextField (Rect (Screen.width - 90, 105, 50, 20), Port);
    9.    
    10.     if (GUI.Button (Rect (Screen.width - 90, 20,70,25), "Host")) {
    11.         Network.InitializeServer(10, 25000); // allow 10 players, port 25000
    12.        
    13.        
    14.     }
    15.    
    16.     if (GUI.Button (Rect (Screen.width - 90, 50,70,25), "Connect")) {
    17.         var Portint = parseInt(Port);
    18.         Network.Connect(IP, Portint);
    19.        
    20.     }
    21.    
    22. }
    23. // remove gui once connected/created server
    24. function OnConnectedToServer() {
    25.     Destroy(this);
    26. }
    27.  
    28.  
    29. function OnServerInitialized()  {
    30.     Destroy(this);
    31.    
    32. }
    33.  
    34. ///////// separate script file
    35.  
    36. var playerTube : Transform;
    37.  
    38.  
    39.  
    40. function OnConnectedToServer() {
    41.     Network.Instantiate(playerTube, transform.position, transform.rotation, 0);
    42.     Debug.Log("connected to server");
    43.    
    44. }
    45.  
    46.  
    47. function OnServerInitialized()  {
    48.  
    49.     Network.Instantiate(playerTube, transform.position, transform.rotation, 0);
    50.     Debug.Log("server made");
    51.    
    52. }
    53.  
    54.  
    all this code is attached to an empty game object, and my player prefab has a network view with state synchronization set to reliable and observing the player prefab's transform.

    thanks a lot for reading,
    jason
     
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Looking at the code, the objects seen to instantiate fine.

    What you've probably missed is that both objects, regardless of who network.instantiated them will have the input (camera) controll scripts. Unity will not take care of this, which isn't strange once you get it.

    Add a script to these objects along the lines of:

    Code (csharp):
    1. function OnNetworkInstantiate(){
    2.   if(networkView.isMine){
    3.      //enable local-user control
    4.      //disable some non-localplayer stuff
    5.   }else{
    6.      //disable local-user control
    7.      //This is an external player
    8.   }
    9. }
     
  3. jg225106

    jg225106

    Joined:
    Nov 20, 2008
    Posts:
    8
    thanks for the reply! sorry i took so long to respond, i was on vacation.

    that made it work. it does make sense once you see all the pieces working together.

    the scripts are attached, if anyone wants to take a look.

    thanks again,
    jason
     

    Attached Files: