Setting up Cameras for Multiplayer

Hello. I followed CG Cookie’s intro to networking guide and had everything running smoothly. However, he just uses one unparented camera. I’m trying to give users their own individual cameras (as any FPS would do). However, when I do that, all players seem to share a camera. That’s not entirely accurate. Let me provide a scenario.

Two players connect, player A and player B. Each has a camera.

If nobody moves their camera, player A sees through his camera and player B sees through his camera (i.e., this works as intended).

Whenever player A moves his camera, player B now sees through player A’s camera. All movement that player A makes is reflected on both screens.

Whenever player B moves his camera, player A now sees through player B’s camera. All movement that player B makes is reflected on both screens.

Any idea how to set up cameras properly? I’m fairly certain the issue is that the network is passing on the camera information to all players. I’m not really sure how to remedy this.

Use isMine, and check to see if the camera is the clients. If the camera is the clients just deactivate the camera. Just a quick example would be…

void OnNetworkInstantiate(NetworkMessageInfo info)
{
if (networkView.isMine)
camera.main.enabled = true;
else
camera.main.enabled = false;
}
2 Likes

I did something similar to this, but not quite the same. I did camera.enabled. Does the .main make a difference? I’ll give it a try when I get off work. By the way, I’m using unityscript, but I checked the manual and it looks like it’s the same code.

I checked for isMine on the character controller and it worked well.

I also might no have done it OnNetworkInstantiate. Hmm… Things to check.

.main just enables the main camera. I don’t know what camera the players are using, but if the main camera is following them around then .main should work fine. If not you have to get a reference to the camera they are using then disable that camera.

1 Like

I’m instantiating a prefab whenever a player connects. The prefab has a camera attached to it. My understanding is that I’ll just reference that camera, then disable all of those that are not “isMine.” Am I on the right track?

Yup sounds right, I think that should do it. I am not a great multiplier programmer so if it doesn’t work, sorry. Best of luck to you.

Thanks! I appreciate the help!

I still seem to be doing something very wrong. The camera situation is still as described above. However, I forgot to note that you control the other player’s character and the other player controls your character. Here’s the code I’m working with. I’ve attached a camera to the player prefab. I stored that camera in the cam variable. I’m using the FPSInputController for movement.

Network Manager Script attached to an empty game object:

var playerPrefab:GameObject; //Prefab for player character
var spawnObject:Transform; //spawnPoint
var cam:GameObject; //camera so that I can destroy cameras that aren't mine.
var gameName:String = "CGCookie_Tutorial_Networking"; //Unique Game Name

private var refreshing:boolean; //used for refresh host list delay to allow server list to populate
private var hostData:HostData[]; //host data contains list of host info (number of players, game name, etc.)

private var btnX:float; //Size
private var btnY:float; //Size
private var btnWidth:float; //Size
private var btnHeight:float; //Size

function Start(){
    btnX = Screen.width * 0.05;
    btnY = Screen.width * 0.05;
    btnWidth = Screen.width * 0.1;
    btnHeight = Screen.width * 0.1;

}


function startServer(){
    Network.InitializeServer(32, 25001, !Network.HavePublicAddress);
    MasterServer.RegisterHost(gameName, "Tutorial Game Name", "This is a tutorial game!"); //Tutorial Game Name should be a user input field where users can name their individual games (e.g., 24/7 Deathmatch!)
}


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


function Update(){
    if(refreshing){
        if (MasterServer.PollHostList().Length > 0){
            refreshing = false;
            Debug.Log(MasterServer.PollHostList().Length); //Returns HostData array which returns all games of the hostlist type
            hostData = MasterServer.PollHostList();
        }
    }
}


function spawnPlayer(){
    Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0); //group 0 allows for everybody assigned zero to have special rules assigned to them (e.g. team chat)
}


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


function OnConnectedToServer(){
    spawnPlayer();
}


function OnNetworkInstantiate(info:NetworkMessageInfo){
    if (!networkView.isMine){  // if this is not my player, remove the camera
        Destroy(cam);
    }
}


function OnMasterServerEvent(mse:MasterServerEvent){ //Unity allows all to use their master server
    if(mse == MasterServerEvent.RegistrationSucceeded){ //If registered to master server
        Debug.Log("Registered Server!");
    }
}


//GUI
function OnGUI(){
    if (!Network.isClient && !Network.isServer){ //Only shows server buttons if not already a client or a server
        if (GUI.Button(Rect(btnX, btnY, btnWidth, btnHeight), "Start Server")){ //Creates start server button
            Debug.Log("Starting Server");
            startServer();
        }
        if (GUI.Button(Rect(btnX, btnY * 1.2 + btnHeight, btnWidth, btnHeight), "Refresh Hosts")){ //Creates refresh button
            Debug.Log("Refreshing");
            refreshHostList();
        }
       
        if (hostData){ //if hostData exists
            for(var i:int = 0; i < hostData.length; i++){ //For every server found
                if(GUI.Button(Rect(btnX * 1.5 + btnWidth, btnY * 1.2 + (btnHeight * i), btnWidth * 3.0, btnHeight * 0.5), hostData[i].gameName)){ //Create a button for each server found
                    Network.Connect(hostData[i]);
                }
            }
        }
    } //Close brackets for isClient / isServer
}

Script attached to player:

var cam:GameObject;



function OnNetworkInstantiate(info:NetworkMessageInfo){
    if (!networkView.isMine){  // if this is not my player, remove the camera
        Destroy(cam);
    }
}