Learn to code by making a 2D space shooter game.

Suleiman Abdullah
6 min readApr 24, 2022

Objective: Implement Smart Enemy

Before doing anything we need to set the ship's y-axis to face down, create an empty game reset its position, rename it as the smart enemy holder, and drag the smart enemy inside the smart enemy holder . Then rotate the smart enemy holder 180 degrees, then flip the y-axis on the sprite renderer component by adjusting the flip option of “y” of the smart enemy.

Note: I rotate the parent game object 180 degrees, to turn the basic vector of the smart enemy to facing down. Since my ship is going down the basic vector needs to face, in the same direction but if I do that the ship will face up and the basic vector will face down. So to make the ship face down I have to flip the y axis of the sprite renderer on the flip option, the dot product needs a basic vector to face as the look direction of the smart enemy is facing for the result to be correct. But if your basic vector is facing where your ship is facing you don't have to do this hack, but make sure is where the smart enemy moves in the same direction.

In the first step drag, the back gun inside the smart enemy set the scale of it and renames it as a back gun.

Note: First unpack SmartEnemy from the prefab, drag the smart enemy to the smart enemy holder then delete the smart enemy inside the prefab folder. If you do not unpack the smart enemy prefab then delete the smart enemy you will have a missing prefab in a smart enemy holder when dragging it to the prefab.

In the second step, the select spawn manager then drags the smart enemy holder inside the missing enemy prefabs.

In the third step, drag the smartEnemyLaser position to the back gun and record the position you want to instantiate the laser.

The next step is time to create a modular SmartEnemyLaser, we do this by creating a bool variable renamed it IsBackgun.

Now create a public method to activate a Back gun, call it ActivateBackgun.

Inside smart enemy laser

Now Activate isBackgun to true inside Activate Backgun method.

Create a new method to move the laser back gun up, so now we will use this method to move the laser up when fires from the back gun.

Go inside the update and check if isBackgun is false to make the laser go down else move the laser up.

In the next step go to the SmartEnemy script, and create a method that detects a player ship from the back.

In the next step, we need to calculate the direction to the target, we take destination minus source.

In the next step, create a float variable for the dot product called it to dot.

Then go to smartEnemyDetectPlayerFromBack, find the dot product we do this by using Vector3.dot then passing the direction and basic vector where the ship is facing but before we need to make sure to normalize the direction since the basic vector is already a normalized vector.

Note: To avoid null reference make sure to check if we have the target.

Now call this function inside update, we do this in order to make sure we are continues to check if the target is behind.

In the next step, create another bool to only fire back gun when a player is behind us.

In the next step, we need to check if isTargetBehind is false to only allow to fire of the front gun.

In the next step, create a method called FireBackLaser, this method is responsible to fire a laser from the back gun.

In the next step, we need to set the Can fire and set the fire rate.

In the next step, we need to Instantiate a laser but we want to move the laser down inside the FireBack laser method, we do this by declaring a GameObject and assign to the Instantiated laser.

In the next step, we need to make our laser move up, we do this by creating a local variable of smartEnemyLaser then assigning a handle to the smartEnemyLaser game object to get the component then we Activate the back Laser.

In the next step, check if the player is behind us we use the dot product to determine this. if two vectors point the opposite directions the value of the dot product is -1. If two vectors are perpendicular the dot product is zero if two vectors point in the same direction the dot product is 1.

the red arrow is the value of the dot product.

So now I will check in the Update method if the dot product is less than -0.986, then we need to set a bool istarget behind variable equal to true.

In the next step, if the target is behind we need to check if the time is greater than can fire then we call the Firebak laser method.

Last if the player is not behind we need to disable isTarget behind, we do this by adding else condition after checking the dot product and setting istargetbehind to false.

Not: if we didn't do this the back gun will fire forever.

In the next step, since we have made our basic vector which is the y-axis facing down, for the smart enemy to move down we need to change the translation by adjusting from vector3 .down to vector3.up to make the player move down.

Last select smart enemy then drags the smart laser, then override the changes to smartEnemyHolder.

Let's see the final result.

See you in the next article.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development