Search Unity

Multiplayer spawn problem.

Discussion in 'Multiplayer' started by EdgarCarrera, Aug 31, 2014.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    in one client ( First client i can see two player and if i move 1 all players move. and second client only show one player. i think network not work?
    My script: Login and Spawn. ( i have character creation area. and work good. if u have character u go automatically to spawn. ) but how i can fix this?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Networking : MonoBehaviour
    6. {
    7.     //Formularios
    8.     public InputField username;
    9.     public InputField password;
    10.     public InputField regUsername;
    11.     public InputField regPassword;
    12.     public InputField regRepeatPassword;
    13.     public InputField email;
    14.     public InputField numberInput;
    15.     public Text message;
    16.     public Text generatedNumber;
    17.     private int randomNumber;
    18.  
    19.     //Menu Actual
    20.     private string _CurrentMenu = "MenuLogin";
    21.  
    22.     //Desactivo GameObjects
    23.     public GameObject menuLogin;
    24.     public GameObject menuRegister;
    25.  
    26.     //Server Status AREA!
    27.     public Text serverStatus;
    28.     public GameObject menuLoginButton;
    29.  
    30.     //Chequea si tiene personaje
    31.     private int existCharacter;
    32.    
    33.     public static int UserID;
    34.  
    35.     void Start()
    36.     {
    37.         currentMenu ();
    38.         message.text = "";
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         ServerCheck ();
    44.         currentMenu ();
    45.     }
    46.  
    47.     private void currentMenu()
    48.     {
    49.         if (_CurrentMenu == "MenuLogin")
    50.         {
    51.             menuLogin.gameObject.SetActive(true); // Activo Script menuLogin
    52.             menuRegister.gameObject.SetActive(false); // Desactivo Script menuRegister
    53.  
    54.             ServerCheck();
    55.         }
    56.  
    57.         if (_CurrentMenu == "MenuRegister")
    58.         {
    59.             menuLogin.gameObject.SetActive(false); // Desactivo Script menuLogin
    60.             menuRegister.gameObject.SetActive(true); // Activo Script menuRegister
    61.        
    62.             generatedNumber.text = randomNumber.ToString();
    63.  
    64.         }
    65.     }
    66.  
    67.     public void loadLogin ()
    68.     {
    69.         _CurrentMenu = "MenuLogin";
    70.         message.text = "";
    71.         currentMenu ();
    72.     }
    73.  
    74.     public void loadRegister ()
    75.     {
    76.         _CurrentMenu = "MenuRegister";
    77.         randomNumber = Random.Range(1000,99999);
    78.         regUsername.value = "";
    79.         regPassword.value = "";
    80.         regRepeatPassword.value = "";
    81.         email.value = "";
    82.         numberInput.value = "";
    83.         message.text = "";
    84.         currentMenu ();
    85.     }
    86.  
    87.     public void regCheck()
    88.     {
    89.         message.text = "";  
    90.         if(regUsername.value == "" || email.value == "" || regPassword.value == "" || numberInput.value == "0"){
    91.             message.text += "Please fill in the empty fields. \n";
    92.         }else{
    93.             if(regPassword.value == regRepeatPassword.value){
    94.                 if(numberInput.value == generatedNumber.text){
    95.                     StartCoroutine("doRegister");
    96.                 }else{
    97.                     message.text += "The number you inputed is not the same number.";
    98.                 }
    99.                
    100.             }else{
    101.                 message.text += "The passwords do not match. \n";
    102.             }
    103.         }
    104.     }
    105.  
    106.     public void logCheck()
    107.     {
    108.         if(username.value == "" || password.value == ""){
    109.             message.text += "Please enter the necessary data! \n";
    110.         } else{
    111.             message.text = "";
    112.             StartCoroutine("doLogin");
    113.         }
    114.     }
    115.  
    116.     private void ServerCheck()
    117.     {
    118.         StartCoroutine (ServerRefresh ());
    119.                
    120.         if(MasterServer.PollHostList().Length != 0)
    121.         {
    122.             HostData[] data = MasterServer.PollHostList();
    123.             foreach(HostData c in data)
    124.             {
    125.                 serverStatus.text = c.gameName + " " + (c.connectedPlayers - 1) + "/" + c.playerLimit;
    126.                 menuLoginButton.gameObject.SetActive(true); // Activo Script menuLoginButton
    127.             }
    128.         }
    129.         else
    130.         {
    131.             serverStatus.text = "Server Offline";
    132.             menuLoginButton.gameObject.SetActive(false); // Desactivo Script menuLoginButton
    133.         }
    134.     }
    135.  
    136.     public void OnConnectedToServer()
    137.     {
    138.         networkView.RPC("LoginRPC",RPCMode.All, username.value, Network.player);
    139.  
    140.         LevelManager.Load ("LoadingData", 0f);
    141.     }
    142.    
    143.     private void OnDisconnectedFromServer()
    144.     {
    145.         _CurrentMenu = "MenuLogin";
    146.     }
    147.    
    148.     private IEnumerator doRegister()
    149.     {
    150.         WWWForm form = new WWWForm();
    151.         form.AddField("Username" , regUsername.value);
    152.         form.AddField("Password" , regPassword.value);
    153.         form.AddField("Email" , email.value);          
    154.         var w = new WWW(Info.webSite + "/Register.php", form);          
    155.         yield return w;
    156.         if(w.error == null){
    157.             message.text += w.text;
    158.  
    159.             yield return new WaitForSeconds(2);
    160.  
    161.             _CurrentMenu = "MenuLogin";
    162.  
    163.             StopCoroutine("doRegister");
    164.         }else{
    165.             message.text += "Error : " + w.error + "\n";
    166.         }
    167.     }
    168.     private IEnumerator doLogin()
    169.     {
    170.         WWWForm logform = new WWWForm();
    171.         logform.AddField("Username" , username.value);
    172.         logform.AddField("Password" , password.value);
    173.         var logw = new WWW(Info.webSite + "/Login.php", logform);
    174.         yield return logw;
    175.         if(logw.error == null){
    176.             message.text += logw.text;
    177.            
    178.             StopCoroutine("doLogin");
    179.         }else{
    180.             message.text +="Error : " + logw.error + "\n";  
    181.         }
    182.         if(message.text == "Login success! Please wait while the game loads..."){
    183.             Network.Connect("avct.sytes.net", 8632);
    184.         }
    185.     }
    186.    
    187.     private IEnumerator ServerRefresh()
    188.     {
    189.         yield return new WaitForSeconds(3);
    190.         MasterServer.RequestHostList("MMOFPS");
    191.     }
    192.  
    193.     [RPC]
    194.     public void LoginRPC(string username,NetworkPlayer player)
    195.     {
    196.         Info.Owner = username;
    197.     }
    198. }

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameStarted : MonoBehaviour {
    5.  
    6.     private const string MALE_MODEL_PATH = "Characters/Male/";
    7.     private const string FEMALE_MODEL_PATH = "Characters/Female/";
    8.     public string[] maleModels;
    9.     public string[] femaleModels;
    10.  
    11.     private int _index = 0;
    12.     public GameObject playerPref;
    13.  
    14.  
    15.     void OnServerInitialized()
    16.     {
    17.         //SpawnPlayer();
    18.     }
    19.    
    20.     void OnConnectedToServer()
    21.     {
    22.         //SpawnPlayer();
    23.     }
    24.     // Use this for initialization
    25.     void Start () {
    26.  
    27.         if(Info.charGender == 0)
    28.         {
    29.             playerPref = Network.Instantiate (Resources.Load (MALE_MODEL_PATH + maleModels[ _index ]), new Vector3(Info.charPosX, Info.charPosY, Info.charPosZ), transform.rotation, 0) as GameObject;
    30.         }
    31.         else if(Info.charGender == 1)
    32.         {
    33.             playerPref = Network.Instantiate (Resources.Load (FEMALE_MODEL_PATH + femaleModels[ _index ]), new Vector3(Info.charPosX, Info.charPosY, Info.charPosZ), transform.rotation, 0) as GameObject;
    34.         }
    35.  
    36.  
    37.     }
    38. }

     
  2. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    I don't think the script you have posted is the reason for any of the observed behaviour.

    Your problem with player movement is probably caused by the script that moves the character. You need to have some way of determining which player is the "local" player and can be controlled and then check for that in the movement script.

    The other problem (the first player not being visible for the second player) is probably caused by the way you instantiate the player. You will have to send an RPC to the second player in order to instantiate the first one. Network.Instantiate does this automatically but I assume that you change levels after connecting which will delete any instantiated objects. This means that you will have to manually instantiate the object again, in the correct scene.
     
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    can u show me a example? for show others players?
     
  4. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    bump
     
  5. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  6. Stormouse

    Stormouse

    Joined:
    Sep 11, 2014
    Posts:
    8
    Try adding Network.isMessageQueueRunning = true; after loading the map
    Remember to remove/disable the controllers(influenced by user input) on your char
    Hope helps.
     
  7. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    can u make a demo please