Search Unity

TowerDefense ToolKit 4

Discussion in 'Assets and Asset Store' started by Song_Tan, Apr 18, 2012.

  1. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    Can anyone inform me how to get the tower build indicator raycasted from the controller instead of the main camera? I've done this in the past, but my solution doesn't work after the updates.
    -Thx
     
  2. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I don't normally deal with controller so I'm not sure how best to approach this. But my take would be you will need a virtual cursor that can be moved around using your D-pad or analogue stick. Then you can simply modify the default code to use the position of the virtual cursor (instead of the mouse cursor) to do the raycast.
     
  3. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    I was editing the BuildManager.cs previously. What would I need to edit now?
     
  4. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm afraid you need more than a edit here. The D-pad driven virtual cursor will need a script of its own. But if you have that covered, what you need to edit is in UIControl.cs, Update(). Just switch the Input.mousePosition to your cursor position. This is assume you are using the latest version of TDTK4.

    BuildManger has been renamed to TowerManager in TDTK4 btw. Are you still using TDTK3?
     
  5. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    Thanks. I'll see what I can figure out.

    No, I've updated to TDTK4. Yeah, I've been messing around with TowerManager.
     
  6. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    I have found a solution for the virtual cursor position. I'm just not sure how to reference the VRInputModule script when replacing Input.mousePosition with m_cursorPos. Maybe you can assist?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. public class VRInputModule : StandaloneInputModule {
    4.     private Vector2 m_cursorPos;
    5.     // Hand this function your fake mouse position (in screen coords)
    6.     public void UpdateCursorPosition( Vector2 a_pos )
    7.     {
    8.         m_cursorPos = a_pos;
    9.     }
    10.     // NEXT TWO FUNCTIONS (GetPointerData/CopyFromTo)
    11.     // are copied from PointerInputModule source, next update for Unity this likely won't be necessary (As Tim C has made them protected as of this morning (my current version is 4.6.0f3), thank you!), if you have a later version, remove these two functions to receive updates!
    12.     protected bool GetPointerData(int id, out PointerEventData data, bool create)
    13.     {
    14.         if (!m_PointerData.TryGetValue(id, out data) && create)
    15.         {
    16.             data = new PointerEventData(eventSystem)
    17.             {
    18.                 pointerId = id,
    19.             };
    20.             m_PointerData.Add(id, data);
    21.             return true;
    22.         }
    23.         return false;
    24.     }
    25.  
    26.     private void CopyFromTo(PointerEventData @from, PointerEventData @to)
    27.     {
    28.         @to.position = @from.position;
    29.         @to.delta = @from.delta;
    30.         @to.scrollDelta = @from.scrollDelta;
    31.         @to.pointerCurrentRaycast = @from.pointerCurrentRaycast;
    32.     }
    33.     // This is the real function we want, the two commented out lines (Input.mousePosition) are replaced with m_cursorPos (our fake mouse position, set with the public function, UpdateCursorPosition)
    34.     private readonly MouseState m_MouseState = new MouseState();
    35.     protected override MouseState GetMousePointerEventData()
    36.     {
    37.         MouseState m = new MouseState();
    38.         // Populate the left button...
    39.         PointerEventData leftData;
    40.         var created = GetPointerData( kMouseLeftId, out leftData, true );
    41.         leftData.Reset();
    42.         if (created)
    43.             leftData.position = m_cursorPos;
    44.             //leftData.position = Input.mousePosition;
    45.         //Vector2 pos = Input.mousePosition;
    46.         Vector2 pos = m_cursorPos;
    47.         leftData.delta = pos - leftData.position;
    48.         leftData.position = pos;
    49.         leftData.scrollDelta = Input.mouseScrollDelta;
    50.         leftData.button = PointerEventData.InputButton.Left;
    51.         eventSystem.RaycastAll(leftData, m_RaycastResultCache);
    52.         var raycast = FindFirstRaycast(m_RaycastResultCache);
    53.         leftData.pointerCurrentRaycast = raycast;
    54.         m_RaycastResultCache.Clear();
    55.         // copy the apropriate data into right and middle slots
    56.         PointerEventData rightData;
    57.         GetPointerData(kMouseRightId, out rightData, true);
    58.         CopyFromTo(leftData, rightData);
    59.         rightData.button = PointerEventData.InputButton.Right;
    60.         PointerEventData middleData;
    61.         GetPointerData(kMouseMiddleId, out middleData, true);
    62.         CopyFromTo(leftData, middleData);
    63.         middleData.button = PointerEventData.InputButton.Middle;
    64.         m_MouseState.SetButtonState(PointerEventData.InputButton.Left, StateForMouseButton(0), leftData);
    65.         m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
    66.         m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
    67.         return m_MouseState;
    68.     }
    69. }
     
  7. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm not entirely sure how that script works to be honest. But if you want to get the value of m_cursorPos, you can change m_cursorPos to a static variable like this:

    private static Vector2 m_cursorPos;

    And add a function to get the value like this:

    public static Vector2 GetCursorPos(){
    return m_cursorPos;
    }

    Then you should be able to retieve the value by calling VRInputModule.GetCursorPos().
     
  8. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    Thank you!
     
  9. Creiz

    Creiz

    Joined:
    Jun 6, 2017
    Posts:
    130
    I'm looking to add a "Dungeon Keeper"-lite feature to my game. Saying, that the player has different rooms that they can place on a grid and the minions will go through them, fighting traps, enemies, etc.

    Since it's basically tower defense but instead of placing "towers" you place "rooms", I was wondering if this asset could reasonably be modified to allow such behaviour?

    Can "creeps" start from one point and then randomly choose branching paths?

    Can we just have a 30x30 grid for example, replace "towers" with "rooms" that creeps would go through?
     
    MaxiMaximaal likes this.
  10. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It's possible however it's probably not going to be straight-forward. First, how do your 'rooms' work exactly? Do them just occupy a single tile like the towers or they occupy an area? If it's the later, then you will have to modify the code for placing stuff on the grid.

    Also the creeps doesn't move randomly, they are specifically code to find the shortest path to the destination. I'm not sure how you make a creep to go to random point on the grid and somehow always go towards the destination. That and how would the creep behave when they reach a room/area.
     
  11. culmastadm

    culmastadm

    Joined:
    Mar 5, 2020
    Posts:
    7
    I just do not see where ammo is set. My towers are running out of ammo, but I can't find where to add more, or which setting controls this. Any ideas?

    BTW, here is the early stages of my game two weeks old right now:
     
  12. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I don't get what ammo you are referring to. I don't think the tower use ammo at all.
     
  13. culmastadm

    culmastadm

    Joined:
    Mar 5, 2020
    Posts:
    7
    That explains why I couldn't find it. :)

    For some reason, the towers stop attacking after a while, but new ones built will attack when the other ones won't.

    Well, that at least eliminates one possibility of a problem.
     
  14. culmastadm

    culmastadm

    Joined:
    Mar 5, 2020
    Posts:
    7

    Ok, now that the ammo thing is out of my head, I saw that they stopped attacking after I use the repair.

    Any thoughts?
     
  15. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Ah. Found a bug. Please change line681 in Unit.cs to hp-=aInfo.damage;. It's the invert sign, causing the repair to 'kill' the tower instead of repairing it.

    Sorry for the trouble.
     
    MaxiMaximaal likes this.
  16. culmastadm

    culmastadm

    Joined:
    Mar 5, 2020
    Posts:
    7

    Cool, I helped find a bug. Thanks for looking into that.
     
  17. Gamingbir

    Gamingbir

    Joined:
    Apr 1, 2014
    Posts:
    195
    Not sure how to use your tool properly. I guess I have to spend a lot more time to understand how it works. If I want to make a tower defence game I want to make something like kingdom rush. I wonder how to change ur prefab for my kind. Then add abilities to towers and all that. I already have art made for it in 2D and 3D.

     
  18. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I would suggest you to first go through the pdf documentation. That should give you a good idea about how the toolkit works and what it can and cannot do. If you want to make a game that look and work exactly like Kingdom Rush, you will need to do a good amount of modification.
     
  19. Niki-4

    Niki-4

    Joined:
    Oct 29, 2017
    Posts:
    21
    Hello songtan) I have some problems:
    1) New towers dont replace old (Example: we have 3 tower 1-2-3 I need to do next: player buys perk and now he have tower 1; next perk - tower 2 replace tower 1; next perk - tower 3 replace tower 2 but at this moment after buy 3rd perk we have 2 tower: towers 2&3)
    How can I fix it?

    2) rules dont work on towers which not replace, only upgrade (example: we have 3 tower 1-2-3 I need to do next: player buys perk 1 and now he have tower 1; perk 2 - tower 1 can be upgrade to tower 2; perk 3 - tower can be upgrade only to tower 3; now if I replace I can upgrade to tower 2 but tower 3 is closed)

    3) Then I set maxx count of towers on map its worked only on towers not on upgrades(

    Can u help me please fix it)
     
  20. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    1. I guess the problem is with your third perk. Have you set to to replace tower-1 or tower-2? I have tried it, by setting first perk to unlock tower-1, second perk unlock tower-2 and replace tower-1, third perk unlock tower-3 and replace tower-2. Works for me. Also make sure you have both tower-2 and tower-3 disabled in TowerManager.

    2. I don't quite follow this one. Are you trying to replace tower? or upgrade? For your information replacing tower doesn't affect a tower availability when it comes to upgrade.

    3. The tower limit inherit from the origin tower. Meaning it only works when you try to build tower, not when you upgrade. I can modify this for you if you want. Please send me an email.
     
  21. Niki-4

    Niki-4

    Joined:
    Oct 29, 2017
    Posts:
    21

    3.It will be greate please! neverovichns@mail.ru

    2. I try to replace upgrade of tower at 1. I try to replace main tower at 2. I try to replace upgrade of tower)

    1. idk why its not work for me I will try it again but its not work in 2 types of towers, Already check TowerManager i will update TDTK and try again
     
  22. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I've just sent you a package containing the modification.

    As for 2, you can't replace the upgrade of a tower atm. Not without a lot of additional code.

    Also, try make sure all your tower prefab has unique prefabID. All of the perks reference to a particular tower is based on prefabID. If you have towers that shares prefabID, you might end up with weird bug like wrong tower being replaced or perks is working on the wrong towers.
     
  23. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    @songtan what you been workin on lately? any new assets or game news?
     
  24. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Thanks for asking. I've been working on a few project but unfortunately all of them are not in a state where I'm ready to talk about yet. Except Element TD 2 of course, which will be out of early access in a few weeks. Frankly it's what I've been spending most of my time on.
     
    culmastadm likes this.
  25. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    Fair enough. I didn't realize Element TD 2 was an early access thing, so of course that's what you've been doing! Hope it's all going well.
     
  26. mhonnibal

    mhonnibal

    Joined:
    Feb 22, 2021
    Posts:
    11
    were you able to fix this?

    Tried all the different colliders prefabbing at a preset height. They all spawn at there centre (using animated pngs).

    can modify the height manualy when you pause but when the mob hits a platform slowly centres and is then under ground rest of combat.

    Tried moving the grid platform and the build area became negated. Any suggestions?
     
    Last edited: Mar 19, 2021
  27. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Are you saying the creep's mesh height changed during runtime? Make sure you don't have unnecessary physic components (collider, rigidbody, etc.) on the prefab.

    By the way, it's never have to do with the grid/build-platform positioning. If you have problem with the y-axis positioning of the creep or tower, it's always the prefab themselves.
     
  28. mhonnibal

    mhonnibal

    Joined:
    Feb 22, 2021
    Posts:
    11
    I tried every collider yesterday. I cant see an option for why they spawn on the y axis at that altitude I even tried using your prefab assets (tanks etc) and taking all the values and applying it to my custom prefab didnt work either.
     
  29. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    Do you have an animator on the prefab? That's a pretty common issue with animation.
     
  30. mhonnibal

    mhonnibal

    Joined:
    Feb 22, 2021
    Posts:
    11
  31. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I pm a step by step to you. You can try place your sprite or mesh under an empty object so you can adjust the position to offset the pivot.
     
  32. ceprowler

    ceprowler

    Joined:
    Oct 24, 2020
    Posts:
    18
    Hello Song,

    In a previous post you mentioned that the shoot points do not assign damage per shoot object / shoot point. Example if my cannon turret has two barrels and two shoot points they would do only one damage calculation. However, I get instances where the damage calculation is based on each shoot object (which I would prefer). I thought it was random however after much testing it is one or two specific towers and the hit per shoot object occurs after I sell or upgrade one of those towers. So what this ends up looking like is my cannon that will shoot twice per cycle only does damage on the 2nd shot until I sell a cannon of a similar type or upgrade it to the level 2 version and then it will do damage per shot. When I upgrade and it has 4 barrels and 4 shoot points each of them will do damage per hit. Honestly I like the effect but I'm trying to figure out how to get all turrets to work the same way or get it to calculate the damage per shoot object / shoot point right from the start. Any insights you might be able to offer would be welcomed. Thank you.
     
  33. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I've tried various combination of upgrading and selling of towers. Nothing seems to break (as in the unit keep doing damage per attack, not shot) as far as I can tell. Perhaps you can let me narrow down the exact step that leads to it so I can replicate it and find out what causes it.

    Anyway, I can do a simple modification so that the damage is calculated on every shoot-object hit. Please send me an email so I can send it to you when it's done.
     
  34. ceprowler

    ceprowler

    Joined:
    Oct 24, 2020
    Posts:
    18
    Message sent and thank you for your help.
     
    mhonnibal likes this.
  35. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, I installed this asset again, after a long time. I updated everything for URP, but now the camera screen looks blue in all scenes, how should I fix this?
     
  36. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm afraid it doesn't work right out of the box for URP. You need to take some additional step to get it working. Change the material, set up how the camera works, etc. There's a step by step in the pdf documentation.
     
    MrZeker likes this.
  37. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    Hi there. I thank you for your very well done asset and I work on a project with it. A Tower Defense mix with RPG element. All work perfect and the asset is really intuitive to use. I could made Game Creator to work alongside as well as Dialogue Manager. But I really need to save player ressource and perk. I knew that there is the TD-save script to use but... I can't find the good way to use it, I'm not a good coder and I wish to ask if someone could share a detailled step by step tutorial to be able to save when leave and load when come back to the game. Where to put the part of the scrip ? on witch element ? how to call it ? Sorry for my "noobish", I want to learn, but there is always a begening.
    Thank in advance !
     

    Attached Files:

  38. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    There isn't a one size fits all solution to saving a loading (so there isn't a standard step by step that you can just use). The script are just an example showing the how you retrieve the key value to save, and put those value back after loading. Depends on how you want to do it exactly, they might not be applicable. You will have to be specific about how your game works, what you want to save/load and how/when you want to do that.
     
  39. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    Ok. well, in fact I only need to save :
    - Resource (I made three different resource with your Rsc manager)
    - Perk that the player bought / Unlock (I made a shop whith specific scene to buy Perk like your exemple)
    - The max level played (imaxlevel)
    the save must be done when player leave the game at the end of the session and could be load when he come back to play.
    I made (also with your asset) some scene between Tower Defense scene, where the player could collect ressource. There is a storyline too. So it's important to give back to the player what he won last time he played.
    :)
     
    Last edited: Apr 6, 2021
  40. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I see. Now the key question is can player save mid-game? If no, then this should be pretty straightforward. You can just use the example script as they come. Just use this line of code to save - TDSave.SaveGeneralProgress(). I would recommend you to call this when the player completes a level, or exit the shop.

    I just noticed a readily available load function in Perkmanager (line 80). It's commented out by default but you can just enable it. This should have the PerkManager load the saved progress.

    For RscManager, you can follow the comment in TDSave.cs. Simply add rscList=TDSave.LoadRsc(); anywhere in Awake() of RscManager.

    Finally, since you have your own scene where the value of resources are modified. You will probably need to save just before you load a new level or quit the game.

    Hope this help.
     
  41. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    Thanks you very much. I'll try that.
     
  42. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    OK, I tried it but I should have done something wrong and now all ressources start to 0 at the begening of each scene... How exactly do you think I should use the line of code TDSave.SaveGeneralProgress() ? And where ? I think this is what I've done wrong.
     
    Last edited: Apr 7, 2021
  43. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Where are you placing that line of code? So just the resources are not working? Are the perks working?

    I would place the TDSave.SaveGeneralProgress() in EndGame() in GameControl.cs, inside the condition playerWon (so it only get called if the player wins).
     
  44. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    regedit.PNG rsc_perk.PNG loaded.PNG I spend hours learning about the data système and I'd learned a lot. Things start to became clearly now. I should have start this way.
    (https://learn.unity.com/tutorial/creating-a-persistent-player-data-system)
    I really want to sucess having a save system in the game with your asset.

    - I made a Debub.log to show when "save general progress" is done, and it work when a level is won, ok for this. Made a debug too when load Rsc and Perk.
    + Key are well created but when I go to check in Regedit, they are all set to 0 after a save.
    = So I guess that the load Rsc work well, but it load all to zero. Now The question is: why it save a zero value instead of the real game value ?
     
    Last edited: Apr 8, 2021
  45. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I would suggest you to put a some debug log in the save function to check what are the value being saved. From there you can slowly work your way to why those value are 0. With that you don't even need regedit to verify what the game has saved.

    Also, where are you calling the save function. This matter, the code saves the cached resource value intended to be carried forth from one scene to another, not the one displayed on screen. And the cached only gets filled up after the game is finished (line104 in GameControl). If you call the save function before that, you always gets a 0.
     
  46. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    reg.PNG I all did well. it's ok to save after level complete. And now I think it's ok to load the ressource. I was about to give up and desperatly move the "rscList = TDSave.LoadRsc();" at the top of Awake instead of some line under the "if", and now I think it's work !
    I'll check if it's work for the perk too and be back . Thx !
     

    Attached Files:

    • save.PNG
      save.PNG
      File size:
      14.6 KB
      Views:
      341
    Last edited: Apr 8, 2021
  47. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    Well. Fine. All seems to work now, I could leave the game, came back, and ressource and perk are saved. It was all about the exact place of the line of code, in my project :
    in RscManager.cs the line rscList = TDSave.LoadRsc(); MUST be JUST under Void Awake
    in GameControl.cs the line TDSave.SaveGeneralProgress(); MUST be after AudioManager.OnPlayerWon(); (line 109 for me but perhaps I insert free line somewhere).
    I had issue until I exactly did that, but now it seems to work fine ! Thanks God and you too SongTan !
     
  48. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Well then. Good job! Glad to have helped.
     
  49. Rage1789

    Rage1789

    Joined:
    Mar 20, 2015
    Posts:
    26
    Hi again. Just some little question today:
    1/ Is there a way to automaticly show the stat of the tower (damage, radius, cooldown...) in the Tower select canvas or either in the description panel ?
    2/ Is there a way to control the speed rate of creep that appear with a Spawner creep type. (by default it's 1 creep/sec. and I would like to slow it by half).
    3/ it's seems that the support tower doesn't work in my project, nothing happen after it appear. But the associate Effect work when I use it, for example, with an ability. So it's not due to the effect. Maybe a miss something with the tower ?
     
  50. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    1. I'm afraid not. The closet thing is GetStatsDescription() in Unit.cs and that only give you damage and cooldown. You can expand that function to include things that you need.

    2. It's actually using the cooldown in attack stats. Which is not ideal as that is also used to track the creep attacks, and it's not exposed in editor, all due to my oversight. I'll fixed it in coming update. For now, you can change that value by enable turret for the creep and adjust the cooldown in attack stats.

    3. There's a bug that cause this. In Unit.cs, line-861. You need to change it to:

    public float GetMulAttackRange_Support(){ return activeEffectMul.stats.attackRange_Support * PerkManager.GetMulUnitAttackRange_Support(prefabID); }