Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Tower Defense Multiplayer - complete networking project (Netcode)

Discussion in 'Assets and Asset Store' started by Baroni, Jun 19, 2023.

  1. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Interested or looking for support? Ask me anything in this thread - from developer to developer :)

    Tower Defense Multiplayer is a new take on the popular tower defense genre, in the form of a co-op multiplayer template using Unity Netcode for GameObjects. By default, it allows up to 8 players per room, to shoot down waves of incoming enemies and compete for the highest score. It features player class selection, tower placement and shooting, upgrades, defense points with health, networked object pooling, several Unity services and more. This template is laying the ground for a successful multiplayer game, by demonstrating common network tasks such as connecting players to a lobby, fully authoritative server actions, commands & RPCs, synced variables / object states, but also monetizing players using in-app purchases and video ads to keep your servers running. I’ve included an in-depth documentation about all approaches I took while building this and commented each and every method, so you can follow along easily.

    Features:

    ✅ Online Matchmaking & Player Lobby
    ✅ Supports Local Area Network (LAN) game discovery
    ✅ Supports Offline mode (solo)
    ✅ Class Selection: players choose a tower class in the lobby before they ready up
    Synchronized Player Properties: name, class, color, score, currency balance
    Authoritative Player Actions: tower placement, upgrade or sell own towers
    ✅ Authoritative Defense Points: server calculates damage and remaining health
    ✅ Shared Towers: upgrade each others towers using your own currency balance
    Networked Object Pooling: synced client pools for enemy units & projectiles
    Unit Movement: making use of Unity Splines, C# Jobs System and Burst
    Minimap: updates and displays unit positions periodically in a RenderTexture
    Reusable Manager Scripts: audio, pools, Unity IAP, Unity Ads
    Ads: display video ads between waves (playing solo) or on game end
    In-App Purchases: buy different tower classes (visual or game impact)
    ✅ Unity UI: menus, wave status, player scores, mobile scroll
    ✅ Input for Mobile (touch) & Standalone (keyboard + mouse)
    Documentation: scripting reference & step by step guide

    (Optional) Use of Unity Services:
    • Unity Ads
    • Unity IAP
    upload_2023-8-29_12-0-59.png

    Scripting Reference and Guides:
    Get it here:
    Try it out:
     
    Last edited: Aug 29, 2023
    toddkc and BetaMark like this.
  2. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    229
    Interested in seeing what you've built here. I'm a Mirror guy right now and I've evaluated a few other net stacks, but I'm very interested to see how your template works with the new NetCode for GameObjects.

    A couple of quick questions that I didn't see answered in your checklist:
    * Will the template include 2d and 3d examples?
    * Are matches able to be played across client types (e.g. one player with webGL, another with Windows, and another on mobile iOS/Android)?
    * How does this template handle auth?
    * Is there Steam Auth? Google auth? Apple Auth?
    * Does this template handle persistence across matches?
    * Does the matchmaking do any MMR for players?
    * What unity version is the baseline for this template?


    Optionally, do you have a low poly kits you recommend I purchase to use with the kit?
     
  3. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Thanks for the questions @BetaMark! Since this is a new asset, of course I am very interested to hear what you expect from it.

    The asset will include two 3D maps, one with tower placement grids only available to a specific player (+multiple defense points), and one with shared placement grids where all players can place their towers (+one shared defense point).

    In the Netcode for GO version, online cross-play is supported and done using Unity Relay. In LAN that works without Relay, but firewalls need to be disabled and a desktop client needs to host the game.

    Authentication has not been a focus of the asset (yet) due to no current features requiring it. Unity Relay and Lobby use anonymous authentication at this point.

    No, since nothing needs to be persisted currently. Matches are real-time sessions, which are "destroyed" if the host or all players leave. In this context, note that host migration with Unity Netcode is also not supported in this asset, but featured in my other asset, Tanks Multiplayer.

    There is no MMR, but that could be easily added in Unity Lobby as an additional filter criteria when searching for open matches. However the question is if you really want to have that, since players do not play against each other, but together, where MMR would not matter that much I guess.

    The asset will be released using Unity 2022.3.0 (LTS) due to Unity Transport requiring at least Unity 2022.2.

    Honestly I have not looking into existing 3D models that much... I am currently working on creating some for demonstration purposes myself, which go in a similar military direction as this or that one.

    Thanks again!
     
    BetaMark likes this.
  4. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    The asset has been submitted to the Asset Store for review!

    Asset Store - Tower Defense Multiplayer - Unity_007.png Asset Store - Tower Defense Multiplayer - Unity_010.png Asset Store - Tower Defense Multiplayer - Unity_014.png

    Documentation and a WebPlayer will be available on release. In the meantime, let me know if you have any questions. Thanks!
     
    BetaMark likes this.
  5. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Tower Defense Multiplayer has been released!

    It is not visible on the Unity Asset Store just yet, but will probably be in a few hours. Please see the first post for a direct link to the asset (that works). Thank you!
     
    BetaMark likes this.
  6. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    please create a discord
     
  7. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    I do not provide real-time support. Please use this public thread for free support.
     
  8. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    in the demo i have everything setup. but can not place down turrets at all. everything else is setup and working for the unity multiplayer and i have populated all slots in the scripts folder in heirarchy
     
  9. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Any errors or logs in the Console?

    I do not know what you mean by "slots in the scripts folder in Hierarchy". Could you please send a screenshot?
     
  10. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    no

    i have connected all "reference objects"
     
  11. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Unfortunately I still do not know what you mean by that. Do you mean ScriptableObject references? Like I wrote in my last reply, could you please attach a screenshot for clarification?

    Without errors or further details it is impossible for me to find the cause of the problem you are experiencing. I can offer trying to reproduce this by installing your Unity version with all your PackageManager dependencies and import the asset in that environment myself. For that, please tell me your Unity version and post the contents of your <Unity Tower Defense project folder>/Packages/manifest.json file here.
     
  12. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    im using 2022.3.10f1 in urp. its a fresh install to test. so whatever comes out of the box with unity and your asset. nothign else on it. i have it all setup. just when i go to place a turret. cant drag or drop one,, and also occasionally get the following "
    Failed to create UdpClient at port 7787: Only one usage of each socket address (protocol/network address/port) is normally permitted.
    "
     

    Attached Files:

    • 1.png
      1.png
      File size:
      75.8 KB
      Views:
      38
    Last edited: Oct 30, 2023
  13. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    @xmonster0 Thank you for your additional details and screenshot. I think I have found the cause - in a new project, the layer settings have not been taken over, even if they are correct in my exported project. Please open the Layer settings (top right -> Edit Layers) and make sure they are set like this, going from 10 to 15.

    I really have no clue why a new project has the old layout of layers I had months ago... will have to figure that out and release an update. Hope that fixes it on your end as well, thank you for your patience!
     
    PixelPirates likes this.
  14. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    That was it! thanks!
     
  15. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    i also get this error alot

    Failed to create UdpClient at port 7787: Only one usage of each socket address (protocol/network address/port) is normally permitted.

    UnityEngine.Debug:LogError (object)
    TowerDefenseMP.NetworkDiscovery:Start () (at Assets/TowerDefenseMultiplayer/Scripts/NetworkDiscovery.cs:74)

    and on map 2 when 2 players join they are both assigned the same playfield
     
  16. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    This is a Unity Netcode error that tells you that you cannot be the host and client on the same pc. This also occurs if you are restarting the match too quickly.

    This issue is the same as above. If you are the host and client on one pc, it counts as one connection which cannot be distinguished. I will try to work around this in an update, but if you would do a real-world test you would need to play the game on two separate devices.
     
  17. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    We use version control and the issue came up testing with 2 seperate pc's. About a ten hour drive apart
     
  18. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    @xmonster0 Really? That's weird, as I have tested that map with two separate pc's as well and it was fine. Thanks for the report, going to try and reproduce / fix this for the upcoming update too.
     
  19. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Really good template, something is wrong with it tho, I have tried multible times to create a new project and import this (and importing everything that is listed in the well written documentation) and I can connect etc as I should without any problems but I can not place any towers on any grid. I am running Unity 2022.3.12f1 (LTS) and I have a 3d URP Core. Any idea why I can not place any towers? I doesnt indicate on the grid at all (as it does in the web build that I tested before purchasing the asset)
     
  20. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Hi @PixelPirates , thanks for posting! The issue is caused by a wrong Layer setup - an update has already been submitted to the store. But you can correct this within a minute yourself, please see this post above.
     
    PixelPirates likes this.
  21. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Thank you for the fast reply, that did it :)
     
  22. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    I did find another issue, in "Map_UI > Canvas > Panels > Panel - Info" it shows "Player 1" info for all, it should be that all players can see their own currency, score and health.


    In this print The Editor have 50 coins but in the UI in the GameView it says it has 10 (as Player 1 have)
     
  23. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Thanks @PixelPirates, looking into this. I have fixed a similar issue before releasing the asset, will have to try and reproduce it again. It could be that it only occurs when running multiple instances on the same machine, or with a different Network Transport version. Investigating and will report back very soon.
     
    PixelPirates likes this.
  24. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    I asked a friend to join in to try remote multiplayer and we had the same issue even tho we are on different networks so thats not the issue here.

    Edit: The project in the print is without any edits to the asset, I have a project just to reflect back on if something is behaving wrong in my project I can go back to the base asset to see if there is a problem with my edits or in the asset.

    Edit: All players also spawn in as player 1 on the spawn location.
     
    Last edited: Nov 14, 2023
  25. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    @PixelPirates Thanks for the confirmation. I have resubmitted version 1.0.1 which now includes fixes to the issues you have mentioned as well. It is live via GitHub access now and probably somewhen next week on the Unity Asset Store.

    v1.0.1
    - fixed wrong Layer settings in TagManager breaking tower placement
    - fixed bug spawning all players on same location due to incorrect local client ID
    - fixed bug displaying player 1 stats to all players due to incorrect local client ID
    - fixed hiding mobile scroll indicators on WebGL build
     
    PixelPirates likes this.
  26. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Do you have any ETA on when the rest of the documentation will be added? It says "Documentation for this page will follow in the upcoming 1-2 weeks" but last update was on oct 27 so it has been more then 2 weeks.
    I have ran into a problem where the upgrade panel isnt showed when clicking on a built tower but I cant figure out why and what scripts that actually relate to this.
     
  27. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    I have planned to finish it this week (working on it right now). It was scheduled for earlier this week, but releasing version 1.0.1 was more important.

    Please note that the "Feature" section in the documentation is more a general description of a feature and what it does, rather than how it works (due to public documentation). The components involved and workflow across scripts will be mentioned though.

    As a first stop, you will want to check the Update() function in UIGame.cs, at the bottom where RaycastTower() is called and verify that the raycast actually hits something. If that is not the case, check that the Tower has a collider on the "Tower" layer.
     
    PixelPirates likes this.
  28. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Thanks for the help, my miss was that I had named the "Tower" layer "Towers" but I found the error thanks to your description. Just adding this comment if someone else in the future does the same misstake as me :)
     
    Baroni likes this.
  29. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    I have made an effect for selected tower but something is not working with the poolmanager and I am not sure what is wrong.

    In the ShowTowerUpgrade() I have the following line:
    PoolManager.Spawn(selectedFX, trans.position, Quaternion.identity);

    and in the Upgrade, Sell & CancelAction() I have the following line:
    PoolManager.Despawn(selectedFX);

    and I have added the prefab in the PooManager as a Pool:


    But I still get error:
    "PoolManager couldn't find Pool for instance: Selected FX"
    &
    "NullReferenceException: Object reference not set to an instance of an object
    TowerDefenseMP.PoolManager.Despawn"
    but the effect is Spawned as it should but when I "deselect" the tower the effect is not deactivated.
     
  30. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Is the gameobject value you are passing in the same in both methods? Because then it won't work. Spawn is expecting a prefab, while Despawn expects an instance as the method parameters tell:

    Spawn (GameObject prefab, Vector3 position, Quaternion rotation)
    Despawn (GameObject instance, float time=0f)

    Since your prefab is not an instance of a pool - it's a prefab. You would want to cache the gameobject instance returned from Spawn() and pass that into Despawn().
     
    PixelPirates likes this.
  31. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Silly me, thanks :)
     
  32. PixelPirates

    PixelPirates

    Joined:
    Dec 29, 2021
    Posts:
    9
    Some functionalities I would like to see in future upgrades (if you want to improve the functionalities):
    - A way to set available classes depending on map chosen in lobby (and even change max players for lobby depending on map).
    - Tower effects (slow and aoe attacks etc).
     
  33. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,258
    Hi @PixelPirates, I was on vacation the last week without access to internet, so was unable to respond. Thanks for your post.

    Also answering your PM: while I have a small list of features I would like to implement, there is no timeframe to do so. Usually, my assets receive 1-2 feature updates per year, 2-3 compatibility updates per year, and as many bug fix updates they need. Prioritization depends on popularity of the asset and I can tell that for this asset, it has not gathered enough market interest yet that would justify a bigger focus (unfortunately).

    I would split this into two feature requests. The first is a very specific request that might not be a good fit for every developer - allowing to disable classes for a map, potentially a class a player has bought, could create acceptance issues. Also, I have the feeling that the ScriptableObject system required to set up classes is complex already - if you have a need for this, I would be more than happy to give you input on how I would tackle this.

    Having different max players per map is something that has been on my list, but I was unsure how to handle cases where the master user selects a map that has a lower player count that the current player count in the lobby. I.e. should the players joined last get kicked out of the lobby then? Additionally I am not sure if Unity Lobby really allows for flexible max players, or if this is something that needs to be checked on the master client in code only. I have put this back on the list.

    Slow is on the list, but has not made it for the release due to the way the "Unit reached end of path" logic currently works. Simply speaking, this is a fixed time value in seconds, since Unity Splines did not allow calculating it for the current Unit position on the path. When slow is applied, this time value should obviously change.
    Implementing AOE damage sounds easy too, however it really isn't: since Units do not use colliders or any physics for maximum performance, projectiles cannot do a SphereCast or something like that to detect Units nearby. I will have to think about this and maybe require adding Colliders for specific purposes like this one.
     
    PixelPirates likes this.