Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tower Defense Tutorial

Hi, I've been working on the tower defense tutorial, and everything works as it is. But I'm trying to use the waypoint script to get my enemies to make a random choice of following a different set of waypoints. (I.E. when the enemy spawns, it either follows path 1 or path 2) Any thoughts on this?

Comments

  • jtok4jjtok4j Member

    Greetings,

    Sounds like a great modification to make! Where are you at with this and are you stuck on any certain part/point?

     Keep on Creating! 


    Justin of JustinTime Studios (http://unity3d.expert) 

  • Hi,


    I have tried putting the waypoints inside an array of arrays. But I couldn't work out how to set it up the array properly, or reference it properly either.

    I'm not even sure if this is the best way to go about it in the first place lol.

  • MenithMenith Member
    edited June 2020

    Hi, try this. Start organizing your waypoints in a hierarchy. E.g. WP1 (empty game object) as the set of the first path and WP2 for the second etc.


    Remove the concept of an explicit start point and use the first waypoint in the sets as the start point.

    Therefore, you must adjust a couple of things.

     

    Wave.cs:

    Add

    Tooltip("Default is 0, if you have added more waypoint sets, than put in here the set you want to use. -1 for a random waypoint set")]
    public int wayPointSet;
    

    Then you can indicate which waypoint set a wave should spawn/use.

     


     

    WaveSpawner.cs:

    Change

    public Transform spawnPoint;

    to

    private Transform spawnPoint;

    We will set the spawnPoint to the first waypoint in the used waypoint set.

     

    Add

    EnemiesAlive = wave.count;
     
    ->      Waypoints.SetWaypointsSet(wave.wayPointSet); // Select the set of waypoints
    ->      spawnPoint = Waypoints.GetStartPoint(); // Set the spawnPoint to first waypoint in set.
     
    for (int i = 0; i < wave.count; i++)
    

     

     

    Waypoints.cs

    Change it to

    public class Waypoints : MonoBehaviour
    {
       private static Transform[][] pointsMap;
       public static Transform[] points;
     
       private void Awake()
       {
           Transform[][] _pointsMap = new Transform[transform.childCount][];
     
           for (int i = 0; i < transform.childCount; i++)
           {
               Transform _transform = transform.GetChild(i);
               Transform[] p = new Transform[_transform.childCount];
               _pointsMap[i] = p;
               for (int j = 0; j < _transform.childCount; j++)
               {
                   //Debug.Log(_transform.name + " - " + _transform.GetChild(j).name);
                   p[j] = _transform.GetChild(j);
               }
           }
           pointsMap = _pointsMap;
           points = pointsMap[0];
       }
     
       public static void SetWaypointsSet(int index)
       {
           // Select a random waypoint set.
           if (index == -1)
           {
               System.Random r = new System.Random();
               int rInt = r.Next(0, pointsMap.Length);
               points = pointsMap[rInt];
               return;
           }
           // minor mistake of the user in the configuration of the wave :-)
           if (index >= pointsMap.Length)
           {
               Debug.Log("index \"" + index + "\" is out of range in pointsMap (max=" + (pointsMap.Length - 1)  + ").");
               index = 0;
           }
           points = pointsMap[index];
       }
     
       public static Transform GetStartPoint()
       {
           // return the first waypoint.
           return points[0];
       }
    }
    


    Thats it, enjoy and have fun.

    Btw. is it possible to format the post in a more code sstyle?

  • danieldaniel Member

    Menith haha yeah its possible you just need to click on the paragraph sign left to the text you are typing then go on the quote sign and select code block

    it should look like this;  //:)
    
  • MenithMenith Member

    @daniel thx a lot. I just tried it out and it worked perfectly.

  • Wow @Menith Thanks a lot. This is exactly what I was trying to do. I just couldn't work it out.


    I still have a problem though. Because I have made quite a few other changes the game. I have changed it so the waves can spawn early, and now if the next wave spawns, the current enemies on the field will change over to the new waypoints.

  • Hi Brackeys, thanks for the awesome tutorials, am learning new stuff everyday,i have a issue and struck with it for days, am doing a Tower defense game with isometric tile, i went thru tower defense tutorial(done in 3D) for learning, but am doing a with 2D sprites, i some how figured out the character movement in way points, but my enemy character has different movement sprites , i dont how to call the respective sprites when character is turning on right or left. The tutorial above is written in Player perspective with Key inputs, but i just want the Enemy spawning in a waypoints with moving on respective direction sprites. can u please refer any tutorial.Thanks a Lot.

  • Hi all, I don't know if this forum is still active, but I need some help I got to ep 10 then I realized my turrets when placed on a node it will sit above the node so I tried the offset nothing still in the air then I put 100 ,100 ,100 in the offset to see if it is working nothing still in the same place and the same height as it was before. So, then I reimported the models to see if that will work no, they're stuck in the node now and offset does not fix it the script is the same as Brackyes the turret and missile turret are my own models they have not been orientated right when exported but I made sure the partToRotate is facing the right way and other empty game object is facing the right way (muzzle flash, Fire Point and the empty game object containing all the this stuff) please help I got so far now I'm puzzled.

Sign In or Register to comment.