Search Unity

Camera problem when client joins

Discussion in 'Multiplayer' started by magicyannik, Apr 28, 2016.

  1. magicyannik

    magicyannik

    Joined:
    Apr 28, 2016
    Posts:
    2
    Hi,

    I was trying to code a simple multiplayer for my game. I got stuck when setting the correct camera for a joined player. When the client player joins my host player the cameras get switched. Camera A for player A is now snapped to player B and vice versa. Here is my code for the player after the prefab:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerSetup : NetworkBehaviour {
    6.  
    7.     int counter;
    8.     Camera sceneCamera;
    9.     private GUIStyle winStyle = new GUIStyle();
    10.     public Camera cameraUp;
    11.     public Camera cameraDown;
    12.     void Start ()
    13.     {
    14.         if (!isLocalPlayer)
    15.         {
    16.             this.GetComponent<SimpleCarController>().enabled = false;
    17.             return;
    18.         }
    19.         else
    20.         {
    21.             Camera.main.enabled = false;
    22.             cameraUp.enabled = true;
    23.             Debug.Log("Camera switch");
    24.             winStyle.fontSize = 80;
    25.             winStyle.alignment = TextAnchor.UpperCenter;
    26.             winStyle.normal.textColor = Color.white;
    27.  
    28.             RegisterPlayer();
    29.             OnGUI();
    30.         }
    31.  
    32.      }
    33.    
    34.     void RegisterPlayer()
    35.     {
    36.         string _ID = "Player " + GetComponent<NetworkIdentity>().netId;
    37.         cameraUp.name = "camUp" + _ID;
    38.         cameraDown.name = "camDown" + _ID;
    39.         transform.name = _ID;
    40.         Debug.Log("Register was here");
    41.     }
    42.  
    43.     void FixUpdate()
    44.     {
    45.         OnGUI();
    46.     }
    47.  
    48.     public void OnGUI()
    49.     {
    50.         counter = count.currentCounthub;
    51.         GUI.Label(new Rect(20f, 20f, 100f, 30f), "Count: " + counter.ToString());
    52.  
    53.         //win-lose
    54.         if (counter == 3)
    55.         {
    56.             GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 0f, 30f), "You Win!", winStyle);
    57.         }
    58.  
    59.     }
    60. }
    61.  
    Can you please help me to figure out what went wrong?
     
  2. Chom1czek

    Chom1czek

    Joined:
    Sep 19, 2015
    Posts:
    66
    To be honest I'm quite confused what is happening but anyway if you are using Unet try to use multiplayer methods like OnStartClient/OnStartLocalPlayer and of course Commands and ClientRpc,

    From what I understood and what I would try to do is:

    - SimpleCarController disable it on playerPrefab and make it enabled OnStartLocalPlayer (within the simpleCarController or whatever class it manages it)
    - Enable camera in OnStartLocalPlayer as well and make the RegisterPlayer a [Command]

    PS I think you don't have to call OnGUI method anywhere (espiecally on any update event) because it will be cast on it's own so remove it's call on fixedUpdate. If my advices won't work please explain what are you trying to accomplish so I can understand it please :) Cheers and good luck.