Search Unity

Network camera(multiplayer)

Discussion in 'Scripting' started by ekoops, Jan 7, 2011.

  1. ekoops

    ekoops

    Joined:
    May 24, 2010
    Posts:
    74
    hello. im trying to make a lan network game and i have big problems with my camera. i now about unity scripting to singleplay. but all that network and networkplayer and network.player. all that new functions. i have no idea how to start.. i have read alot about it. but i dont se the point in anything:S total lost. any ideas how to start. ??

    my first problem is my camera. i got players connectet. but thay use the same camera..
     
  2. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Do you mean you cant see your other players?And they cant see you.Im not quite understanding your problem exactly.
     
  3. ekoops

    ekoops

    Joined:
    May 24, 2010
    Posts:
    74
    i can se the other players. but they use the same camera. i just put the camera under my player with no scripts on. my clients and my servers use the same camera..
     
  4. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    So whats the problem?What is going wrong that you dont like and want to fix?
     
  5. ekoops

    ekoops

    Joined:
    May 24, 2010
    Posts:
    74
    hehe :D the problem is that.. both players use the same camera.. they need a camera each to follow.. just like a ect. world of warcraft. so each player got thire own camera.

    Sry for my bad bad english btw. and ty for fast reply.
     
  6. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Ok next question.Are you using a server like smart fox?
     
  7. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    The easiest way to handle setting the camera in a Unity networked multiplayer game, in my experience, is to only have one camera in the scene, and have whatever follow script you have on it have no target. Then, when a player's prefab is instantiated, use networkView.IsMine to check if the current player is the owner of that player prefab, and then set the camera target to the player prefab.

    If you are using Network.Instantiate to spawn player prefabs when new players connect, you would put something like this on your player prefab (for the sake of example, I'm pretending you're using the SmoothFollow script on your camera):[

    Code (csharp):
    1.  
    2. OnNetworkInstantiate(info : NetworkMessageInfo){
    3.     if(networkView.IsMine){ //if I am the owner of this prefab
    4.         Camera.main.GetComponent(SmoothFollow).target = transform;
    5.     }
    6. }
    7.  
     
  8. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    what does this have to do with a camera ?
    Just as Legend says, attach a script to the camera with a target and set the target at spawning.
     
    Khyrid likes this.
  9. ekoops

    ekoops

    Joined:
    May 24, 2010
    Posts:
    74
    ty alot for this:D it works.. and now i understand how to make the clients own things.
     
  10. falconer

    falconer

    Joined:
    Feb 6, 2013
    Posts:
    21

    Hi, I used this in the smooth follow script. It works fine at the server side, but at the client side, the camera does follow th car but it doesnt rotate as per the rotation of the car. What is wrong here?
     
  11. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    You should try using this:
    Code (csharp):
    1. function OnNetworkInstantiate (info : NetworkMessageInfo) {
    2.     if (networkView.isMine) { //if I am the owner of this prefab
    3.         GameObject.Find("Camera").GetComponent(SmoothFollow).target = transform;
    4.     }
    5. }
    It'll directly find the camera on both sides of the server and It'll assign the transform. If you camera is not named "Camera", dont forget to change the object name to look for in the GameObject.Find

    Hope that helps :D
     
    Vykax likes this.
  12. Skibur

    Skibur

    Joined:
    Jul 6, 2013
    Posts:
    20
    Create a new script;

    Code (csharp):
    1. void Update(){
    2. if(networkView.isMine){
    3. camera.gameObject.enabled = true;
    4. }
    5. else{
    6. camera.gameObject.enabled = false;
    7. }
    8.  
    Save it, apply the script to your Camera that's in your prefab.

    You have to make sure that there can only be one camera active when you play multiplayer games, otherwise Unity 3D will be confused with several other camera in the scene too.

    P.s. Be sure to have another script that toggles audiolistener too! It slows down your performance if you have more than one!
     
  13. Vykax

    Vykax

    Joined:
    May 31, 2014
    Posts:
    4
    I'm trying to use this in my game but I'm getting a NullReferenceException error, are you sure this code works? if so how do I avoid this issue?
     
  14. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    As I said in my last reply, you must change the name in the GameObjet.Find field to the name of the respective camera, but I just realized that using the Find isn't the best idea. Use this:
    Code (JavaScript):
    1. function OnNetworkInstantiate (info : NetworkMessageInfo) {
    2.     if (networkView.isMine) { //if I am the owner of this prefab
    3.         Camera.main.GetComponent(SmoothFollow).target = transform;
    4.     }
    5. }
    If this doesn't work, please give the full error so we can help fully.
     
    Vykax likes this.
  15. Vykax

    Vykax

    Joined:
    May 31, 2014
    Posts:
    4
    I think the error is coming from " = transform" I'm woefully beginner, still having issues with this one.
    My camera is named "Camera", and I am using the SmoothFollow script aswell.

    Here is the error:

    NullReferenceException: Object reference not set to an instance of an object
    Player_mover.OnNetworkInstantiate (NetworkMessageInfo info) (at Assets/Player_mover.js:87)
    UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
    NetworkManagerScript:spawnPlayer() (at Assets/NetworkManagerScript.js:48)
    NetworkManagerScript:OnServerInitialized() (at Assets/NetworkManagerScript.js:56)
    UnityEngine.Network:InitializeServer(Int32, Int32, Boolean)
    NetworkManagerScript:startServer() (at Assets/NetworkManagerScript.js:28)
    NetworkManagerScript:OnGUI() (at Assets/NetworkManagerScript.js:74)





    And I would like to thank you sincerely for replying so fast and helping me out. I've been pulling my hair out over this one cause your code looks like it should work perfectly.

    Edit: I put the code in my player game object (thats just where it made sense to me) I just tossed it in his movement script but I've also tried putting it in it's own script to no avail.



    edit edit:

    I set up my multiplayer through this tutorial, I am running a master server aswell
     
    Last edited: Jun 22, 2014
  16. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    Okay, new edit to the code.
    Code (JavaScript):
    1. var player : Transform;
    2.  
    3. function OnNetworkInstantiate (info : NetworkMessageInfo) {
    4.     if (player == null) {  // quit the function if the player var is null
    5.         Debug.LogError("Player variable is null.  Please define variable in Inspector");
    6.         return;
    7.     }
    8.  
    9.     if (networkView.isMine) { // if I am the owner of this prefab
    10.         Camera.main.GetComponent(SmoothFollow).target = player;
    11.     }
    12. }
    This new code should definitely work no matter where the script is, but make sure to define the player variable in the inspector.
     
  17. Vykax

    Vykax

    Joined:
    May 31, 2014
    Posts:
    4

    Still getting:
    NullReferenceException: Object reference not set to an instance of an object
    Player_mover.OnNetworkInstantiate (NetworkMessageInfo info) (at Assets/Player_mover.js:71)
    UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
    NetworkManagerScript:spawnPlayer() (at Assets/NetworkManagerScript.js:48)
    NetworkManagerScript:OnServerInitialized() (at Assets/NetworkManagerScript.js:56)
    UnityEngine.Network:InitializeServer(Int32, Int32, Boolean)
    NetworkManagerScript:startServer() (at Assets/NetworkManagerScript.js:28)

    I see it says something about the mt network manager script in my error, does that have anything to do with it?

    My network script is as follows:
    var playerPrefab:GameObject;
    var spawnObject:Transform;
    var gameName:String = "TestGame";

    private var refreshing:boolean;
    private var hostData:HostData[];

    private var btnX:float;
    private var btnY:float;
    private var btnW:float;
    private var btnH:float;

    function Start(){




    btnX=Screen.width *0.05;
    btnY=Screen.width *0.05;
    btnW=Screen.width *0.1;
    btnH=Screen.width *0.1;
    }

    function startServer(){
    Network.InitializeServer(32,25001,!Network.HavePublicAddress);
    MasterServer.RegisterHost(gameName,"TestGame","This is a test");
    }

    function refreshHostList(){
    MasterServer.RequestHostList(gameName);
    refreshing = true;
    }

    function Update(){
    if (refreshing){
    if(MasterServer.PollHostList().Length > 0){
    refreshing = false;
    Debug.Log(MasterServer.PollHostList().Length);
    hostData = MasterServer.PollHostList();
    }
    }
    }

    function spawnPlayer(){
    Network.Instantiate(playerPrefab,spawnObject.position, Quaternion.identity, 0);


    }

    //Messages
    function OnServerInitialized(){
    Debug.Log("Server initialized!");
    spawnPlayer();
    }

    function OnConnectedToServer(){
    spawnPlayer();
    }

    function OnMasterServerEvent(mse:MasterServerEvent){
    if(mse == MasterServerEvent.RegistrationSucceeded){
    Debug.Log("Registered Server");
    }
    }

    // GUI
    function OnGUI(){
    if(!Network.isClient && !Network.isServer){
    if(GUI.Button(Rect(btnX, btnY, btnW, btnH), "Start Server")){
    Debug.Log("Starting Server");
    startServer();
    }




    if(GUI.Button(Rect(btnX, btnY * 1.2+ btnH, btnW, btnH), "Refresh Hosts")){
    Debug.Log("Refreshing");
    refreshHostList();
    }
    if(hostData){
    for(var i:int=0; i<hostData.length; i++){
    if(GUI.Button(Rect(btnX*1.5+btnW,btnY*1.2+(btnH*i),btnW*3,btnH*0.5),hostData.gameName))
    {
    Network.Connect(hostData);
    }}
    }}
    }



    Sorry for having so much trouble with this :p
     
  18. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    Could you please put your code into the code inserter thingy? When writing your reply, look for the toolbar tab that says insert and click it, then click code, and paste your code in there. It makes it easier to read.
     
    Vykax likes this.
  19. Vykax

    Vykax

    Joined:
    May 31, 2014
    Posts:
    4
    I resolved my issue. In case anyone else is having this same issue, what I did was I just bypassed setting up a 3rd person camera all together, I noticed that most of the tutorials I've watched have been using 1st person cameras, so I just made a camera the child of the prefab, with a code that disables the camera if it's not owned by that particular client, and set the coordinates for the camera to be behind the player. I loose the advantage of a smooth follow, but I'm going to be adding camera controls to zoom the camera in and out and pivot it around, so it might end up working out better for me. Thanks for your help with all this.
     
    blaumeisje likes this.
  20. Research_3DVAL

    Research_3DVAL

    Joined:
    Jun 26, 2014
    Posts:
    15
    would you mind me asking about the code that you used for this? I'm working on some similar stuff and facing the same problem.
     
    quencat likes this.