Learn to code by making a 2D space shooter game.
Objective: Implement a new enemy type with the ability to fire a laser, missile, and new unique movement.
Before doing that adds a box collider2D,rigidBody2D and add a new script called Smart Enemy.
In the next step, go inside SmartEnemy, and implement a unique movement.
If you want to understand how cos and sin work, go to check how to implement a new enemy movement in this article below.
here is a link to the article: https://suleimanabdullah.medium.com/learn-to-code-by-making-a-2d-space-shooter-game-c5991b2b5749
In the second step drag, the laser sprite adds all necessary components, rigid body boxcollider2D then drag it to the prefab folder.
In the next step add a script for laser behavior, if you don't know how to add a laser behavior go to my previous article.
The next step is to make smart enemy's ability to fire a laser, we need to set it by dragging smartenemyLaser and duplicate it then create an empty parent object. Then drag those two lasers to the parent container and reposition them to the SmartEnemy front guns.
In the next step drag the Smart_Enemy_lasers to the prefab.
The next step creates a logic for the enemy to fire a laser at every random second.
Note: to understand how to Implement this go to my previous article on how I implement player firing lasers.
In the Update method, provide this logic to fire every random second.
In the next step, we need to damage the player with our laser.
In the next step, I will damage the player if we collide with each other to damage the player and destroy us, I won't explain how to set variables.
In the next step-change the enemy prefab game Object to an array.
The next step is to spawn a new enemy using SpawnManger, go to the smart enemy create an Action delegate and call it when we hit a player.
Then inside SpawnManager change the enemy prefab we Instantiate it to an array since we will store a random enemy, and instantiate it as an array with a random number.
Then call the call OnDeath of SmartEnemy after OnDeath of normal Enemy.
In the next step go to the Inspector, add the size of enemyPrefab as 2 then drag normal enemy and smart enemy from Prefab then delete SmartEnemy from the hierarchy.
In the next step, we need to give our new enemy the ability to fire missiles, first create this variables.
Then create a method to calculate the target and rotation of the missile.
In the next step call this method in the fixed update, since we work with a rigid body.
if you need a deep explanation of how to implement homing missile.
here is a link below:
In the next step, we need to give the enemy ability to damage the player when being hit by a missile.
In the next step go to the smart enemy create a method to fire every couple of seconds and call it in the update.
In the Last step Delete the missile then select smart Enemy drag the missile to the field.
Let's see the final result.