- Home /
How do you give each player a separate camera?
How do you give each player a separate camera? I am making a 3d multiplayer game, and I want each player to have a separate camera. Whenever a player joins, I create a new camera. The problem is, while there are two cameras in the scene, one for each player, both players are looking out of the same location, and only one player can control the location for both players.
I know that whatever camera is tagged "MainCamera" is the one that the player uses. Because of that, I am trying to set each player's camera to the main camera every update, so that they will use the correct one, but that isn't working. This is the code for that:
GameObject[] cameras = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject c in cameras) {
if (c.tag.Contains ("Camera")) {
if (c.GetComponent<PlayerScript> ().team != team) {
c.tag = "Camera";
} else {
c.tag = "MainCamera";
}
}
}
I also tried doing this:
GameObject[] players = GameObject.FindGameObjectsWithTag("MainCamera");
foreach(GameObject i in players) {
i.GetComponent<Camera>().enabled = false;
}
gameObject.GetComponent<Camera>().enabled = true;
(The script for this is attached to the camera, and the script has a team variable to tell which player it is supposed to be.)
Neither of those worked, and I am not sure how else to make it so that the players use the correct camera.
My players are spawned from the LobbyManager game object from the unity networking, and the player is a camera with a movement script attached, and the above code, as well as other un-related stuff.
A new camera is created correctly when a player joins the game (I can see it from the inspector in the editor). The players should both be able to use the arrow keys to move their camera around, but only one of the players can move the camera, and both players' are seeing from the same spot (the second player's view will move around with the first), so it seems that they are both looking out of the same camera, and the second one is unused.
I would like both players to have their own cameras, and be able to move their cameras around.
I am using the built-in unity networking libraries, and unity 5.4.1
Just forget about the $$anonymous$$ain Camera tagging stuff. It just complicates things.
I don't understand the problem you have at all. If a player joins, create a camera at the required position, make it a child if needed, and be done with it. Or, just make the camera a part of the player prefab so it automatically gets instantiated with it. Set the Viewport so it first the right screen positions and you should be fine.
I am already creating a new camera whenever a player joins. The problem is, the player still ends up using another player's camera ins$$anonymous$$d of the one I made for them. What do you mean by "set the Viewport"? I don't really know what that is.
How is it possible that one player uses another's camera? A camera has settings for the viewport to control where the viewport is located on the screen, and it's dimensions.
Answer by OneProGoober · Jan 30, 2017 at 07:41 AM
Hey, I've just got a suggestion. There's are a few youtube tutorials I'd highly reccomend concerning this issue...
Try looking at these tutorials, I'd highly recommend learning something from this guy: https://www.youtube.com/watch?v=AIgwZK151-A&list=PLbghT7MmckI7BDIGqNl_TgizCpJiXy0n9
Or this: https://www.youtube.com/playlist?list=PLPV2KyIb3jR5PhGqsO7G4PsbEC_Al-kPZ
I personally like the idea of using Photon (also Quill18 is excellent!), but if that's not your thing then best of luck ;)
Here's a snippet of my code so you can see what I do for cameras - might provide some help, probably not though:
//Disable the overheadCamera (by the way, this is just my Main Scene// //Cam)
overheadCam = GameObject.FindGameObjectWithTag("Cam");
overheadCam.SetActive(false);
//Instantiate the new camera and player (shouldn't matter if you //use photon or not.
//just be sure to enable to camera and the character controller and have //them disabled by default).
player = PhotonNetwork.Instantiate(player.name, spawnPoint.transform.position, Quaternion.identity, 0);
playerCam = PhotonNetwork.Instantiate("PlayerCam", spawnPoint.transform.position, Quaternion.identity, 0);
playerCam.GetComponent<Camera>().enabled = true;
playerCam.GetComponent<CameraControllercs>().enabled = true;
playerCam.GetComponent<CameraControllercs>().target = player;
I looked at the tutorial you linked to, and it said that I should only have my scripts on the local player. The problem is, because the players are networked, if I add my scripts to the local player, then they are on my player on all of the clients.Do you know how I can stop this from happening?
In Start(), check whether or not the current player is the local player. If it is, destroy the script component.
But if I have two players, they will both destroy the other player's script, and then neither of them will have a script. I need some way so it only happens locally.