Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

some question about tower defense

Discussion in 'Scripting' started by kykiskie14, Aug 16, 2016.

  1. kykiskie14

    kykiskie14

    Joined:
    Aug 14, 2016
    Posts:
    4
    hi guys, i wanna learn how to make a simple TD game like gemTD, where the enemies have to move to waypoint and the next while avoiding the towers that i built, i have several simple questions :

    1. Pathfinding
    a* algorithm package from aroganberg.com or navmesh.obstacle ? whats the pros and cons ? which one should i use ?

    2. lets say i have 3 towers (a, b, c) and 3 tiers (bad, normal, good)
    every time i place a tower, the tower will be randomized.
    lets say the chances are (50% bad, 30% normal, and 20% good) , and then i want to random between tower a,b,c and then instantiate their prefab depending on their tiers. how do i achieve this ?

    thanks

    p.s : no code yet
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Definitely navmesh if you want easiest integration with Unity's physics colliders. You wouldn't be locked to grid when building towers either, unless you want to. Can have towers 0.7x the size of the larger ones, thin walls or whatever.

    But if you want to use strictly tile-based map and you don't mind A* it would possibly be very performance optimal. As long as you remember to not keep searching a path every frame, just when the path potentially changes, like the moment you add a structure.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    I would assume you have a lot of enemies and a relatively small number of waypoints.

    So, assuming you're working on a grid (which you SHOULD if you're new, as it makes your life about a gazillion times easier), you shouldn't use A* or NavMeshes for that. Instead, you just keep a distance map for each waypoint. You can recompute these when the player adds a structure; it's fairly quick to do. And then using it is really fast — each unit simply looks at the distance map for its waypoint, and steps into the unoccupied cell that has the smallest distance.

    What's a distance map, you say? It is simply a 2D array of numbers that is the distance (number of steps) from every point to the goal. You build this as follows:
    • initialize the whole array to something impossible, like -1, to mean "not figured out yet"
    • keep a queue (or list or whatever) of grid locations you need to consider
    • initialize the location of the goal with a 0 (distance 0, see?), and throw its location onto the queue
    • while the queue is not empty:
      • pull the next location off the queue, and note its distance in the grid (call it d)
      • consider each of its neighbors; if reachable and still set to -1 in the grid, then set its distance in the grid to d+1, and throw it on the queue.
    That's it. It's just a breadth-first search, filling in the distances in the array. (Basically the first two steps of Lee's algorithm for routing circuit paths on a chip.)

    If you've programmed before, this will be pretty easy... if you're entirely new to coding, it may be a bit of a challenge. But it's definitely the right way to go!
     
    Last edited: Aug 16, 2016
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Aron's A* solution is fantastic. Unity's Navmesh is garbage.
     
    kykiskie14 and SubZeroGaming like this.
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    The navmesh is fine for what it is. The problem with the nav mesh is that it's baked. You can't dynamically define your walkable area, which means if you place a tower on the floor, you can't recalculate the navmesh. You can only check if the path is reachable.

    A* is a good way to go and it seems that Joe's version is quite efficient

    As for your randomizing quesiton, use Ramdom.value.

    if (random.value < .2f)
    20%
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That's only the tip of the iceberg :)

    We tried to use it one time as part of a Ludum Dare and threw it out after the first day. You can't update it, you can't calculate it at runtime, the obstacle avoidance is a complete joke, there's no way to do an effective arrive behavior when moving multiple agents to the same spot (they just rub against each other endlessly until they all push each other out of the way enough to trigger their "arrived" functionality). Unity should just hire Aron and bake his solution into the Editor and API.
     
    SubZeroGaming likes this.
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Great suggestion. Similar deal with what they did for NGUI/UGUI
     
  8. kykiskie14

    kykiskie14

    Joined:
    Aug 14, 2016
    Posts:
    4
    thanks all for reply, guess i'll go with a* from aron then..

    and for my randomizing question, can you elaborate more ? im thinking about nested if but dunno if thats the proper way to do it.
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    they still officially deny the ngui guy did anything on the eventual ugui implementation...

    at least the navmesh rework got bumped from "development" to "alpha" on the roadmap since the last time I checked...
    https://unity3d.com/unity/roadmap

    curious if the usability testers can talk about what they've done with it?
     
  10. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008

    So you want to check for 20%, or 50%, etc. right?

    within 0-20% you want a good tower right?


    Code (CSharp):
    1. float towerPercent = random.value;
    2.  
    3. if (towerPercent < .20f)
    4. {
    5. //1-20% spawn great tower
    6. }
    7. else if (towerPercent < 0.5f && towerPercent > .20f)
    8. {
    9. //20-50% spawn a good tower
    10. }
    11. else
    12. {
    13. //spawn the S***ty tower
    14. }
     
  11. kykiskie14

    kykiskie14

    Joined:
    Aug 14, 2016
    Posts:
    4
    yep and each tiers there are 3 types of towers to be randomized (tower a,b,c) so.. nested if ?
     
    SubZeroGaming likes this.
  12. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    yup