Search Unity

Unity Photon cameras switching?

Discussion in 'Scripting' started by Clicksurfer, Aug 17, 2014.

  1. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    Hey. I am kind of new to Unity in general, yet I've decided to jump into the water and to try and develop a Multiplayer game. It's actually been going pretty well, however I have encountered a problem I just can't get around. The player spawns with a camera slightly above and behind them (So a 3rd person view), and during single player everything works fine. However, when another player joins the server, their cameras are swapped (So player 1 can still control his character, but he sees everything through player 2's camera). How do I get them to switch? I've made all the code using C#.
    I know this might sound like a dumb question to most of you, but I would really appreciate it if you helped me out.
    This is my NetworkManager code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetworkManager : Photon.MonoBehaviour {
    5.  
    6.     public int PlayCount =0;
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         PhotonNetwork.ConnectUsingSettings ("alpha 0.1");//Connects using version name, so it won't let it enter if it's an older version.
    11.     }
    12.  
    13.  
    14.  
    15.     void OnGUI() {
    16.         GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    17.         }
    18.    
    19.     void OnJoinedLobby() {
    20.                 PhotonNetwork.JoinRandomRoom ();
    21.         }
    22.  
    23.     void OnPhotonRandomJoinFailed() {
    24.         PhotonNetwork.CreateRoom("RoomName");
    25.     }
    26.  
    27.     void OnJoinedRoom() {
    28.         Vector3 Pos = new Vector3 ((float)150.135, (float)6.009, (float)134.748);
    29.         GameObject MyPlayer = PhotonNetwork.Instantiate ("Player", Pos, Quaternion.identity, 0);
    30.         PlayCount = PlayCount + 1;
    31.         if (PlayCount > 1) {
    32.             //This is where I intend to put the 'Camera swapping' effect.
    33.                 }
    34.  
    35.  
    36.         PlayerMov MyPlayerScript = MyPlayer.GetComponent<PlayerMov> ();
    37.         MyPlayerScript.IsPlayer1 = true;
    38.  
    39.  
    40.     }
    41. }
    42.  
     
  2. Joxno

    Joxno

    Joined:
    Aug 20, 2013
    Posts:
    6
    This is due to your prefab having a Camera, so when the 2nd player joins it'll spawn a 2nd camera. The easiest way of going about this is to remove the camera from the prefab, and after your "PhotonNetwork.Instantiate" you locally create your own camera for the client.

    So only 1 camera exists on the client instead of N Cameras per Joined Player.
     
    Clicksurfer likes this.
  3. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    Hey, Joxno. First of all, thank you very much for replying to me. I attempted to do as you said, and it didn't quite work well for me. Then again, it was probably 99% me just screwing up your advice. You were right about the fact that my prefab (Specifically here, my player) had a camera child object located right behind him to give the 3rd person effect. I was, however, unsuccessful when it came to creating a camera locally after the player spawned. I have the coordinates and everything for it, I was just wondering if you could give me a pointer, perhaps, on how to create the camera locally.
    Once again, thank you for taking the time to reply to me.
     
  4. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    Hey, sorry about before. After a little bit of trial and error, and some searching, I managed to do what you suggested. It works fantastically. Thank you very much for all your help.
    For those who are wondering, this is my final script concerning the NetworkManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetworkManager : Photon.MonoBehaviour {
    5.  
    6.     public int PlayCount =0;
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         PhotonNetwork.ConnectUsingSettings ("alpha 0.1");//Connects using version name, so it won't let it enter if it's an older version.
    11.     }
    12.  
    13.  
    14.  
    15.     void OnGUI() {
    16.         GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    17.         }
    18.    
    19.     void OnJoinedLobby() {
    20.                 PhotonNetwork.JoinRandomRoom ();
    21.         }
    22.  
    23.     void OnPhotonRandomJoinFailed() {
    24.         PhotonNetwork.CreateRoom("RoomName");
    25.     }
    26.  
    27.     void OnJoinedRoom() {
    28.         Vector3 Pos = new Vector3 ((float)157.8945, (float)6.370018, (float)136.5505);
    29.         //Vector3 CamPos = new Vector3 ((float)157.8945, (float)10.62301, (float)131.8129);
    30.         GameObject MyPlayer = PhotonNetwork.Instantiate ("Player", Pos, Quaternion.identity, 0);
    31.         //GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
    32.         GameObject camera = GameObject.FindWithTag ("MainCamera");
    33.         if (camera != null)
    34.         {
    35.             CameraController followScript = camera.GetComponent("CameraController") as CameraController;
    36.             if (followScript != null){
    37.                 followScript.target = MyPlayer;
    38.             }
    39.         }
    40.         PlayCount = PlayCount + 1;
    41.         if (PlayCount > 1) {
    42.             //This is where I intend to put the 'Camera swapping' effect.
    43.                 }
    44.  
    45.  
    46.         PlayerMov MyPlayerScript = MyPlayer.GetComponent<PlayerMov> ();
    47.         MyPlayerScript.IsPlayer1 = true;
    48.  
    49.  
    50.     }
    51. }
    52.  
    The player is spawned, while the camera is actually the MainCamera which is already in the scene, and it is used locally to show the player around. The following is a small script called CameraController which is attached to the Main Camera (So it basically sets itself to the Player's face (Turning it first-person) and follows him, and also turns with him):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.    
    6.     public GameObject target;
    7.     private Vector3 offset;
    8.     private Vector3 HeightPos = new Vector3 ((float)0,(float)3,(float)0);
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         offset = transform.position;
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void LateUpdate () {
    17.         if (target != null){
    18.             transform.position = target.transform.position + offset+HeightPos;
    19.             transform.rotation = target.transform.rotation;
    20.         }
    21.     }
    22. }

    Anyway, thanks to all those who helped me, and happy coding!
    -Clicksurfer
     
    MadAssau1t likes this.
  5. toffino

    toffino

    Joined:
    Oct 23, 2014
    Posts:
    1
    I draged and droped my characterprefab in the target field... But the camera doesn't follow my player.
    I use your code for a 2d game... So I supposed that x and y axis should follow my player in those lines from CameraController:
    1. void LateUpdate () {
    2. if (target != null){
    3. transform.position = target.transform.position + offset+HeightPos;
    4. transform.rotation = target.transform.rotation;
    But, when I press playtest, and I drag the characterprefab(clone) in the target field it's working.. How can I tell to my CameraController that it should target the clone correctly ?

    ALLLLLLLL RIGHT!
    I just forgot to tag my MainCamera into MainCamera in the inspector menu top left side.
     
    Last edited: Oct 24, 2014
  6. nicopucho

    nicopucho

    Joined:
    May 3, 2017
    Posts:
    9
    Thank you for posting the solution!! <3