Search Unity

Random Item box for multiplayer Runner

Discussion in 'Multiplayer' started by Acarnan, Aug 10, 2018.

  1. Acarnan

    Acarnan

    Joined:
    Aug 10, 2018
    Posts:
    3
    Hi everybody,

    I am writing to you today because I am facing a problem. I created a racing game where users can pick up weapons with ItemBox. The box is an object, with a collider (trigger). When the user (client or host) encounters the box, this triggers a [command] in the player script (the player is instantiated by the network manager and each weapon is referenced in the network manager as spawnable), the command generates a Random number between which allows the user to retrieve a weapon in a [] gameobject list.
    When the player presses the fire button, a [command] is launched and the object is instantiated via NetworkServer.SpawnWithClientAuthority

    The problem is that the object passed in reference by the player, is not necessarily the one instantiated by the server. I have an image that allows me to know which object I will use, which is displayed only for the player, and this image is not always the right one, by that I mean the one that is attached to the weapon's gameobject . Everything works correctly on the host, however.

    Would anyone have an idea? Or would someone have an example of itembox for a multiplayer game?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    To get help with your code you'll need to post the code.
     
  3. Acarnan

    Acarnan

    Joined:
    Aug 10, 2018
    Posts:
    3
    Hello Sorry for time to respond :

    So threre is code when player hit an itembox :
    Code (CSharp):
    1.  
    2. if (col.gameObject.tag == "Weapon")
    3.         {
    4.             if (col.GetComponent<BoxWeapon>().unabledtimer == 0)
    5.             {
    6.  
    7.                 weapon = listWeapon[weaponNo];
    8.                 try
    9.                 {
    10.                     if (weapon.GetComponent<Weapon>().weaponImage != null)
    11.                     {
    12.  
    13.                         weaponImage.sprite = weapon.GetComponent<Weapon>().weaponImage;
    14.                     }
    15.                     else
    16.                     {
    17.                         weaponImage.sprite = weapon.GetComponentInChildren<Weapon>().weaponImage;
    18.                     }
    19.  
    20.                     GetObject();
    21.                 }
    22.                 catch (Exception e)
    23.                 {
    24.                     Debug.Log(e.StackTrace);
    25.                 }
    26.                 //Désactivation de la box
    27.                 col.GetComponent<BoxWeapon>().UnabledWeapon();
    28.             }
    29.         }
    30.  
    31.     public void GetObject()
    32.     {
    33.         //Récup de l'arme
    34.         weaponNo = (int)UnityEngine.Random.Range(0, 4);
    35.         canIShoot = true;
    36.     }
    37.  
    There the code when you shoot :

    Code (CSharp):
    1. [Command]
    2.     void CmdFire()
    3.     {
    4.         youCanShoot(false);
    5.         weapon = listWeapon[weaponNo];
    6.         if (weapon.GetComponent<MineScript>() != null)
    7.         {
    8.             GameObject myWeapon = Instantiate(weapon, new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z), Quaternion.identity);
    9.             NetworkServer.SpawnWithClientAuthority(myWeapon, gameObject);
    10.             //Destroy(myWeapon, 20);
    11.  
    12.         }
    13.         else if (weapon.GetComponent<CatScript>() != null)
    14.  
    15.         {
    16.             if (weapon.GetComponent<CatScript>().destination != null)
    17.             {
    18.                 GameObject myWeapon = Instantiate(weapon, new Vector3(transform.position.x + 0.7f, transform.position.y, transform.position.z), Quaternion.identity);
    19.                 NetworkServer.SpawnWithClientAuthority(myWeapon, gameObject);
    20.             }
    21.             else
    22.             {
    23.                 GameObject myWeapon = Instantiate(weapon, new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z), Quaternion.identity);
    24.                 NetworkServer.SpawnWithClientAuthority(myWeapon, gameObject);
    25.             }
    26.             //Destroy(myWeapon, 5);
    27.  
    28.         }
    29.         else
    30.         {
    31.  
    32.             GameObject myWeapon = Instantiate(weapon, new Vector3(transform.position.x + 0.7f, transform.position.y, transform.position.z), Quaternion.identity);
    33.             NetworkServer.SpawnWithClientAuthority(myWeapon, gameObject);
    34.             //Destroy(myWeapon, 5);
    35.  
    36.         }
    37.     }
    The problem is the Picture associate to the weapon i get with the GetObject Methode (just a INTEGER it give the position of weapon in ListOfWeapon) is not the right picture of the weapon firing by the command method...

    Thanks for help.