Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Third Party Best way to keep a weapon drop consistent throughout the room? (Photon Multiplayer)

Discussion in 'Multiplayer' started by Benjaben22, Nov 6, 2014.

  1. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    73
    I'm having an issue right now in my game where sometimes when one player picks up a gun off the ground, it's not removing the gun off the ground allowing the player to essentially duplicate the item.

    In my WeaponPickup script I have the following:

    Code (CSharp):
    1. void Update()
    2. {
    3.   if(weaponToPickup)
    4.   {
    5.      SwitchWeapon(weaponToPickup);
    6.      weaponToPickup.GetPhotonView().RPC("Remove", PhotonTargets.All); //removes the gun on the ground
    7.      GameObject dropWeapon = PhotonNetwork.Instantiate(currentWeapon, transform.position, transform.rotation, 0); //throw your current weapon down
    8.      dropWeapon.name = currentWeapon.name;
    9.   }
    10. }
    And then the RPC is:

    Code (CSharp):
    1.  
    2. [RPC]
    3. void Remove()
    4. {
    5.      if(PhotonNetwork.isMasterClient)
    6.      {
    7.           PhotonNetwork.Destroy(gameObject);
    8.      }
    9. }
    10.  
    Is this a terrible way to do this? It seems to work for the most part, but after a while once players start picking up each other's weapons and dropping them it gets a bit confusing and starts doing the whole duplicating thing.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    It's not a terrible way but as you see it just doesn't work reliably either.
    It might not be obvious but RPC "Remove" gets executed immediately on the client that sends it to all. And: It picks up the weapon, no matter if anyone else is picking the weapon up at the same time (which means the other's "Remove" will arrive any moment but didn't yet).

    You better send "Pickup" to AllViaServer. Then you can remove the item from the ground and pick it up on the client who sent the "Pickup" first (check "did i send it? if so, pickup" before you remove the item).

    You can see an implementation of it in the PUN package's "Pickup, Teams & Score Demo". Update PUN if you don't have it.
     
  3. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    73
    Thanks for your help! That does make a lot more sense.
     
  4. aanimation

    aanimation

    Joined:
    Oct 23, 2012
    Posts:
    49
    whatched this