Today, we will make Homing Projectile. It will be another rare powerup. It will seek the closest target and destroy it.
We need to make a Prefab of the new powerup. Find a sprite you like to represent the powerup. Call it Homing Projectile. Change the color and change the sorting layer to Foreground. Add Rigidbody2D and change Gavite Scale to 0. Add Collider2D and Powerup script. Change powerup ID and add powerup sound effect.
Create a new GameObject for our projectile
Go to Player script and add a new method called HomingProjectile.
Go to the Powerup script and add a new case to the switch statement. Now, when we get the powerup we will call the method.
Add two variables in the Player script. Assign the new projectile to the _missle in the Inspector.
In the HomingProjectile() method change _homingProjectileActive = true to active the new projectile.
In the Fire() method, add a new else if statement, where we change back the_homingProjectileActive = false, and we Instantiate the missle.
In the Laser script, in the Start() method we need to check if the projectile we fired is our missle.
Now in the MoveUp() method we need to check if there are any enemies in the scene. If there are, we need to find the closest one and move towards it.
In the enemy script we need to add our missle to the OnTriggerEnter2D() method.
Last thing we need to do is add our powerup to the spawnManager. We have 5% chance to spawn the Homing Projectile powerup.