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.

Photon Unity Networking

Discussion in 'Multiplayer' started by tobiass, Aug 23, 2011.

  1. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
    PUN Classic is PUN 1.x and not recommended anymore.
    PUN 2 fixed a lot of the problems and is still something you can use, yes.
    However, there are not a lot of reasons, why you shouldn't use Fusion. It supports newer features of Unity, has proper authority (which clarifies who does what) and the synchronization of objects is better out of the box. Not everything you do with it has to be high-end.
    Having advanced features doesn't mean you have to use them. But later on, you could.
    Coming from PUN, use the shared mode and you don't have to worry about how the host runs everyone's state via input alone.
     
    Last edited: Sep 26, 2022
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
    I was now able to check in Unity 2019.4. and could build and run UWP after one simple fix:
    In NetworkingPeer.cs, line 839, change the compile condition from UNITY_WINRT to NETFX_CORE (keep the not in editor condition).
    That works, pings the regions (when using Best Region setting) and seems fine overall.
    I actually can't even make sure this works in 2017.x too.
     
  3. ayrton2389

    ayrton2389

    Joined:
    Oct 25, 2013
    Posts:
    26
    Thank you for the solution! i really appreciate it!
     
    tobiass likes this.
  4. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    323
    Hello, I think that there's a BUG in Photon PUN.

    I have a Gameobject that can be moved by 2 users, and basically each of them take its ownership pressing a button. Unfortunately, after some movements (just position and rotation), when one of them take its ownership it "jitter" quite a lot (it move to another position and back to the right one) on the user that "lost" the ownership (but not on the user that is the new owner).

    This issue does not happen just in my game, but in the every PUN tutorial too.

    It's easy to reproduce:

    1 - create a Cube, with its PhotonView and PhotonTransformView
    2 - create a simple script, attached to the cube, with the function "pv.RequestOwnership();" and a button that call this function
    3 - in gameplay, user1 can move and rotate the cube with the mouse in the scene, and its position/rotation are updated on user2
    4 - user2 press the button to take the ownership, and to move and rotate the cube, and its position/rotation are updated on user1
    5 - after a couple of movements, and changing from user1 and user2, you will see that the cube, on the user that just lost the ownership, will move around its position a bit

    Is it a (huge) bug?

    Many thanks!
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,724
    You should post your code that reproduces the bug, otherwise it isn't certain whether it's Photon or your code that's at fault.
     
  6. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    323
    It's not a matter of my code. As I wrote, it does happen in every single Photon PUN project that I've made from scratch, or downloaded from Github.
    If you just create a very simple scene, as I described above, you will see this.

    Anyway the code is very simple:
    Code (CSharp):
    1. public PhotonView pv;
    2.  
    3. public void TakeOwnership()
    4.     {
    5.         pv.TransferOwnership(PhotonNetwork.LocalPlayer);
    6.     }
    The good news is that I've been able to avoid this issue, with some lines of code (but in my opinion this behaviour is quite a shame)

    This is my solution:
    Code (CSharp):
    1. public PhotonView pv;
    2. public bool imMovingTheGameobject;
    3. List<PhotonTransformView> ptvList = new List<PhotonTransformView>();
    4.  
    5. void Start()
    6.     {
    7.         foreach (var observed in pv.ObservedComponents)
    8.         {
    9.             if (observed.gameObject.GetComponent<PhotonTransformView>() != null)
    10.             {
    11.                 PhotonTransformView phv = observed.gameObject.GetComponent<PhotonTransformView>();
    12.                 ptvList.Add(phv);
    13.             }
    14.         }
    15.     }
    16.  
    17. void Update()
    18.     {
    19.         if (!pv.IsMine)
    20.         {
    21.            SetTransformViewEnabled(true);
    22.         }
    23.  
    24.         else
    25.         {
    26.            SetTransformViewEnabled(imMovingTheGameobject ? true : false)
    27.         }
    28.     }
    29.  
    30.  
    31. void SetTransformViewEnabled(bool value)
    32.     {
    33.         foreach (var ptv in ptvList)
    34.         {
    35.             if (ptv.enabled != value)
    36.                 ptv.enabled = value;
    37.         }
    38.     }
    39.  
    It's not so nice, because I'm enabling and disabling the TransformView Component on Update, but it work, and actually it seem to me the best solution for this bug
     
    Last edited: Dec 1, 2022
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,724
    You've posted your workaround code, but not how you've fully implemented the ownership transfer itself. For example, you haven't shown the RequestOwnership call or the code on the current owner that handles the OnOwnershipRequest callback.

    The only reason I ask for clarification is that I haven't encountered the same behaviour myself with ownership transfer.

    Edit: My understanding (which works as expected) is that TransferOwnership is called on the current owner, passing the id of the new(requesting) owner as an argument, when it receives a request to transfer ownership. But in your brief example you seem to be transferring ownership to the same client that invokes the transfer, which doesn't make sense.

    https://doc.photonengine.com/en-us/pun/v1/demos-and-tutorials/package-demos/ownership-transfer
     
    Last edited: Dec 2, 2022
  8. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    323
    The "TakeOwnership" function in called by the user that has NOT the ownership, pressing a button "TAKE THE OWNERSHIP".
    The function
    Code (CSharp):
    1. TransferOwnership(PhotonNetwork.LocalPlayer)
    as far as I know is exactly the same as "RequestOwnership"
    I dont have any callbacks, because I don't need them, and I suppose that it should work just as it is
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,724
    I'm assuming your TakeOwnership method is executing on the client PC that is wanting to become the new owner, in which case I think your understanding of how it works is flawed...

    From the docs I linked to in my previous post, regarding the OnOwnershipRequest callback,
    When someone requests ownership, this will be called by PUN on the client that is the current owner. As you can see, the current owner calls view.TransferOwnership(requestingPlayer.ID) to actually transfer the PhotonView to it's new owner.


    TransferOwnership is definitely not the same as RequestOwnership, and as far as I understand it, what you have won't work as it is, which I think is borne out by the fact that you are noticing odd behaviour,

    I'm using the RequestOwnership/TransferOwnership program flow exactly as explained in the docs example and it works perfectly.

    Maybe @tobiass can weigh and correct me if I'm wrong.
     
    Last edited: Dec 4, 2022
  10. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    323
    Tank-you for the explanation.
    I've tried with "RequestOwnership" but the issue is still there
     
  11. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,724
    Sorry you're still having this problem, but it's going to be difficult to help further unless you post example code that reproduces the problem, which so far you haven't.
     
    Last edited: Dec 5, 2022
  12. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    Your logic is not correct at all, lets say player 1 is the owner, as of now, if player 2 presses the button it can't take ownership from player 1, player 1 has to give it, you will have to send an RPC to the player who presses the button which is called by the owner (player 1)
     
  13. hellaeon

    hellaeon

    Joined:
    Jul 20, 2010
    Posts:
    90
    Hi there. I'm trying to find out where Photon.Realtime fits in all the old and new photon stuff.

    It seems to be the unspoken child.

    We like to use it because of the exposed interfaces allows us to come up with a dependency injection setup for our systems. @tobiass are you able to summarise in a couple of sentences where realtime sits and it's development sits? Will it be supported for a number of years yet?

    Cheers
     
  14. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
    The Realtime API is the basis for pretty much all SDKs we offer. It solves the matchmaking, enables communication in rooms and acts as relay for Fusion (if needs be). It will be a central API for Photon in the next years and you can use it directly, for sure.
    It's a bit more basic to explain and we have higher level solutions on top of it, which means we don't talk about it as much, yes. Also it's not changing quickly, so there are less news about it, too.

    The latest version of the Realtime API is on our SDK download page.
     
  15. hellaeon

    hellaeon

    Joined:
    Jul 20, 2010
    Posts:
    90
    Thanks @tobiass we have started to implement it. Thanks for the information.
     
  16. GamePyro-com

    GamePyro-com

    Joined:
    Jul 28, 2018
    Posts:
    193
    Hello

    Have problem getting Photon PUN 2 to work in my project try since much hours different scripts i coded and configurations.

    I have a Player with (Rigidbody, Sphere Colilider, Player Controller Script, Spring Joint)

    The Photon View, Photon Transform View work at it when i instantiate the Player

    But the Player Model with animator is instantiated also after it with the Model player selected.

    So the Player Model is Child of the Player. It has the Animator, a Player Model Data Script on it

    So its the Player as Parent and in it the Player Model as Child

    also tryed with Instantiate with PhotonNetworking and at the Child PlayerModel his Own PhotonView.



    Always same problem.


    I tried set at the Player model also the Photon Transform View and Photon Animator View. and at the Parent it auto find all and it finds in child. But the animations from Child are not much synced to other players not all and only some also not the speed and pose .




    I wrote a script and changed the Photon Animator that it can be at the Parent and use the Animator from Child to sync over network but same problem only some animations get synced.
    It feels like Photon hangs at syncing and only sends some changes when it uses the Animator and sync from Child object.



    If i instantiate the Player with PhotonView with Photon Networking and the Player Model without set Player as Parent then it works it synces the Animations all and then in Editor i can move the PlayerModel with Mouse into the Player it works all but not when set Player as Parent with Script to the Child why.


    When sync only Player Model to other Players over network it is at 0,0,0 because it needs the Positions of the Player thats why its Parent in Player as Child. Also for other scripts work that when other player crash in so it need be Player with the Player Model as Child synced


    Hope someone can help find the problem or fix it or is it a Photon problem


    Also tryed same as he done its nearly same i do but same problem still not all animations get synced



    Regards Thank you so much
     
  17. Joginder_Pal

    Joginder_Pal

    Joined:
    Oct 10, 2022
    Posts:
    2
    i am stucked in same situation.. can you tell me what you done to fix this...
     
  18. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,619
    Ya there is a option somewhere on a manager that destroys objects when new players join or something like that. Can't remember fully.
     
  19. Joginder_Pal

    Joginder_Pal

    Joined:
    Oct 10, 2022
    Posts:
    2
    Can You tell me how can i fix this issue for my project. as i am also stuck here too
     
  20. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
    I posted some questions for that issue. Try to find the answers for them and then go from there.
    It is a usage issue mostly. If you are stuck, please post a new thread, tell us the related info for your project, etc. "I am stuck there too" is not enough info to help properly.
     
  21. unity_8C2CBBFCCF91C9EF8710

    unity_8C2CBBFCCF91C9EF8710

    Joined:
    Apr 26, 2023
    Posts:
    1
    201 means a player has loaded a plugin into their game