1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Photon Unity Networking

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

  1. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    667
    Hello,
    am I right in thinking that I can change the PhotonTransformView a bit to sync player positions with a floating origin without having to hassle with custom interpolation code, since it gets the position from the PositionControl?
    That looks too easy to be true.
     
    Last edited: Jan 6, 2017
  3. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    123
    Okay so I haven't touched Photon Cloud for another three months. I started working with it today and everything's just so strange to me (after a lot of UNET-ing).

    So I thought this would be a piece of cake:
    - Do something specific while we are the Local Player
    - Do something specific while we are not the Local Player
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LocalRemoteAnnouncer : Photon.MonoBehaviour {
    5.  
    6.     public AudioClip localSound;
    7.     public AudioClip remoteSound;
    8.  
    9.     // Use this for initialization
    10.     void Awake () {
    11.         if (photonView.isMine) {
    12.             //Play "friendly" AudioClips when we are the isLocalPlayer
    13.             GetComponent<AudioSource> ().PlayOneShot (localSound);
    14.         } else {
    15.             //Play "hostile" AudioClips when we're not the isLocalPlayer
    16.             GetComponent<AudioSource> ().PlayOneShot (remoteSound);
    17.         }
    18.     }
    19. }
    20.  
    and the prefab itself
    [​IMG]

    ...but is there anyone who could explain to me why the code, when we are the Local Player, is being executed while I'm not the stupid Local Player? o_O
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    @Justice0Juic3, the PhotonView.isMine is true, when the local client instantiated the prefab (with PhotonNetwork.Instantiate()). Maybe you're not using PhotonNetwork.Instantiate()?
    Also: When using PhotonNetwork.Instantiate(), all clients in that room will instantiate that object. No need to call an RPC, to make everyone call our Instantiate(). If you would do that, every client will instantiate it's own copy of the object (and thus trigger the script's isMine).
     
  5. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    123
    @tobiass - I only used PhotonNetwork.Instantiate to spawn the player at the beginning of the game. Thanks for info by the way. I'll see what I can do.

    I think I got it.
     
  6. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    196
    Hi,

    - I just tested and it works, during awake, isMine is behaving as expected.

    - From the screenshot, you seem to have an old version. I would recommend updating to the latest Photon and then test. Make a backup before of course :)

    - You don't need to observe your component to check for isMine. This is not necessary.

    Bye,

    Jean
     
  7. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    196
    Hi,

    What do you mean by "Floating origin" ?

    If you want to write your own position synchronizer, using PhotonTransformView and modify the code is likely too much, The PhotonTransformView is here to help you get running in no time with default setups.
    If you need something specific, writing your own is definitly the best and quickest way. It's really only few lines to write and read the position with the serializer. Check out the "Demo Synchronizer" and the script CubeLerp.cs for example,

    Bye,

    Jean
     
  8. NebuTube

    NebuTube

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hello. I want to do a game on Photon Cloud. But is there possible play with friends in local wifi like in UNET. I mean 2 buttons. First it play with all players using PhotonCloudInternet and second it play locally like in MiniMilitia
     
  9. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    PUN does not support local (WiFi/Bluetooth) connections.
     
  10. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    174
    how to handle bullet spawn on photon. Currently when i fire my bullet seems to spawn on all the players in room instead of just player who shot.
    Code (CSharp):
    1.  
    2. voidFire(){
    3. Quaternion rotate = Camera.main.transform.rotation;
    4.         Vector3 bullpos = bulletspwan.transform.position;
    5.         gameObject.GetComponent<PhotonView> ().RPC ("spawnbullet", PhotonTargets.All,rotate,bullpos,PhotonNetwork.player.ID);
    6. }
    7.     [PunRPC]
    8.     void spawnbullet(Quaternion rotate,Vector3 bullpos,int playerid)
    9.     {
    10.         PhotonView pv = GetComponent<PhotonView> ();
    11.         if (PhotonNetwork.player.ID==playerid) {
    12.             GameObject bull = (GameObject)Instantiate (bullet, bullpos, rotate);
    13.             bull.GetComponentInChildren<Rigidbody> ().AddForce (bull.transform.forward * 5000);
    14.             Debug.Log ("bullet");
    15.         }
    16.     }
     
  11. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    Isn't that what you would want to happen? All clients see the bullet?
    An RPC is called on a specific object and executed by all clients. If you want only a few clients to "see" it, then you need to come up with some rule who should spawn/have the bullet.
     
  12. NebuTube

    NebuTube

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hello. Anyway I was trying to use PUN because Miltiplayer more needed than local. And I have one question now

    How can I sort playerList like they joined to the room? I really need it using any way!
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,000
    Hi there
    I have been away from Photon for a few weeks so I hope i am missing a simple step but I don't think so.....

    My issue is for some reason, an object Instantiated over the network, if isMasterClient(), sends and RPC("functionX", PhotonTargets.All, value). the result is, functionX is called on the master client but no other.


    Code (csharp):
    1.  
    2. ///At start, Multi Game Mng, if is MasterClient, ChooseLevel()
    3.    void Start(){
    4. if(PhotonNetwork.isMasterClient){
    5. GameObject mLineMng = PhotonNetwork.Instantiate("multi_lineMng", Vector3.zero, Quaternion.identity, 0);
    6. }
    7. }

    Code (csharp):
    1.  
    2. /// in the just spawned over the network, multi_LineMng, at Start(),
    3. //we ask if is MasterClient, if so, initSections()
    4. void Start(){
    5. if(PhotonNetwork.isMasterClient){
    6. int[] values = MultiGameMng.instance.init_sections_asInt;
    7. initSections(values);
    8. return;
    9. }
    10. }
    11.  
    12. //Sent only on the masterClient, here we initiate the photonView.RPC() to all targets...
    13. ///.... we pass an int[] init_section_asInt
    14.  
    15. public void initSections(int[] init_section_asInt){
    16. ///here, we use the photonView, to init the create_initSections() across network on each client.
    17. photonView.RPC("create_initSections", PhotonTargets.All, init_section_asInt);
    18. }
    19.  
    20. [PunRPC]
    21. public void create_initSections(int[] init_section_asInt){
    22. ///THE RESULT.. only fires on MasterClient
    23. }
    24.  



    Any ideas???
    Thanks
     
  14. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    All players have an ActorNumber internally in a room. This is the PhotonPlayer.ID in PUN and you can sort by that.
    You can also take a look at the ColorPerPlayer and PlayerRoomIndexing scripts. Those are even better than ordering the list of players: You get players assigned to 1..maxPlayers, no matter who leaves, joins, etc.. So if you want player 1..4 in a four-player game, this is a base for you.

    @renman3000: I assume the problem is that your master client is in the room when that gets created. It will call initSections() and that will call the RPC. As that is not buffered, only the current players in a room will execute this, which means only the master client does it.
    You can buffer the RPC or you can make your live even easier by using a Custom Room Property to save the section init for everyone. Think shared player preferences.
    Here's a little guide:
    http://doc.photonengine.com/en-us/pun/current/manuals-and-demos/synchronization-and-state/
     
    Last edited: Jan 19, 2017
    renman3000 likes this.
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,000

    Thanks!! Will try. Page for link "not found".

    Anyhow, ok
     
  16. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    Fixed that link :)
     
    renman3000 likes this.
  17. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,000
    @tobiass
    Hi, so the issue was a condition I was asking on my end. SOLVED!!!!
    THANKS
     
    tobiass likes this.
  18. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    174
    Bullet is spawned on each player instead of only player that actually fired shot.
    I am already sending playerid of person who fired shot and if player id is same as current player id then spawn bullet.
    But it seems to spawn on all players instead of one
     
  19. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    Don't you want to show the bullet on the other player's screen?
    If not: Why not spawn the bullet in Fire() instead of in the RPC? Fire() is local. The RPC is executed by everyone (who knows about the networked GameObject).
     
  20. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    174
    Let me explain properly, lets say there are three players, player1,player2 and player3.
    Player 1 fires shot which calls rpc to spawn bullet. bullet is spawned on player1 flys through. Player 2 and player3 can see this bullet. This is what i want to achieve.

    But this what actually is happening.
    Player1 fires shot which calls spawnbullet rpc.
    Now bullet is spawned on all 3 players. So all three players are shooting bullet . So bullet is spawned on each player and flys through. instead of spawning only on the main player
    I am already checking player id
    if (PhotonNetwork.player.ID==playerid)

    but this doesnt seems to work
     
  21. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,000
    Hi @tobiass or other,
    If I have Photon, Instantiate a GameObejct, this gameObject has a PhotonView itself. Can I gather the rank of each instance across the network?

    My issue is this, I want each to know if it is 0, 1, 2, 3 etc.


    Thanks!
     
  22. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    @renman3000: I don't know what the rank means. A PhotonView simply is a component that identifies one object across all clients/players in a room. So there is no rank as such.
    Players have an ID and there are scripts in PUN which order players into player-numbers.
     
  23. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,000
    Right,
    So rank is a very loose term. I simply want to know that I can associate player x with 0, player y with 1 etc.

    How do I do that natively? At the moment I am just creating my own list.
     
  24. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    In a game/room, you can associate player x with position/rank A, etc. Of course.

    Please take a look at the ColorPerPlayer and PlayerRoomIndexing scripts. You get players assigned to 1..maxPlayers, no matter who leaves, joins, etc.. So if you want player 1..4 in a four-player game, this is a base for you.
    I don't have a written manual for doing this but the script should commented and hopefully useful.
     
  25. sezbladex

    sezbladex

    Joined:
    Sep 15, 2013
    Posts:
    22
    I used pun 1.74 same codes same project its well but now i updated 1.80 my game doesnt work i create room and someone player join my room.other player invisible
     
  26. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    150
    I can't seem to understand Photon architecture? I just want have classic client-server architecture for my game, where server is also player(no dedicated server) without any fancy clouds, rooms etc, and I can't find any simple api to just connect to host? I want one player to host game, and other needs to know it's IP to connect, just like in Little Fighter 2, is that even possible with Pun?
    Or even if I use these rooms, how do I set if I'm hosting game(server) or I am client?
    Everywhere in descriptions I see lot of how easy to use it as and friendly API, and I have completely opposite feeling, like I dont get this system completely. I dont need clound I just need better networking solution than Unity's UNET.
     
    Last edited: Jan 25, 2017
  27. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    150
    From documentation:
    "By default, the LoadBalancing API will get you into the game's "Lobby" when you connect. While in the lobby, the Master Server provides a list of rooms to a client. To keep traffic low, clients can't interact with each other in the Lobby.

    A room can be considered a separate area where players interact (play their games). While they are in the same room, players can send and receive events from others, change/update properties of the room etc."


    If I understand correctly there is no game-server in Photon architecture, everyone who joins game as client....? Does that make sense? How is that even possible? What if I want physics to run only on server, and clients to just update their transform positions?
     
  28. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    @Roni92pl: I'm sorry you feel confused and let down. The thing is most likely that PUN's architecture simply doesn't match your requirements. That happens.
    The PUN package only works with dedicated servers by design. The so called Master Client can take over special duties in the room (similar to the host).

    If you want players to host matches, you should consider our Bolt package (docs pages). Any player can host a game and others connect directly to it. It has support to make parts authoritative and we're very happy with the latest reviews by our customers.
    The Unity Forum thread for Bolt is this: https://forum.unity3d.com/threads/photon-bolt.374376/
     
  29. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    150
    Ok, thank you for explanation, so if I understand correctly, I can't have e.g. one player(client) being 'authorative' where he's doing all physx, and other simulations, and other client being only 'renderer' mostly? Or is that possible, but PUN is not designed to work like that.
     
  30. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    You could make one PUN client authoritative for events/actions in-game but it's not best practice.
    As you plan to run authoritative physics (which is hard to do consistently) I would recommend Bolt, which fits your ideas/requirements better (games are hosted in Unity).
     
  31. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    150
    Ok, I'll definitely look into Bolt, thanks again ;
     
    tobiass likes this.
  32. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    179
    I met a case when new joined player do not receive BufferedRPC's for scene instantiated objects.
    I am using PhotonNetwork.InstantiateSceneObject() method because need that object exactly after scene is loaded.
    Some modifications can be applied to that object (rarely) and when it did, I send RPC targeted as AllBuffred.
    New joined player do not receive that Rpc. I broke my mind, trying to find solution or explanation of that case.

    What is the problem? Can it be because scene owned object can't send buffered rpc? Is yes, what is the best way to sync data in that case?
     
  33. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    @novaVision: Are you also using Interest Groups? The buffering only works when you use group 0 (default).
    Is anything logged in the console in these cases? If there the RPCs arrive but don't find their target, it should log something.
    Do you have a repro case which you could share?
     
  34. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    179
    @tobiass group is set to 0, if you mean the group I shall to set on scene object instantiating.
    RPC do not arrive at all and no errors in console about it.
    The only error I receive (and received before as well) is screenshot but I got it for a long time already, and was cause because of OnPhotonSerializeView player components syncing on start. I didn't find any solution and leave it like that.
     
  35. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    Then, I think, we need a repro case for this. Some little project which shows the error quickly without us doing much.
    Please mail it as download link to: developer@exitgames.com and we can have a look at it.
     
  36. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    Hi @tobiass! Can you post a link to a Getting Started or tutorial page? I really want to try this out. :)
     
  37. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    I found them! :)

    @tobiass Is is possible to run authoritative server code in Photon Cloud? Or is it only possible using Photon Server?
     
  38. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    196
    Hi,

    with the Cloud, you can only use WebHooks, which allows you to catch activities and run your own routine but nothing authoritative no. Maybe this is enough for what you want to achieve. Can you describe what you want to do?


    Bye,

    Jean
     
  39. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    @Jean-Fabre Thank you for the response!

    I don't think WebHooks would be enough. The game I'm making is a turn-based card game (think Hearthstone). I would need server-side validation for moves, and then save the results to a database after the match. The server should also handle the timer for each turn.

    I was reading about Photon Server. However, I can't find a tutorial or a sample on how to integrate it with Unity. Is there something like that out there? I am also thinking of deploying it on AWS EC2.
     
  40. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    196
    Hi,

    sure, there is a 5mn tutorial to get you up and running with a local onPremise server, which is the same process than on AWS EC2 apart from the having to do so with a remote terminal. If you have never worked with AWS, then it's going to be a steep learning curve at first, so be prepared :) and maybe getting help from a someone with experience with AWS would be best at least to get up and running.

    https://doc.photonengine.com/en-us/onpremise/current/getting-started/photon-server-in-5min

    Bye,

    Jean
     
  41. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    @Jean-Fabre
    I've seen this video before. But what I'm looking for is a tutorial on how to have your Unity client connect to Photon Server. Is there something like that out there?
     
  42. ChristianSimon

    ChristianSimon

    Joined:
    Jun 20, 2016
    Posts:
    5
    Hi @zee_ola05,

    this is basically the same as you would do when connecting to the Photon Cloud. Navigate to the PhotonServerSettings file in your project, change the hosting to Self Hosted, add address, port and protocol. From one of your scripts call
    Code (CSharp):
    1. PhotonNetwork.ConnectUsingSettings(string gameVersion);
    and you should be ready to go.
     
  43. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
  44. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    My game is a card game. I want my clients to send their input/actions to the server. And have the server validate these actions, then send acknowledgments back to the client. But I can't seem to find a way to send messages from client to server.

    Is there a way for PUN to send messages to Phtoton Server? So far I got these:
    Code (CSharp):
    1. PhotonView photonView = PhotonView.Get(this);
    2. photonView.RPC("SomeFunction", PhotonTargets.All, "parameter1");
    Code (CSharp):
    1. PhotonNetwork.RaiseEvent(evCode, content, reliable, null);
    But both don't have a way of specifying sending it to Server Only.

    How do you guys do it?
    PS. I'm also creating a separate thread for this problem.
     
  45. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    143
    Just an update regarding my last question. I have found a solution - described in this thread.
     
  46. Ogoku

    Ogoku

    Joined:
    Feb 10, 2015
    Posts:
    5
    Hi,

    how would I realize a ball in a Football/Soccer game (it is Football, because the other sport is Handegg ;P). Where every client can interact with the ball but on every client it should move the same way.
     
  47. RF008

    RF008

    Joined:
    Oct 28, 2016
    Posts:
    20
    does photon have dedicated slack chat channel ?
    if provide link thanks.
     
  48. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,837
    @RF008: We don't have a slack channel for Photon, sorry. Post in our forum and we do our best to help asap..

    @Ogoku: When every player can interact with an item at the same time, things get hairy: Every client needs to get the same result of what happened - no matter if there's lag or temporary loss of messages, etc..
    PUN does not have a "out of the box" solution for it. You would need to have an idea how to solve this with networked messages and then use PUN to send those.
    Or you could take a look at our TrueSync package for one potential solution.
     
  49. Shivam_Dhoot

    Shivam_Dhoot

    Joined:
    Sep 24, 2014
    Posts:
    3
    Hi,
    Going through a crisis.

    The crisis is, my game is intended to run in enterprise network where we can't enable ports locally and have to use a proxy and stuff. I am adding a pic which i got from my friend working over there, I am adding that here.

    What I understand is:
    I need to set a proxy in Photon Unity Networking, because I am trying execute the game in a private network in computer laboratories at my university.
    I don't obtain to establish connection with Photon Cloud, once the proxy is blocking my communication with the server.

    please help...
     

    Attached Files:

  50. RF008

    RF008

    Joined:
    Oct 28, 2016
    Posts:
    20
    Hi i want to do matchmaking based on player ranks. i need sample script where i can make match between two players based on the rank.
    i have tried old forum posts those are outdated i am looking for solid sample Thanks
    Thanks
     
    Last edited: Mar 1, 2017