Implementing Enemy Spawn after Enemy Defeat in Tower Defense Game

Updated on January 4, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on December 25, 2017

Hey everyone, I’m Dexter.

So you know how in Bloons Tower Defense after a bloon gets shot, it’ll be destroyed and spawn a weaker bloon? Well thats what I’m going for in my game, but I ran into a problem. When the weaker enemy gets spawned from the stronger enemy, it follows the very first waypoint. I’ve tried a couple of methods to fix it, but nothing is working.

How do I change the weaker enemy’s waypoint target to the stronger enemy’s waypoint target before it gets destroyed?

Also I put this thread in tutorials because I was following the tower defense tutorial, just fyi if that wasn’t obvious. Anyways, any help is very much appreciated, thanks!

  • Liked by
Reply
0 on December 30, 2017

UPDATE: I’ve tried two methods to fix this,

1. Have the spawned enemy get the next waypoint from the enemy it spawned from.

2. Have the spawned enemy find the nearest waypoint.

The second method has it’s flaws, but I have no choice but to use it until I can somehow make the 1st method work.

Here’s most of the code for the 1st method:

void EnemyToSpawn (GameObject[] enemy, EnemyMovement _eM)
 {
 if (enemeyToSpawn != null)
 {
 for (int i = 0; i < enemy.Length; i++)
 {
 Instantiate(enemy[i], transform.position + (offset * i), transform.rotation);
 WaveSpawner.EnemiesAlive += enemy.Length;
enemy[i].GetComponent<EnemyMovement>().SetCurrentWaypoint(_eM.GetCurrentWaypoint());
enemy[i].GetComponent<EnemyMovement>().SetCurrentWaypointIndex(_eM.GetCurrentWaypointIndex());
Debug.Log("Currently following: " + _eM.GetCurrentWaypoint());
 // Problem detected: After instantiate, enemy follows first waypoint. Code a way for the new enemy to find next waypoint nearest to it. (Fixed? Kinda, see problem below)
 // New problem: Finding next waypoint isn't perfect. Code a way to get the waypoint info from stronger enemy so the spawned enemy can follow that waypoint.
 }
 }
 }

the eM is the EnemyMovement component gotten from the stronger enemy that the weaker enemy is spawned from. Sorry if this confuses you, I’m not good at explaining code to someone

  • Liked by
Reply
Cancel
0 on January 2, 2018

Try these and let me know if you need any more help.

Edit: I am not sure if you are using NavMeshAgent or not so let me know and i will adjust accordingly.

ENEMYMOVEMENT

 public class enemyMovement : MonoBehaviour
 {
 public Transform[] waypoints; //SET OF TRANSFORMS TO USE AS WAYPOINTS
 private int currentWaypoint; //THE CURRENT WAYPOINT INDEX
 private NavMeshAgent agent; //THE NAVMESHAGENT ATTACHED TO THE ENEMY THIS SCRIPT RESIDES ON
 void Start()
 {
 agent = GetComponent<NavMeshAgent>(); //GETS A REFERENCE TO THE NAVMESHAGENT
 agent.autoBraking = false; //SETS THE NAVMESHAGENT AUTO BRAKING TO FALSE SO THEY DO NOT SLOW DOWN
 chooseNextPoint();
 }
 void chooseNextPoint()
 {
 if(waypoints.Length == 0) //IF THERE ARE NO WAYPOINTS DO NOT CONTINUE
 return;
 agent.destination = waypoint[currentWaypoint].position; //SETS THE NAVMESHAGENT DESTINATION TO THE NEXT WAYPOINT
 currentWaypoint = (currentWaypoint + 1) % waypoints.Length; //INCREASES THE CURRENT WAYPOINT BY 1 (AUTO RESETS TO 0)
 }
 void Update()
 {
 if(!agent.pathPending && agent.remainingDistance < 0.5f) //CHECKS IF THE NAVMESHAGENT HAS A PATH AND THEY ARE CLOSE TO THE CURRENT WAYPOINT
 {
 chooseNextPoint();
 }
 }
 public void setWaypoint(int index)
 {
 currentWaypoint = index - 1; //SETS THE CURRENT WAYPOINT TO THE ONE PAST IN - 1 SO WHEN WE CALL CHOOSE NEXT POINT IT GETS THE CORRECT ONE
 chooseNextPoint();
 }
 }

 

ENEMY SPAWNER

void EnemyToSpawn(GameObject[] enemy, EnemyMovement _eM)
 {
 if(enemeyToSpawn != null)
 {
 for(int i = 0; i < enemy.Length; i++)
 {
 Instantiate(enemy[i], transform.position + (offset * i), transform.rotation);
 WaveSpawner.EnemiesAlive += enemy.Length;
 enemy[i].GetComponent<EnemyMovement>().setWaypoint(_eM.getCurrentWaypoint());
 }
 }
 }
  • Liked by
Reply
Cancel
0 on January 4, 2018

I actually decided to use the NavMeshAgent and change the way the enemy moves entirely, but after seeing your code, I didn’t realize the different methods NavMeshAgent has and your code might make my enemies move a little better. So I’m trying out your code and so far I ran into a problem. When I set the waypoints I have as a reference in the inspector, it doesn’t let me do that for some reason. I think I have to make it find the waypoints that are in the scene by way of code

  • Liked by
Reply
Cancel
0 on January 4, 2018

Nvm, figured out the problem, I forgot I had a Waypoints script lol

So I tried out your code, and it works fine! I turned on autobraking cause when it was off, the enemy’s looked like they ran into a wall like they’re drunk or something lol.

But I like it! It looks a little better than my code, thanks so much for the help!

  • Liked by
Reply
Cancel