Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party New Photon Unity Networking Tutorial Series

Discussion in 'Multiplayer' started by Oliver-Eberlei, Apr 17, 2014.

  1. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    what you bought on asset store can be used in commercial projects
     
    tobiass likes this.
  2. detrohutt

    detrohutt

    Joined:
    Feb 13, 2015
    Posts:
    2
    Perfect, thanks Damien!
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @detrohutt: Of course you can use the code and assets in your game and apps. Aside from distributing the content, you are free to use it in your projects as you need to.
     
  4. Deleted User

    Deleted User

    Guest

    I hope you'll continue the tutorials! I'd love to see one on Cheat Prevention.
     
  5. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Hi i purchased the asset and was trying to use the PickupBase for my photon project i'm still a newbie in programming , was wondering since the script uses Ship ship and tag "Ship" for the code what if i use 2 differently named prefabs for the players (PlayerTeam1 , PlayerTeam2 ) wich uses the same tag "Player" but can both use the pickup , how would i make the code work ? And is it attached to something or its just accessed by other scripts . TY !
     
  6. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    The name of the prefab or the name of the gameObject doesn't matter. As long as it has the Tag "Ship" and it has a Ship component it should work either way.

    If you want to use the tag "Player", you simply change this line in the OnTriggerEnter method:

    if( collider.tag == "Ship" )
     
  7. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Hi Oliver thank you for the quick reply !

    I change the OnTriggerEnter tag to "Player" but then i get the error : the type or namespace 'Ship' could not be found. Are you missing a directive or an assembly reference ?

    Since i have no Ship in my project.
     
  8. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Well, the code is obviously looking for the component Ship with the code in the OnTriggerEnter method:

    Code (csharp):
    1.     Ship ship = collider.gameObject.GetComponent<Ship>();
    2.  
    3.     if( CanBePickedUpBy( ship ) == true )
    4.     {
    5.         PickupObject( ship );
    6.     }
    The tutorial uses the ship component to handle the pickup. If you don't have the ship component then you have to rewrite that method so it uses your own components.
     
  9. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Yes , but instead of using the Ship component ( wich i think is a component shared in both teams in the sky arena project) since in my project i use 2 different components one for each team ( PlayerTeam1 and PlayerTeam2) i was wondering how could i write the code so that both components with the same tag "Player" in my case could both use the Pickup ?

    :S Sorry if this is a bit confusing i'm still just trying to understand the basics of this...
     
  10. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Yes you can, though I would suggest that this is not the best approach. Since it sounds like you will duplicate a lot of code if you have PlayerTeam1 and PlayerTeam2

    What you can do is create a base class, for example PlayerTeamBase

    Then you extend both classes from this base class like so

    Code (csharp):
    1. public class PlayerTeam1 : PlayerTeamBase
    and in the OnTriggerEnter method you can then do

    Code (csharp):
    1. GetComponent<PlayerTeamBase>()
    which will give you either PlayerTeam1 or PlayerTeam2, because both of them count as a PlayerTeamBase class.
    This way you can only access methods that are defined in PlayerTeamBase though, so they will work for both classes.

    You can also do something like this (but now we are getting into really ugly code)

    Code (csharp):
    1. PlayerTeamBase baseClass = collider.gameObject.GetComponent<PlayerTeamBase>();
    2.  
    3. if( baseClass is PlayerTeam1 )
    4. {
    5.     PlayerTeam1 team1Class = (PlayerTeam1)baseClass;
    6.  
    7.     //do you stuff with team1Class here
    8. }
    9. else if( baseClass is PlayerTeam2 )
    10. {
    11.     //same thing as above
    12. }
    but yeah,... even typing that makes my programmer heart cringe ;)
     
  11. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    So from this i would write :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public abstract class PickupBase : MonoBehaviour
    5. {
    6.     public class PlayerTeam1 : PlayerTeamBase;
    7.     public class PlayerTeam2 : PlayerTeamBase;
    8.    
    9.     public abstract bool CanBePickedUpBy( PlayerTeamBase playerTeamBase );
    10.    
    11.     public abstract void OnPickup( PlayerTeamBase playerTeamBase );
    12.    
    13.     PhotonView m_PhotonView;
    14.     protected PhotonView PhotonView
    15.     {
    16.         get
    17.         {
    18.             if( m_PhotonView == null )
    19.             {
    20.                 m_PhotonView = PhotonView.Get( this );
    21.             }
    22.            
    23.             return m_PhotonView;
    24.         }
    25.     }
    26.    
    27.     void OnTriggerEnter( Collider collider )
    28.     {
    29.         if( collider.tag == "Player" )
    30.         {
    31.             PlayerTeamBase playerTeamBase = collider.gameObject.GetComponent<PlayerTeamBase>();
    32.            
    33.             if( CanBePickedUpBy( playerTeamBase ) == true )
    34.             {
    35.                 PickupObject( playerTeamBase );
    36.             }
    37.         }
    38.     }
    39.    
    40.     void PickupObject( PlayerTeamBase playerTeamBase )
    41.     {
    42.        
    43.         if( PhotonNetwork.offlineMode == true )
    44.         {
    45.             OnPickup( PlayerTeamBase );
    46.         }
    47.         else
    48.         {
    49.             PhotonView.RPC(
    50.                 "OnPickup"
    51.                 , PhotonTargets.AllBufferedViaServer
    52.                 , new object[] { PlayerTeamBase.PhotonView.viewID }
    53.             );
    54.         }
    55.     }
    56.    
    57.     [RPC]
    58.     protected void OnPickup( int viewId )
    59.     {
    60.         PhotonView view = PhotonView.Find( viewId );
    61.        
    62.         if( view != null )
    63.         {
    64.             PlayerTeamBase playerTeamBase = view.GetComponent<PlayerTeamBase>();
    65.            
    66.             if( CanBePickedUpBy( playerTeamBase ) == true )
    67.             {
    68.                 OnPickup( playerTeamBase );
    69.             }
    70.         }
    71.     }
    72. }
    Am i close i know this is not the right way to code this since i get alot of errors in console :(
     
  12. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    It looks right, without the errors I can't know whats wrong.

    But, just a well meaning suggestion (I know you don't want to hear this) it looks like you should start with a more basic tutorial and learn how Unity and C# works before you jump into multiplayer development. Because this structure is the easy part of it all.
     
  13. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Well its not that i did'nt expect to hear this hehe !
    Even if i start over i will end up back at this point ...
    Its just that i'm trying to use example :

    i have 2 ships each with a different model different animations ... different GameObject (prefab) names ...
    trying to figure out how to make this work with the PickupBase Script ...
    Seems extremely complicated for me to do this with Photon i guess lol !
     
  14. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Well, the PickupBase script is an abstract class that you cannot use by itself. So you need another pickup script that implements this class which you can then use as a component.

    At the moment you are not using Photon at all. The PickupBase script is just game logic. It's not tied to Photon yet
     
  15. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    trying to understand Photon
    if i use photon i would start by adding ?

    using Hashtable = ExitGames.Client.Photon.Hashtable;

    then i tryed to use PhotonNetworkPlayer as component but i guess i need to split my head way further then this to start understanding i really thought there was an easyer solution for using a different player prefab for each team.

    But i will follow your lead and go back to learning more basic stuff...

    Thank you for trying , sorry to have bothered you :(
     
  16. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    I think you might just have the wrong idea about what your problem ist.

    Are you able to connect to a photon server and join a room? Because you have to do that first before you create any prefab at all. You should check out the Photon documentation first which shows you how to connect to a server with multiple clients. http://doc.exitgames.com/en/pun/current/getting-started/pun-intro

    Of course you can use different prefabs for different teams but you should try to get it to run with one prefab first
     
  17. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
  18. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Yes i connect to photon server with my serial, i already have lobby display of rooms, create room , join room etc...
    join team 1 join team 2 , Photon instantiate with spawn points and player prefabs with PlayerHealth scripts etc
    now i'm at a point where i made a HealthPickup script with a respawn timer attached to a GameObject(cube) that accesses my PlayerHealth script and adds health AllBuffered wich works correctly i get x ammount of health and all players are updated , my problem right now is that if ex: player A picks up HealthPickup but then player B enters the room , player B sees the HealthPickup GameObject but player A does not since he just picked it up...

    So to solve this i saw your tutorials and decided to buy the asset to understand how to make my pickups works so that when player B enters the room after player A took the Healthpickup he is updated and does not see it until it respawns so i am trying to apply your PickupBase concept so it uses the AllBufferedViaServer but i encountered the problem of using 2 diffrent player prefabs and modifying the PickupBase so that it works for the 2 different Player Prefabs and i can't seem to get it to work lol !

    Everything works , only my pickups not getting updated to newly joining players .... :(
     
  19. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    This was made before you ever released tutorials i don't have matchmaking and i'm pretty sure you would say the code is ugly lol ! so it might be good for me to start from scratch after months of modeling/rigging/animating ...

    Go to basics until i reach your future tutorials and keep trying with a better base :)

    But i really want to use different models for each teams so i'll keep at it and maybe one day i'll show you how it's done JK :p lol !

    Don't worry i'll work this out , its really not easy for you since you don't have my entire project in hand and i understand ,i really appreciate your responses and help TY Oliver.
     
  20. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Well, as I said, if you don't post your errors, I can't help you.
     
  21. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    This is the errors i get from the script above that i made from what you suggested , i did not toy with it as i went back in documentations to find a different approach for this (maybe with an index and make a call to instantiate all pickups from server but that is a big puzzle for me) but if this does the job i'm all ears . Like you said PickupBase is a simple clean way
    , if you want a better look you can just copy paste the script up above and have a real look at it in 2 clicks :p ...(seems like a parsing error i can't put my finger on)
    Appreciate your help !

    errors.jpg
     
    Last edited: Mar 27, 2015
  22. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Oh, right. Line 6 and 7 are class definitions. Funny how I missed that before... I think I am relying too much on the compiler nowadays :D If you want to define these classes they should be in another file like any other MonoBehaviours. I thought you already had classes for Team1 and Team2, isn't that what we are talking about? Or do you only have different prefabs with two different names without any components on them that should handle the pickup?

    All the other errors should only be a result of that.
     
  23. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    I have 2 different prefabs but i only had a script on the Pickup itself ( HealthPickup Script adds health accessing the player's health script with RPC AddHealthNetwork,PhotonPlayer ...) that is how its setup right now.

    But i put it on the HealthPickup's script and i still get parsing errors there for lines 6 and 7 ! :eek:
    Like need to add something after
    Public class PlayerTeam1 : PlayerTeamBase , _______ ?
    It underlines the semicolon in red and tells me parser error should be a , { or a 'where'
    ?
     
    Last edited: Mar 28, 2015
  24. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    i want to check the time and when the time is over more than one and less than five will join the room and rest of the player creats new one. anybody help me please
     
  25. PixelPaw

    PixelPaw

    Joined:
    Feb 18, 2013
    Posts:
    91
    Photon needs to make their stuff more newbie friendly lol !

    the whole thing is only a renderer that goes on and off why cant i just set photonview observe the renderer and have it do what its supposed to do lol .... that would save a whole lot of headackes but that does not work i guess ....

    Instead of doing a class to access a prefab that accesses a pickup script that accesses a pickup base script my goooooooooooooooooooooooooooooood * lol ...
     
  26. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    i want to check the time before start the game play scene. after that time will over more than one and less than 5 players will play the game and create new room for the rest of the players.
     
  27. Abhijit-Mukherjee

    Abhijit-Mukherjee

    Joined:
    Jan 9, 2015
    Posts:
    193
    How to you photon server for publishing unity web game in Facebok canvas .. Dose photon server allow HTTPS ..

    please help me . I need server to publish my game in Facebook ....
     
  28. Deleted User

    Deleted User

    Guest

    had this same problem...yesterday..

    .this not a NEW issue...just forgotten?

    I do not see..plainly written, in your product page.. that this does not support android..

    Can I just not use chat??

    p-.
     
    Last edited by a moderator: Apr 11, 2015
  29. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @ForceVFX: You could update Sky Arena to the latest PUN Free and Chat and then move to PUN+. It's just more convenient when the package is ready and most up to date.

    I will ask Oliver to do an update to the latest PUN Free version in Sky Arena. That supports Chat and can also be migrated to PUN+ (including Chat support). The PUN+ package is supporting Android and Chat.
    In Unity 5.x, you don't need PUN+ to export to mobile, as Unity's licensing changed.
     
  30. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Hi. Just landed on the networking area, started to read the unity documentation and following tutorials to get the idea and hopefully the skills..

    From the incoming features I would love to see these specially.
    • Multiple Maps
    • Multiple Game Modes {looks like this one is already done!}
    • User Authentication
    [edit]: I see this will involve ships so would also love to see how
    mecanim should be networked.
    • Mecanim
     
    Last edited: Apr 19, 2015
  31. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Started to see the series. Wow, amazing work, very focused on the multiplayer side without loosing focus by messing with art etc.
    Skipping the server setup was a bit disorienting. I think if master client uses its pc you can have 100ccu with teh free plan, if this is right it might worth a video efen if it is not so nicely edited.
    I found it a bit crazy to skip, to be honest.
     
  32. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    Anybody reply me
     
  33. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    i want to play the game play scene when there may be more than one and less than 5 players are in the room. anybody can tell me how it is possible? I also want to check the time. After that time is over more than one and less than 5 players automatically play the game play scene. i have searched all the way but there is no clue, hint or anything else related to my problem. I think photon only supports the players enter game play scene one by one. I have never found any demo , example according to this problem like more than one player enter in the room and play the game play scene simultaneously. if there is any demo, example, tutorial or anything else please refer me and i am thinking that photon do not support this type of things. if support it the give me any reference tutorial. if not then also do let me confirm by you. It will be very thankful to you.
     
  34. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
  35. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    The part 2 audo quality is terrible. Voice sounds like if it was in an empty room and has really low quality, the music is so loud that interferes with voice.
    Filter that voice and remove the music as it does really not help to follow. When showing code I would also remove the picture of yourself as adds nothing but distraction to what is important that is the code.
    The speed of the part 2 is too high. No way to get a thing with that massive flow of data. Have a look to 3d buzz vids to get an idea of what a good speed for a videotutorial is. I'm considering buying the assets but I'm afraid of getting lost with this hi-speed tuts...
     
    VoLkizz likes this.
  36. VoLkizz

    VoLkizz

    Joined:
    Mar 29, 2014
    Posts:
    40
    Agree with Pulov!

    Want to add that it would be great to show how SA was created from very beginning, step by step (it would make connections between scripts more clear).That's what i expected, when bought Sky Arena Tutorial asset. A bit disappointed...
     
    danish115 likes this.
  37. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Hey guys,

    I finally updated the project so it works with Unity 5. I've also update PUN to the newest version so it is really easy to switch it out with PUN+ now, if you want to.

    Thanks for the feedback on the videos Pulov and VoLkizz. You're right that the audio is not as good on these ones. I had some technical issues there and I will make sure to keep the music in check if I do more :)
     
  38. jdzsk

    jdzsk

    Joined:
    Jan 24, 2015
    Posts:
    1
    Hi!

    I haven't watched these tutorials yet, but I'm making a 2D CTF type game. Can I use these tutorials and somehow try to port the code to work in 2D? (I think I can, the real question is how) If so.. what would you suggest looking at to make the character controls work with 2D enviroment?

    And is this authoritative approach or non-authoritative?

    I'm going to watch these tutorials regardless if the answer to my question is positive or not, since they seem time-worthy, but wanted to know this stuff before-hand.
     
  39. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Nice Pack so far, but i have really problems to setup another model for the game. I have done the setup from the Ship_Player_07, but it does not appear in the scene.
    Or it is important to make "Ship_Player_0x" i get no success there. !?
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. ShipVisuals.SetTeamColors (Team team) (at Assets/Scripts/Ship/ShipVisuals.cs:142)
    4. Ship.SetTeam (Team team) (at Assets/Scripts/Ship/Ship.cs:192)
    5. PlayerSpawner.CreateLocalPlayer (Team team) (at Assets/Scripts/General/PlayerSpawner.cs:38)
    6. PickTeamGUI.ChooseTeam (Team team) (at Assets/Scripts/GUI/PickTeamGUI.cs:81)
    7. PickTeamGUI.OnGUI () (at Assets/Scripts/GUI/PickTeamGUI.cs:100)
    8.  
     
    Last edited: May 16, 2015
  40. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    I don't have this pack but have you checked that it is in a folder named "Resources"?. Also. Make sure you saved the prefab active and thath the names do match. Else. I'm out.
     
  41. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Oh this is helping me a lot, i found it :) And i will give it a try. Thank you
     
  42. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Can anybody tell me where are the level Border are set, please? i think it is in a script?

    Found it:

    Code (CSharp):
    1.         //We don't want the player to fly too close to the sun or the ground. It's hurtful
    2.         newPosition.y = Mathf.Clamp( newPosition.y, 0.5f, 500f );
    3.         transform.position = newPosition;
     
    Last edited: May 17, 2015
  43. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Looks like the Photon have problems with large level?
    How big can the level be?
    My Level is ca. 12-14 times bigger then yours.

    What is the max size for a good gameplay?
     

    Attached Files:

  44. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    i have just complete my multiplayer bike racing game in the same city which you are using. its not an issue of asseets dude.
     
  45. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    But i have nothing changed, i have replaced only the level!?

    See the screenshot: it does not look that it is syncing. Time and Position is wrong.
     

    Attached Files:

    Last edited: May 19, 2015
  46. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    you've errors, iron them first.
     
  47. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    the error is persistent in any level:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. GamemodeCaptureTheFlag.OnTearDown () (at Assets/Scripts/Gamemodes/GamemodeCaptureTheFlag.cs:40)
    3. GamemodeManager.InitiateSelectedGamemode () (at Assets/Scripts/Gamemodes/GamemodeManager.cs:103)
    4. GamemodeManager.Awake () (at Assets/Scripts/Gamemodes/GamemodeManager.cs:56)
    It must be another problem.
     
  48. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    it is another problem. you should check the base triggers and spawn points
     
  49. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    You do not understand me. In small level i have no sync problem ship is where i expected it.
    When the Level goes larger i have problems the ship position is not where i excepted it. I do not write about flags or spawn points i mean the position where i except the ship. I hope this is more clear.
     
  50. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    public class GamePlayGUI : Photon.MonoBehaviour
    {
    public string[] Playerjoin = new string[4];
    public int PlayerConnect = 0;
    void OnPhotonPlayerConnected ()
    {
    StartCoroutine (wait());
    }
    void wait()
    {
    yield return new WaitForSeconds (3.0f);
    Playerjoin [PlayerConnect] = Pname;
    PlayerConnect++;
    photonView.RPC("UpdatePlayer", PhotonTargets.All , Playerjoin ,PlayerConnect);
    }
    }
    .....

    Now problem is that ... the lines are not executing after wait for seconds. any body tell me why this is happening and what i have to do now ??