Search Unity

Third Party How to make pickable items with PUN 2?

Discussion in 'Multiplayer' started by Vefery, Oct 27, 2020.

  1. Vefery

    Vefery

    Joined:
    Feb 23, 2018
    Posts:
    119
    I need to make pick/drop item system for my coop game, items should remain as they are (so the object that was lying on the ground should be the same object that moves to player's hand). It's easy with singleplayer just disable rigidbody and set the hand as a parent, but I can't think of the logic for multilplayer. I tried to write the script:

    Code (CSharp):
    1. public void PickUp(Transform TPhand, Transform FPhand, ToolsManager toolsManager) //TPhand - third person model's hand that others see, FPhand - first person hand
    2.     {
    3.         rb.isKinematic = true;
    4.         if (GetComponent<Tool>())
    5.         {
    6.             if (pv.IsMine) // pv is PhotonView
    7.             {
    8.                 toolsManager.tool = GetComponent<Tool>();
    9.                 transform.SetParent(FPhand);
    10.                 transform.position = FPhand.position;
    11.             } else
    12.             {
    13.                 transform.SetParent(TPhand);
    14.                 transform.position = TPhand.position;
    15.             }
    16.         }
    17.         else
    18.         {
    19.             if (pv.IsMine)
    20.                 toolsManager.secondaryTool = gameObject.name;
    21.             transform.position = new Vector3(-10, -10, -10); //Secondary items don't need to be visible on player, so they don't have Tool script and are teleported to the "dumpster" (in case player decides to drop it)
    22.         }
    23.     }
    But with this the script works only with the client that picked the item up, for others the item remains lying on the ground despite having Photon View Transform component and being instantiated with Photon at the begining. Also sometimes the items teleports in other player's hand
    I've tried to search for tutorials about this, but all themes are old and referring to some "Pick Up, Scores and Teams" demo that is not included in PUN 2, also some suggest using RPCs, but I still don't get how to use them properly
     
  2. Vefery

    Vefery

    Joined:
    Feb 23, 2018
    Posts:
    119
    please somebody
     
  3. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    You can TransferOwnership of PhotonViews, and also you can use RPCs if you wanted to send info about the new Owner Player to others in the room.

    Also, a side note, you may want to look into using OnPhotonSerializeView for IPunObservable scripts.
     
    Last edited: Oct 29, 2020