For the continuation of our enemy revamping, we are going to look at creating an enemy that will dodge our shots at them. This is a relatively straightforward process, once you figure it out. The issues I ran into a bit during the process was to make sure all my lasers had a proper tag on them. As I was using a copy of the player laser for the enemy laser, and attached 2 of them to the parent for a dual laser, I had to go into that parent and change the children to a new ELaser tag so that the enemies wouldn’t try to dodge their own lasers the moment they spawned.
To start, we need to create an empty object and attach it to our enemy units. We will attach a rigid body and a polygon collider to this object:
The reason for the polygon collider is so that we can create a cone shape to simulate the “eyes” of our enemy’s ship. From here, we can create a new script for this detector and add in a couple OnCollision voids:
The purpose for these two voids is to either activate or deactivate our dodge mechanic that we have built onto the new enemy type. From here, we can move over to the new enemy and add in a couple new variables to work with:
With our _ranNum, we will just add it into our void start as such → “_ranNum = Random.Range(0, 2) * 2–1;”
As for our new enemy type, we will create both the enemy and a void to go with it:
We are attaching our basic laser fire script along with an if statement for our new dodge mechanic. This new mechanic will have our enemies move at a random distance away from our lasers as long as the statement is true.
With all of this in place, we can now look at how it appears in game. As a temporary measure, I have just created a colour change on the new enemy ships, as I may have some new models coming.
Now that we have a dodging enemy, we can look at using this new detection feature in the future to have them target and destroy our powerups.