Search Unity

Tanks Multiplayer - complete networking project (Netcode, Photon)

Discussion in 'Assets and Asset Store' started by Baroni, Jun 11, 2016.

?

Which networking solution would you like to use with this asset?

  1. Unity Networking

    136 vote(s)
    51.3%
  2. Photon Networking

    99 vote(s)
    37.4%
  3. Other

    30 vote(s)
    11.3%
  1. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi @dclgamingtr, thanks for your purchase. Please see this guide for instructions on how to add your own tank.
     
  2. Kalle801

    Kalle801

    Joined:
    Mar 3, 2013
    Posts:
    80
    I just purchased this and want to use my own Selfhosted Server (Photon), this seems not to work... The asset reset my settings to "Region based" with photon app id -.-
     
  3. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @Kalle801 the "Online" mode in Tanks Multiplayer is designed to be online via Photon Cloud - Best Region. If you would like to change that, have a look at the StartMatch method in NetworkManagerCustom. You can replace the region override with PhotonNetwork.ConnectUsingSettings or .ConnectToMaster there. For a LAN setup, see here.

    Edit: It's very impolite to not wait for an answer over Christmas (where my reply could take few hours more than usual) and leaving a 1-star review on the store page for this instead.
     
    Last edited: Dec 25, 2017
  4. Kalle801

    Kalle801

    Joined:
    Mar 3, 2013
    Posts:
    80
    Does not work... I try it like this:

    PhotonNetwork.PhotonServerSettings.HostType = ServerSettings.HostingOption.SelfHosted;

    Okay. This seems to work:

    switch (mode)
    {
    //connects to a cloud game available on the Photon servers
    case NetworkMode.Online:
    PhotonNetwork.ConnectUsingSettings(appVersion);
    break;
     
    Last edited: Dec 26, 2017
  5. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Instead of both lines
    Code (csharp):
    1. PhotonNetwork.PhotonServerSettings.HostType = ServerSettings.HostingOption.BestRegion;
    2. PhotonNetwork.ConnectToBestCloudServer(appVersion);
    you would call

    PhotonNetwork.ConnectUsingSettings(appVersion) as I wrote above. ConnectUsingSettings uses your Photon settings configured in Unity, on the PhotonServerSettings file.
     
  6. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    After creating a new project and importing your asset.
    I do network setup for UNET, then if I hit play it works fine.
    But if I try to build and connect to it I get this error:

    host id out of bound id {-1} max id should be greater 0 and less than {2}
    UnityEngine.Networking.NetworkDiscovery:Update()

    Is there a setting I am missing somewhere?
     
  7. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @katasteel try opening all scenes (Intro and Game scene) and just re-save them without doing any changes. If the error still occurs, what Unity version are you using so I can try to reproduce this myself?

    NetworkDiscovery is used only on LAN mode, are you running that by any chance?
     
  8. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    Sorry for the late reply.

    I tried saving the scenes and same error.

    The client gets the error when trying to connect to a UNET LAN game.
    I am using the newest available version 2017.3.0p4.

    The game still does run and connect but gives that error.
     
  9. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hey there, I just bought this a few minutes ago but was not really thinking ahead. I have uMMO which I have been using on my own linux server to host my own game but have been having a lot of issue with it due to lack of support and documentation. Since this uses matchmaking and not a dedicated server, will I be able to adapt your implementation of networking to a client / authoritive server style of usage? I know things with the player and sync vars and such will be useful to actually be able to see them in action and how to use them properly, but I was just wondering about the connection side of things.

    Thanks!
     
  10. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    I was able to reproduce this on a client joining the LAN game running Unity 2017.3.1f1. I'm not entirely sure where the error is raised though, as Unity's NetworkDiscovery script does not contain any logs on this matter. The game runs as expected indeed, so I'm assuming it's safe to ignore for now.

    Hi! This asset makes use of an authoritative instance, where the master client is responsible for registering player damage and game results. Only player movement / input is handled locally. If you mean that you would like to run your own server or do all game calculations 'fully authoritative' on a non-client instance, then that's not really something this asset is designed for - it's meant as a basic, tutorial / learning project for network developers after all. The backend of Unity's matchmaking and relay servers is very much a blackbox, so this might be possible with the LLAPI in some way, but again, not what we're after with the HLAPI approach in this asset.
     
  11. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Well, shoot. : / I was excited, not quite as much anymore. I guess I should not be quite so hasty in my purchases, lol.
     
  12. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    How can I limit the UNET LAN game to 2 players?
    I have tried everything I could think of but it still lets > 2 players join.
     
  13. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @katasteel

    You are defining the maximum players per room in the UNET dashboard, which you can see at 2:14 in our getting started video. This is for online games though.

    If you want to "force" the maximum connection count in a LAN game, try modifying line 112 of NetworkManagerCustom - where it says StartHost(); - to StartHost(singleton.connectionConfig, 2);.
     
  14. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    Changing that line give me this error on the client.

    UNet Client Disconnect Error: CRCMismatch
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
     
  15. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Weird. Another alternative would be to check the current size (active players in the room) of the matchInfo property and not joining it if exceeds your desired room limit.
     
  16. esp_sys

    esp_sys

    Joined:
    Nov 19, 2016
    Posts:
    6
    Hi RebounGames
    I purchased Tanks Multiplayer to see host Migration codes
    But I can't see any code in class "NetworkMigrationManagerCustom" like below
    ========================================================
    namespace TanksMP
    {
    /// <summary>
    /// Custom implementation of the migration manager class.
    /// Not needed when using Photon Networking.
    /// </summary>
    public class NetworkMigrationManagerCustom : MonoBehaviour
    {
    }
    }
    =============================================================
    Can you tell me what is wrong?
     
  17. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
  18. esp_sys

    esp_sys

    Joined:
    Nov 19, 2016
    Posts:
    6
    Thanks for your answer!
    But As for now I don't consider Photon Network for my game.
    I'm develooing a game with Unet
    So I have to condisder situation "Host Migration"
    Can you tell me where I can find HostMigration with Unet on your Tanks Multiplayer?
     
  19. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @esp_sys

    You didn't follow the instructions and imported the UNET package then? The script will be overwritten if you've imported the network package you would like to use. Please set up your project with UNET first, as described in the documentation or in our tutorial video.
     
  20. Koodetat

    Koodetat

    Joined:
    Apr 11, 2014
    Posts:
    14
    @Baroni
    Hello, do you plan an update of your script in the near future? He is fantastic, thank you for your work.
     
  21. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi @Doud8ou, what script or update do you mean? An update for what exactly?
     
  22. Koodetat

    Koodetat

    Joined:
    Apr 11, 2014
    Posts:
    14
  23. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Yes it is @Doud8ou, although there is no time frame for specific features, or fixed update dates, and features on the roadmap can change too. Tanks Multiplayer is designed as a networking learning project / template, and as such, it is considered feature-complete already. New features take time to integrate, and may or may not come, because we carefully integrate them to not interfere with the learning aspect or bloating it up code-wise.
     
  24. Koodetat

    Koodetat

    Joined:
    Apr 11, 2014
    Posts:
    14
    @Baroni Thanks a lot for your answer.
     
    Last edited: May 17, 2018
  25. pRob3

    pRob3

    Joined:
    Oct 13, 2017
    Posts:
    3
    Just bought the asset and it has a few bugs.
    Using version 2018.1.6f1 and get this error if the server player is killing the local player:

    UnassignedReferenceException: The variable killedBy of Player has not been assigned.
    You probably need to assign the killedBy variable of the Player script in the inspector.
     
  26. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hello @pRob3, could you please post the full error message with the script and code line this is coming from? I don't have Unity 2018.1.6f1 installed currently, but I can try to reproduce this with 2018.1.0.
     
  27. pRob3

    pRob3

    Joined:
    Oct 13, 2017
    Posts:
    3
    UnassignedReferenceException: The variable killedBy of Player has not been assigned.
    You probably need to assign the killedBy variable of the Player script in the inspector.
    TanksMP.Player.RpcRespawn () (at Assets/TanksMultiplayer/Scripts/Player.cs:533)
    TanksMP.Player.InvokeRpcRpcRespawn (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
    UnityEngine.Networking.NetworkIdentity.HandleRPC (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:657)
    UnityEngine.Networking.ClientScene.OnRPCMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:746)
    UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:468)
    UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:424)
    UnityEngine.Networking.NetworkConnection.TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:575)
    UnityEngine.Networking.NetworkClient.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:753)
    UnityEngine.Networking.NetworkClient.UpdateClients () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:965)
    UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1088)

    And the line in Player.cs
    Code (CSharp):
    1.  
    2. //local player was killed, set camera to follow the killer
    3. camFollow.target = killedBy.transform;
    4.  
    the killedBy is null.
     
  28. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi again @pRob3,

    I have found the issue and it is more complicated than I thought. The cause is that the Bullet > OnTriggerEnter method is not called on the local client at all, since the server already despawned the bullet on its server-side collision. I have to make a few adjustments to the Bullet and Player script for this, but for now, you can also do a quick fix by adding the [SyncVar] attribute to the killedBy variable, Player script line 139.
     
  29. kraaazy

    kraaazy

    Joined:
    Dec 31, 2015
    Posts:
    20
    Hello!

    I noticed host migration does not work for Unet when doing anything besides LAN. Is there any way to get this to work or is it simply not possible?

    Does host migration work in this project if I use the Photon settings?
     
  30. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi @krehzzzy, thanks for your question.

    Unity did not implement host migration for UNET on their relay servers, so there is nothing we could do to add it. See this page at the bottom:

    "This works for direct connection games. Additional work is required for this to function with the matchmaker and relay server."

    Honestly I don't think this will ever be supported with UNET. Photon does support host migration on their servers and our asset implements that as well.
     
  31. Barritico

    Barritico

    Joined:
    Jun 9, 2017
    Posts:
    374
    Hi.

    I have tested the asset in your web player. I have opened two sessions in my browser with two different users. They have entered the server correctly.

    However, the movements are not synchronized. There is an important difference between the movements made by the "origin" and what reaches the "destination".

    Is this a mismatch of the webplayer?
     
  32. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi @Barritico,

    sorry for the delay as I'm currently moving houses without internet access.

    The web player has been built using Photon, automatically choosing US servers (on auto matchmaking). So while there will always be some lag over the network, which also depends on your location and internet connection, it should not be "that" visible since movement positions are being updated 10 times per second in our asset. I haven't noticed an extreme lag spike or completely different player positions in my test runs.
     
  33. Barritico

    Barritico

    Joined:
    Jun 9, 2017
    Posts:
    374

    Thank you.
     
  34. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Hello. I was wondering how I could make the camera follow the tank turret like if I parent it to the turret. Or simply how i could parent the camera to the tank turret. Thank you.
     
    Last edited: Nov 4, 2018
  35. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi @Claudiotheworm, you have two options for this:

    1. You rewrite the FollowTarget script. This is the camera script which follows the player around. Instead of doing that, you could have it parent itself to the player once it exists in the scene. You can access the player object via GameManager.localPlayer.

    2. You parent the camera in the player prefab. This spawns the player with the camera attached already, but you would need to add 1-2 lines of code to disable the camera on other player instances, so you do not end up with 12 cameras in the scene.
     
  36. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Thank you! Just 2 questions.#1 what two lines of code would stop 12 cameras spawning, and #2, in which script would I use the Gamemanager.localplayer? I'm using photon.
     
  37. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    The camera is not spawned - it already exists in the Game scene. With the two lines of code I am referring to the general Unity Transform parent variable which you would use for this.

    The GameManager script has a localPlayer variable when using Photon too. It is accessible via GameManager.GetInstance().localPlayer. You could either rewrite the FollowTarget script, write your own or change the camera parent in the Player script directly - it's your decision where you want it, really ;)
     
  38. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Thank you! I'll try that.
     
  39. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Hello! I'm currently adding two different modes to the game. I am wondering how I could make two different games a person could join.
    Code (CSharp):
    1. If(playerprefs.getint("Mode") == 1) {
    2. //go to game A
    3. }
    4.  
    Code (CSharp):
    1. If(playerprefs.getint("Mode") == 2) {
    2. //go to game B
    3. }
    I need to know how to send them to different games.
    I'm using photon.
    Edit: would it work if I simply duplicated the game scene and sent you to different scenes? And how would I do this if so?
     
  40. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hi again @Claudiotheworm,

    there is a major difference in the implementation. If you just want the master client to select a different scene when creating a new room, then have a look at this thread. Other clients would then join the same room automatically.

    However, if you would like to provide players the option for choosing the level or mode they want to play, this requires slight adjustments to the matchmaking logic:

    For adding a map filtering option, you would need to modify the CreateRoom and JoinRandomRoom methods.
    - you can find the CreateRoom call in the OnPhotonRandomJoinFailed method of NetworkManagerCustom. In its RoomOptions, you need to define the map as an additional argument as CustomRoomProperties (e.g. a string / level name or int / scene index as value).
    - next, locate the JoinRandomRoom call in the OnConnectedToMaster method of NetworkManagerCustom. In its parameters, define the expectedCustomRoomProperties with the desired map value.

    Please refer to the official Photon documentation on how to use these parameters in detail (search for the word "map"):
    https://doc.photonengine.com/en-us/realtime/current/reference/matchmaking-and-lobby
     
  41. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Thank you! I'll tell you if I run into any issues. Thank you for your help!!!
     
  42. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    I'm not a master coder, but what do I do here:
    Code (CSharp):
    1.  PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = (byte)this.maxPlayers }, null);
    Edit: I tried copying and pasting the Photon Documentation code but ended up with errors.
     
    Last edited: Nov 10, 2018
  43. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Wait, never mind, I think I got it fixed.
     
  44. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    I have a issue. I did what you said to do but when Player A creates a room, Player B does not detect that room, and creates their own room. Here's my code...
    Code (CSharp):
    1.         /// <summary>
    2.         /// Called after the connection to the master is established.
    3.         /// See the official Photon docs for more details.
    4.         /// </summary>
    5.         public override void OnConnectedToMaster()
    6.         {
    7.             //set my own name and try joining a game
    8.             PhotonNetwork.playerName = PlayerPrefs.GetString(PrefsKeys.playerName);
    9.             Hashtable expectedCustomRoomProperties = new Hashtable() { { "map", 1 } };
    10.             PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, (byte)this.maxPlayers);
    11.         }
    12.  
    13.  
    14.         /// <summary>
    15.         /// Called when a joining a random room failed.
    16.         /// See the official Photon docs for more details.
    17.         /// </summary>
    18.         public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
    19.         {
    20.             Debug.Log("Photon did not find any matches on the Master Client we are connected to. Creating our own room... (ignore the warning above).");
    21.             RoomOptions roomOptions = new RoomOptions();
    22.          
    23.             roomOptions.CustomRoomProperties = new Hashtable() { { "map", 1 } };
    24.             roomOptions.MaxPlayers = (byte)this.maxPlayers;
    25.             //joining failed so try to create our own room
    26.             PhotonNetwork.CreateRoom(null, roomOptions, null);
    27.         }
    28.  
    29.  
    Is there something I did wrong?
    Edit: When I get rid of the expectedroomproperties, It works but there is no filter. Please help.
     
    Last edited: Nov 10, 2018
  45. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @Claudiotheworm, please do not spam posts and create duplicates for the same issue. We, nor Photon will reply to your posts on weekends. We are game developers but we do have a private life as well.

    You are missing the CustomRoomPropertiesForLobby property as shown in the following thread, on the Photon forums:
    https://forum.photonengine.com/disc...m-room-properties-doesnt-appear-to-be-working
    https://doc.photonengine.com/en-us/pun/v2/lobby-and-matchmaking/matchmaking-and-lobby

    Your code would look like this.
    Code (csharp):
    1. RoomOptions roomOptions = new RoomOptions();
    2. roomOptions.CustomRoomPropertiesForLobby = new string[] { "map" };
    3. roomOptions.CustomRoomProperties = new Hashtable() { { "map", "1" } };
    4. ...
    Note that in Photon's scripting reference, it is mentioned that custom room property values should be of type string (in both hashtables). The rest of it looks fine to me.
     
    Last edited: Nov 11, 2018
  46. electricpunch

    electricpunch

    Joined:
    Feb 18, 2014
    Posts:
    22
    Hello, can you please help with making Lobby system? I already made all UI to create and join room, but can't edit your scripts. Is it possible to add lobby without huge code editing? I want to save the scene index when player create the room and give ability for other players to choose the room for connection, not random room.
     
    Last edited: Nov 15, 2018
  47. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    @electricpunch Please do not share snippets of our source code, even if modified, in a public forum like this - please remove the code you posted. If you have code related issues or questions contact us via email instead.
     
  48. electricpunch

    electricpunch

    Joined:
    Feb 18, 2014
    Posts:
    22
    I edited the post (sorry for this) and wrote to your emal. Thanks!
     
  49. Claudiotheworm

    Claudiotheworm

    Joined:
    Jul 27, 2018
    Posts:
    30
    Hello again. I'm making a bot tank (in online mode) spawning system and when I spawn it in offline mode, the bot tank works fine.
    In online mode however, the bot tank bugs out and doesn't work anymore. By that, I mean it doesn't have any health and you can't even hit it.
    Code (CSharp):
    1. void SpawnTurret()
    2.         {
    3.             Debug.Log("TurretSpawned");
    4.             GameObject obj = PhotonNetwork.Instantiate("Turret1", this.transform.position, Quaternion.identity, 0);
    5.  
    6.             //let the local host determine the team assignment
    7.             Player p = obj.GetComponent<Player>();
    8.             p.GetView().SetTeam(0);
    9.          
    10.          
    11.         }
    And:
    Code (CSharp):
    1. if(Input.GetKey("k"))
    2.             {
    3.                 SpawnTurret();
    4.             }
    This is in the Player script.
     
    Last edited: Nov 16, 2018
  50. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,267
    Hey @Claudiotheworm,

    in the Player script, Awake method, only the master client is allowed to set the health of players. You can track this down to the PlayerExtensions.SetHealth method. If your turrets have the Player script attached to them, the output of this method is actually the same both offline and online. So I would try to check whether the method is actually being called on your side.

    Regarding "can't even hit it", please double check the colliders on the object as well as the team assignment. If the object is in the same team as you, you can't hit it - since friendly fire is disabled.