Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

I don't get networking

Discussion in 'Multiplayer' started by spiralgear, Apr 23, 2008.

  1. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    I read through all the networking docs/guides twice now, but im still having trouble getting a simple networking test game to work


    Okay so im using the "loader" level that is in the network example, i have it set up to load my level


    in my level there is a terrain, a directional light, and an empty object called spawner


    This is spawners code:
    Code (csharp):
    1.  
    2. var playerPrefab : Transform;
    3.  
    4. function OnNetworkLoadedLevel (){
    5. var rand=Vector3(transform.position.x*Random.Range(1, 2),transform.position.y*Random.Range(1, 2),transform.position.z*Random.Range(1, 2));
    6. Network.Instantiate(playerPrefab,rand , transform.rotation, 0);
    7.  
    8.  
    9. }
    10.  
    11. function OnPlayerDisconnected (player : NetworkPlayer)
    12. {
    13.     Debug.Log("Server destroying player");
    14.     Network.RemoveRPCs(player, 0);
    15.     Network.DestroyPlayerObjects(player);
    16. }
    17.  

    the player Prefab is just the Standard assets FPS Walker, that has a networkView on it watching a script called "NetworkInterpolatedTransform" that i got from the third person example, it also has a script called "ThirdPersonNetworkInit" on it also from the network example but ive changed it to this
    Code (csharp):
    1. function OnNetworkInstantiate (msg : NetworkMessageInfo) {
    2.     // This is our own player
    3.     if (networkView.isMine)
    4.     {
    5.         Debug.Log("mine");
    6.         GetComponent(MouseLook).enabled=true;
    7.         GetComponent(FPSWalker).enabled=true;
    8.         GetComponent("NetworkInterpolatedTransform").enabled = false;
    9.  
    10.     }
    11.     // This is just some remote controlled player
    12.     else
    13.     {
    14.         name += "Remote";
    15.         GetComponent(FPSWalker).enabled = false;
    16.         GetComponent(MouseLook).enabled=false;
    17.         GetComponent("NetworkInterpolatedTransform").enabled = true;
    18.  
    19.     }
    20. }
    21.  

    The result is that you control the opposite FPSWalker, that is, instead of you moving when you press forward, you can see the other FPSWalker moving, and vice versa

    I very intently read through the reference guide and the docs, and although I understand what im trying to do, im having trouble translating it into unity



    Am I making a simple mistake? or am I no where close?
     
  2. JavaChilly

    JavaChilly

    Joined:
    Mar 8, 2008
    Posts:
    33
    Are you seeing both of them move or just the remote object?
     
  3. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    I think i figured out the problem

    when i network instantiate the FPSWalker prefab it instantiates its child camera as well, so both host and client have TWO cameras in their hierarchy instead of just its own, and it has apparently decided to look throgh the remote players camera instead of its own, in the network examples the camera is not instantiated, the level loads with a single camera that then follows you, and i didn't take that into account


    i just started up my scene and after it connected i went to the remote players main camera in the hierarchy and turned it off and then it worked perfect



    what should i do to stop this from happening? My first thought would be to have an empty GO where i want the camera to be and after the connection is made locally instantiate a camera at the empty game objects position/rotation



    but im not sure if it would work, and i would assume there is an better way right?


    how should i do that?
     
  4. JavaChilly

    JavaChilly

    Joined:
    Mar 8, 2008
    Posts:
    33
    Thats basically what I do. I've got a number of points of view, empty GOs setup as children of my networked prefab object.

    I leave my Main Camera at the top of the scene hierarchy, and OnNetworkInstantiate, I do:

    Code (csharp):
    1. Camera.main.SendMessage("AttachTo", transform);
    The AttachTo function then changes the parent of the camera's transform to the currently selected POV. It also toggles the objects renderer on and off as appropriate for the camera's location.
     
  5. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    that works perfect thanks, its finally starting to click