Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sheepers · Jan 29, 2017 at 12:39 PM · unity 5camera3dmultiplayerswitch cameras

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jan 29, 2017 at 08:06 PM 0
Share

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.

avatar image Sheepers Cherno · Jan 29, 2017 at 09:21 PM 0
Share

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.

avatar image Cherno Sheepers · Jan 29, 2017 at 09:45 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
 

 

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sheepers · Jan 30, 2017 at 11:22 PM 0
Share

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?

avatar image jdean300 Sheepers · Jan 30, 2017 at 11:45 PM 0
Share

In Start(), check whether or not the current player is the local player. If it is, destroy the script component.

avatar image Sheepers jdean300 · Jan 31, 2017 at 01:32 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera only showing 'touching' objects 2 Answers

Custom frustum culling with scissoring 0 Answers

Szise is wrong in build 2 Answers

Rotate camera only in 2 directions based on player touch 1 Answer

player rotate to camera direction but not moving in it's direction 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges