Search Unity

TowerDefense ToolKit 4

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

  1. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I think has nothing to do with the orthographic view. Whenever you click on a build point, the system will run a check to see the build point is clear for building. One of the criteria is that it's free of obstacle. My guess of what happen is when you place the platform at y<2.2, the code detected the terrain collider and deem the build point invalid. So the build menu is not showing up. What you should do is set the layer on the terrain object to terrain (layer31). Then the system will recognize that it's a terrain and ignore it in the check.
     
  2. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    Ah good idea. So I assume if I want to have some kind of model to show it's an open spot to build a tower on, like a flag or something, I should set that in the same layer so the system doesn't think it's blocking the build area. I would remove the flag before the tower is placed. Guessing I'll have to dig into the code to do that. I could make then flag a child of the slot and then find it by name and hide it.
     
  3. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Yes you will have too. Let me know if you need any pointer. :)
     
  4. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    Im trying to integrate easy save 2.
    it will save everything about a prefabs location etc
    do you have lists of prefabs on the scene that i can access (re:towers)
    here is easy saves example code
    im guessing your list are unitTowers...its giving me issues
    or is there a built in option to do this (commented out mabey)


    Code (CSharp):
    1. // A List which we'll add any created prefabs to.
    2.     private List<GameObject> createdPrefabs = new List<GameObject>();
    3. //i should change this out for your list
    4.  
    5. void OnApplicationQuit()
    6.     {
    7.         // First, we save the length of the createdPrefabs list so we know how
    8.         // many prefabs we need to load when we restart the application.
    9.         ES2.Save(createdPrefabs.Count, filename+"?tag=prefabCount");
    10.  
    11.         // Now we iterate through our prefab list and save each one seperately,
    12.         // using it's position in the array as the tag.
    13.         for(int i=0; i < createdPrefabs.Count; i++)
    14.             SavePrefab( createdPrefabs[i], i );
    15.     }
    16.  
    17. /*
    18.     * This is where we initialize our prefabs.
    19.     */
    20.     void Start ()
    21.     {
    22.         // If there are saved prefabs to load, load them.
    23.         if(ES2.Exists(filename))
    24.             LoadAllPrefabs();
    25.     }
    26.  
    27.     /*
    28.     * This method will load all of the saved prefabs when called.
    29.     */
    30.     void LoadAllPrefabs()
    31.     {
    32.         // Load our prefab count so we know how many prefabs to load.
    33.         int prefabCount = ES2.Load<int>(filename+"?tag=prefabCount");
    34.         // Load each prefab using a for loop.
    35.         for(int i=0; i < prefabCount; i++)
    36.             LoadPrefab(i);
    37.     }
    38.  
    39.     /*
    40.     * Loads the prefab specified by the tag number
    41.     * supplied as a parameter.
    42.     */
    43.  
    44.  
     
  5. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    The code that you are looking for is this:
    Code (csharp):
    1. List<UnitTower> towerList=TowerManager.GetActiveTowerList();
    2. for(int i=0; i<towerList.Count; i++) createdPrefabs.Add(towerList[i].gameOject);
    Just place this at the start of OnApplicationQuit().
     
    wethecom likes this.
  6. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    I've created a new tower based off the laser tower. I made it available to be placed on a platform. When I click the platform I see the tower in the list. When I click it I get the error below. The tower has a prefab and has the UnitTower script on it so not sure why it's been destroyed. I would think that would be enough to create a new instance of the prefab and place it on the platform. Any ideas?

    MissingReferenceException: The object of type 'UnitTower' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    TDTK.TowerManager.BuildTower (TDTK.UnitTower prefab, TDTK.BuildPlatform platform, Int32 nodeID, Boolean useRsc) (at Assets/TDTK/Scripts/TowerManager.cs:380)
    TDTK.UIBuildButton.OnBuildButton (UnityEngine.GameObject butObj, Int32 pointerID) (at Assets/TDTK/Scripts/UI/UIBuildButton.cs:115)
    TDTK.UIItemCallback.OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/TDTK/Scripts/UI/UIClass.cs:192)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerDownHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:36)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerDownHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()
     
  7. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It's a very unusual error to be honest. I'm not sure why it happen. Have you modified the code in anyway? Do you see any other error/warning message in the console?
     
  8. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    Got it working after a little messing around. Not 100% sure what it was.
     
  9. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    so i have this unique question
    lets say instantiate a creep manually
    how would i go about initiating them to do their thing?...
    i want to override remove the spawner an initiate 6 custom creeps and have no waves...just a one shot deal
    when i place them on the map they dont do anything
    i figure you knowing this so well could fast track me to figuring it out
     
  10. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    You can refer to the function SpawnUnit() in SpawnManager. You can spawn any unit by calling that function and pass the creep prefab and path you want the creep to follow. Since waveIdx is the value use to determine which wave the unit belong to, you can set it to -1 in this case. You will also have to modify the code handling unit destroyed event, _CreepDestroyed(). That means checking for the unit waveIdx value. It it's -1, then it's your custom unit so don't proceed with the rest of the code.

    That is everything I can think of on top of my head. There maybe more things you need to modify.
     
  11. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    thanks ..this sounds like fun
     
  12. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    got it to spawn and towers target it and it moves like it should
    now the killing it is an issue
    any ideas
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TDTK;
    5. public class BaseAttack : MonoBehaviour {
    6.     public GameObject MySpawnManager;
    7.     public UnitCreep MyCreep;
    8.     private UnitCreep creepInstance;
    9.     public Path MyPath;
    10.     // Use this for initialization
    11.     void Start () {
    12.         creepInstance= MySpawnManager.GetComponent<SpawnManager> ().SpawnUnit (MyCreep,-1,MyPath,null );
    13.         SpawnManager.AddActiveUnit(creepInstance);
    14.  
    15.     //    MySpawnManager.GetComponent<SpawnManager> ().SpawnUnit (MyCreep,-1,MyPath,null );
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.         Debug.Log("hp:" + creepInstance.hp.ToString());
    21.         if(creepInstance.hp<=0){
    22.             //unit.Destroyed();
    23.             creepInstance.Destroyed(true);
    24.             //Debug.Log("before");
    25.             //Unit.Destroy (creepInstance);
    26.             //SpawnManager.Destroyed (true);
    27.             //SpawnManager.RemoveActiveUnit(creepInstance);
    28.             Debug.Log("creep destroyed");
    29.             //SpawnManager.DestroyImmediate (creepInstance);
    30.             //kablow boom bang detroy and do something
    31.  
    32.         }
    33.     }
    34. }
    35.  
     
    Last edited: Oct 30, 2017
  13. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    What do you mean? If the tower can target it, means it can be killed just like any other creep.
     
  14. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    the creep goes to 0 hp or less and sits stale in the scene
    you mentioned somthing about "You will also have to modify the code handling unit destroyed event, _CreepDestroyed()."
    if i just destroy the game object will everything be fine?...i think there is a proper process to remove so it cleans up lists signals sounds of destruction etc...
    you can make a empty game object and put this in a scene and assign the variables creep and path then build a few laser towers to see what i mean
     
  15. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Do you see any error message in the console log when your creep got destroyed? If you haven't modify _CreepDestroyed(), you should get some error. That is what prevent your creep from destroying itself.

    What I meant in my earlier post is that in _CreepDestroyed(), waveIdx assigned to the creep will be used to identified which wave the creep is from. Since you use -1, that will throw an error since there are no wave -1. Look in the function yourself and you will understand. What you need to do is add a line if(creep.waveIdx<0) return; in the function. Right after the first line RemoveActiveUnit(creep);.
     
    wethecom likes this.
  16. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    that did it ..i wouldn't have figured that out..thanks
    for other people's reference here is what i did
    in SpawnManager.cs line 328 add if(creep.waveIdx<0) return;

     
  17. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    so i installed Inventory Pro(IP) into my game project and got it running
    it automatically scans and assigns cameras. IP is pretty complex.
    it breaks your interface sort of...you can no longer drag navigate thru the scene and when you try and place a tower and every place is a invalid position
    pretty sure this is a ray casting issue...what script in tdtk should i be looking to resolve this?..im guessing ill have to hard code in a camera into your script
     
  18. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    ok i think i got this...it is the event system related
    2 event systems are on the scene
    the ui are in 2 location
    got rid of one and it makes tdtk work again but not inventory pro
    im doing a event system tutorial i think canvases and event sytems is giving me the hell and combining the ui together will fix all this...im out ...tata
     
  19. ocimpean

    ocimpean

    Joined:
    Aug 10, 2013
    Posts:
    128
    Does the new v.4 release include save and load?
     
  20. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    @ocimpean, not really. But there's some example code included to get you started.
     
  21. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    when you select a tower build button it instantiates a tower under the cursor to place on the grid.
    what script handles that?..i need to massage the function to work from another UI
     
  22. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It's in UIBuildButton.cs, OnBuildButton(). What you are looking for is in the last part of the function, for drag-n-drop mode.
     
  23. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
  24. uB1Ca

    uB1Ca

    Joined:
    Jul 25, 2013
    Posts:
    2



    Hello.

    I have a little problem. when I'm traying on first photo to accese tower build platform on that "transform "Y"" ore less number it's not working and when I set "transform position Y" like on photo 2 it's accesable now. and problem is on photo 3 there is a empty space from terrain and tower. can't fix it. if i set "transform Y" to "0" i can't accese tower build
     
  25. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Does your terrain has a collider component. Or is there any other collider in the scene that occupying the space between the platform and the terrain. If you don't need those collider, I suggest you just remove them. If you need them, assign them with layer-31 (terrain). Otherwise the system will not recognized them and treat them as obstacle, which blocks building of tower.
     
  26. uB1Ca

    uB1Ca

    Joined:
    Jul 25, 2013
    Posts:
    2


    My fail. sorry

    the problem comes from terrain layer mask :( so stupid situation.



    thanx for your fast answer ;)
     
  27. petrmaxa

    petrmaxa

    Joined:
    Jan 10, 2015
    Posts:
    17
    What controller do you use for towers animations? :)
     
  28. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    There's only one controller, used for both creep and tower. Look for it in the animation folder.
     
  29. petrmaxa

    petrmaxa

    Joined:
    Jan 10, 2015
    Posts:
    17
    Well if i use TDUnitController then it snaps towers always to (0,0,0) and i dont know why :/
    upload_2017-11-21_20-39-55.png

    upload_2017-11-21_20-42-51.png
     
  30. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    The controller will reset the transform position back to (0, 0, 0). You need to place the game-object with the animator component as a child object of the tower prefab. This way it's reset to it's parent transform position. Please refer to the example creep prefab in the animation folder.
     
  31. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    hello
    so i have this issue
    i want to place a single node platform on gameobject but the collision detection for the node is to sensitive and makes the gizmo red meaning you cannot place a tower there....
    where is the collision detection taking place in the script?
    is it adjustable?
    can i manually place a working node there?
    is it possible to add a script to hack it and activate it?
    is there some kinda work around you can think of?...
    i can place the platform 3.3 y above the object....but it would mean adjusting all the towers ive made and rebuilding all the work.
    in the image the green box on the top is a platform
     
  32. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    First is there a reason why there's a collider on the game-object? I would suggest you to remove it unless you need it. If you need the collider to be there, try assign it as a terrain-object, by setting it's layer to 31. The collision detection will then ignore it.
     
  33. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    removing the collider work..but i must add that i already tried that before on another game object before i got serious about trying to solve this.
    ive tried all the other TD game kits out there and i must say you have done very fine piece of work here thank you
     
  34. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Thank you. I'm glad that you find it useful.
     
  35. petrmaxa

    petrmaxa

    Joined:
    Jan 10, 2015
    Posts:
    17
    Does your audio manager somehow deactivates unity default sound? :D
    I am trying to add sound to gui
    upload_2017-11-28_13-13-54.png
    upload_2017-11-28_13-14-9.png
    upload_2017-11-28_13-14-55.png
     
  36. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It's nothing to do with the AudioManager. The way the UI works is that the game-object get deactivated when they are hidden. Deactivated game-object or component won't be able to do anything (hence the warning message, and it won't play). You need to place the audio source at another game-object.
     
  37. petrmaxa

    petrmaxa

    Joined:
    Jan 10, 2015
    Posts:
    17
    Well all buttons works great so far except ,,ButtonPause". :):) I tried to attach working sound effects from other UI buttons but it does not work. I guess pause button works somehow different?
     
  38. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm not sure if this will work but you can try this. Change the line71 (base._hide()) to this:
    Code (csharp):
    1. canvasGroup.interactable=false;
    2. canvasGroup.blocksRaycasts=false;
    3. UI.FadeOut(canvasGroup, duration);
     
  39. Arcanebits

    Arcanebits

    Joined:
    Dec 18, 2013
    Posts:
    108
    Hi,
    Im about to get the upgrade, just one quick question.
    How adaptable will it be to use game pads ? Usb for PC / Xbox or Playstation?

    any comment?

    Aldo
     
  40. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm afraid gamepads are not supported by default. I'm not familiar with them myself to be honest. I suppose you can use the UI navigation system with of the default unity UI to navigate the UI. You can also use key mapping to access UI function. These shouldn't be a problem. The tricky bit is to emulate mouse-cursor/touch-input to select a build-point/tower. I'm not exactly sure how you can do that effectively. A virtual cursor maybe? Either way, you will probably need to add quite a bit of modification to the UI.
     
  41. micheal332001

    micheal332001

    Joined:
    Mar 18, 2017
    Posts:
    27
  42. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    The version that you have purchaced (TDTK3) has been around for quite some time (a few years if I remember correctly). I've been maintain and update it. But there's only so much I can do trying to get the old framework to do more things that it wasn't designed to. That's why I've rewrite the new TDTK from ground up using newer version of Unity. It has a few new features compare to the old one. For that reason I think it's justifiable as an upgrade.

    If you have purchased the older version of TDTK, you can upgrade it to the new package for $15. I hope this explains it.
     
    schmosef likes this.
  43. micheal332001

    micheal332001

    Joined:
    Mar 18, 2017
    Posts:
    27
    yes it does but dont get me wrong here this is a great asset to get people started on making not just a tower defence game it can also be made into other types of game.

    Thanks for the reply.
     
  44. micheal332001

    micheal332001

    Joined:
    Mar 18, 2017
    Posts:
    27
    songtan i want to have the drons randomly choose a path so i have added 3 new paths 1 where they spawn from and then the other 2 attach to the first.
    I want them to randomly choose from the other 2 when they get there as these will be flying over head.
    could you please let me know if this is already there if not what script to look in to make the changes i need.

    Many thanks
     
  45. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    You can try look into UnitCreep.cs, Move(), around line393. You will see that the code getting the next shortest path from the current path. You can just replace that with a new function that return a random path from all available path. Make sense?
     
    Proto-G likes this.
  46. sala3000

    sala3000

    Joined:
    Jan 15, 2018
    Posts:
    8
    Hey Songtan,

    I'm trying to modify the scripts since my game requires custom code but since it's my first C# and unity attempt I'm a bit lost (I code in Java mainly), I'm having trouble understanding the calling order of everything.

    I'm trying to generate a random sized platform and random position checkpoints when the map initialize, where should I look to do this (what file)?
     
  47. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    The initializationof the level (towers, path, spawn) is done in GameControl.cs, Awake(). It's pretty obvious if you just look at the function. If you have custom code that need to be run before that, you need to make sure GameControl call and run it before the default initialization. Hope that helps.
     
  48. micheal332001

    micheal332001

    Joined:
    Mar 18, 2017
    Posts:
    27
    I thought it was down to the UnitCreep just needed to know how it was called so that if there flying to choose a path randomly instead of the shortest path.

    Will look in to this a bit more thank you.

    I have already changed the code to add a commend centre in and show the health level of it.
    The reason for this is to show the health level better than just in the top left as 23/30 you know what i mean.
     
    Last edited: Jan 29, 2018
  49. jim00

    jim00

    Joined:
    Feb 1, 2015
    Posts:
    57
    can TDTK be used in 2D?
     
  50. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    In term of game mechanic, you should have nor problem getting it to work in 2D. However, I should say that it doesn't quite support full sprite-display (for instance, have the sprites facing the correct direction in isometric view). It's all about the camera angle you want to use really. If you are going for a top-down view, the framework is fine as it's. Otherwise you might have to do a little modification. Hope that answer your question.