Day 45: New Enemy Part 2

Objective:Create a new enemy that has a different movement path and has different weapon type.

Yesterday we left off when the enemy is now able to move to the way points while facing it correctly. Today we’ll cover how they move to the next one.

In direction, it’s getting a way point at a certain index. To increase the index we’ll use a onTriggerEnter2D. Inside we’ll check to see if the enemy has collided with an way point by checking it’s tag. On all way points, we set tag to Waypoint.

If it was a waypoint increase the _currentWayPoint variable by one. To stop us from having a Index out of range error we’ll check to see if the currentWayPoint is greater than or equal to the length of the array. If it is we’ll set it back to zero and make the enemy back to the top of the screen. If we hit the last way point we should be off the screen and it’s time to respawn to the top. We’ll set the collision to match the same location the enemy spawns at. This allows the instantiated waypointprefab to stay in a proper reference of the enemy. If we didn’t the enemy would keep crossing the screen to go and do the same points again.

The respawnTopOfScreen is the samething we created a while back for the enemy to respawn at the top of the enemy was to far down on the y axis.

One last thing to clean up is the amount of instantiated object in the hierarchy. When the enemy gets destroyed we’ll destroy that waypointprefab that’s associated with the enemy. We do this by taking that variable we created when the object was instantiated.

This will help keep our hierarchy clean from a lot of them being spawned.

We will have one more article about the new enemy tomorrow. It’ll finish up with the new weapon the enemy will have, I’m currently calling the scatter bomb! See you tomorrow!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood


I am passonate on learning to program and use Unity to become a skillful Unity Developer