Search Unity

Multiple Cameras

Discussion in 'Multiplayer' started by drok0920, Jul 19, 2015.

  1. drok0920

    drok0920

    Joined:
    May 7, 2015
    Posts:
    5
    I am making an online multipler game and i cant seem to get the cameras to work. I have a camera attached to my player object and when the first player join it works, but when the second player joins the first player uses the camera attached to the second player. How can i fix this?
     
  2. LegoboomeyIf

    LegoboomeyIf

    Joined:
    Dec 21, 2012
    Posts:
    3
    I'm also having this problem. I have seached on the forums a bit and found this code:
    Code (JavaScript):
    1. function update(){
    2. if (!gameObject.transform.parent.gameObject.NetworkView.isMine) {
    3.             gameObject.active = false;
    4.         }
    5.        }
    It doesn't work for me but you could try it out. The forum poster also says you need to have Network view attached to the camera. If it works for you please tell me how you did it :b
     
    drok0920 likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    You have to turn off cameras that aren't "yours". Or, vice-versa, you can start with the camera off, then turn it on.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerScript: NetworkBehaviour {
    7.  
    8.     public Camera cam; // Drag camera into here
    9.  
    10.     void Start()
    11.     {
    12.         // IF I'M THE PLAYER, STOP HERE (DON'T TURN MY OWN CAMERA OFF)
    13.         if (isLocalPlayer) return;
    14.  
    15.         // DISABLE CAMERA AND CONTROLS HERE (BECAUSE THEY ARE NOT ME)
    16.         cam.enabled = false;
    17.         //GetComponent<PlayerControls>().enabled = false;
    18.         //GetComponent<PlayerMovement>().enabled = false;
    19.     }
    20.  
    21. }
    @LegoboomeyIf That's the pre-Unity 5.1 networking system.
     
  4. drok0920

    drok0920

    Joined:
    May 7, 2015
    Posts:
    5
    Thank you worked great.
     
    DoomLord115 likes this.
  5. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    As Stardog said that's for the old Raknet networking system, and that's a bad piece of code, executing it every frame inside update. Posts with the "Unity Multiplayer" prefix are using the new UNET networking.
     
  6. Viswanath Nuggu

    Viswanath Nuggu

    Joined:
    May 24, 2015
    Posts:
    2
    @Stardog, I tried this but the way it is behaving is camera is getting activated only on to newly created player.
    Example: I have 3 clients in my multiplayer game, if first client game is in one window when this is created it is working and when second player is joining the game second client camera is working but when i switch back to client one camera is not following client one, simillarly when client three is joining the game client three camera is activated and it is following him but when i switch back to client1,2 camera is not following them.

    I am using Unity5.1.2f1 and i am trying to implement multiplayer game with default ThirdPersonController in unity.

    I have attached the camera to the ThirdPerson prefab and placed the above code in the start() of "ThirdPersonUserControl.cs"

    could you please help me in resolving this.
     
    Last edited: Sep 11, 2015
  7. Agent-6141

    Agent-6141

    Joined:
    Mar 17, 2013
    Posts:
    3

    Did you ever figure this out?
    I'm doing the same thing right now. The first player works, but the second player the camera does not activate for itself. here is my code. I feel like the second player is not seeing itself as local Authority.

    The second player doesn't activate anything. Just keeps falling because the encapsulator does not get activated.

    Code (CSharp):
    1. using UnityEngine.Networking;
    2. using UnityStandardAssets.Characters.ThirdPerson;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class LocalPlayerControl : NetworkBehaviour {
    7.  
    8.     public Camera cam;
    9.  
    10.      void Start()
    11.     {
    12.  
    13.  
    14.         if (isLocalPlayer)
    15.  
    16.             return;
    17.  
    18.             GetComponent<ThirdPersonUserControl>().enabled = false;
    19.             GetComponent<ThirdPersonCharacter>().enabled = false;
    20.             GetComponent<Animator>().enabled = false;
    21.             GetComponent<CapsuleCollider>().enabled = false;
    22.             GetComponent<AudioSource>().enabled = false;
    23.             GetComponent<NetworkAnimator>().enabled = false;
    24.             GetComponent<NetworkTransform>().enabled = false;
    25.             cam.GetComponent<CameraControl>().enabled = false;
    26.             cam.enabled = false;
    27.     }
    28.  
    29.  
    30. }
    31.  
     
  8. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Here's some code for another project:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetPlayerSetup : NetworkBehaviour {
    6.  
    7.    [Header("References")]
    8.    public Camera _cam;
    9.  
    10.    void Start()
    11.    {
    12.        if (isLocalPlayer)
    13.        {
    14.            Debug.Log(GetType().Name + " - Start() isLocalPlayer - " + gameObject.name);
    15.        }
    16.        else
    17.        {
    18.            // NETWORK: DISABLE COMPONENTS
    19.            if (_cam != null) _cam.gameObject.SetActive(false);
    20.  
    21.            Debug.Log(GetType().Name + " - Start() !isLocalPlayer - Disabled components - " + gameObject.name);
    22.        }
    23.    }
    24.  
    25.    public override void OnStartLocalPlayer()
    26.    {
    27.        if (isLocalPlayer)
    28.        {
    29.            Debug.Log(GetType().Name + " - OnStartLocalPlayer() isLocalPlayer - " + gameObject.name);
    30.        }
    31.        else
    32.        {
    33.            Debug.Log(GetType().Name + " - OnStartLocalPlayer() !isLocalPlayer - " + gameObject.name);
    34.        }
    35.    }
    36.  
    This was for an FPS. Each player would be a prefab with a child camera, enabled by default. You have to click-drag it as a reference.

    Are you spawning the 2nd player correctly, or they are both prefabs in the scene? You might need to spawn them properly.

    Maybe Unity changed something in the networking code since I did this code...
     
  9. KillerHawk

    KillerHawk

    Joined:
    May 21, 2017
    Posts:
    1
    Hello, i was having bugs with my scripts and this thread really helped me so thanks! In the process of finding the anwser here i did find out that you cannot use the isLocalPlayer method in the awake method. Im not sure if this is a bug or not but i wanted to post so others would know.