Learn to code by making a 2D space shooter game.
Objective: Implement Secondary Fire Powerup.
The first step is to drag missile Powerup add rigid body set gravity scale to zero to add box collider2D check is Trigger drag it to the Prefab folder then an adiosource component.
The second step go to the player script create a bool variable called isMissileActive;
The third step create a public method called ActivateMissile and set isMissileActive to true;
The fourth step got the FireMissile method and check if isMissileAtcive is equal to true if it's true Instantiate otherwise don't Instantiate.
Fith stet creat MissilePowerDownRoutine wait for about 9 seconds the turn isMissileActive to false.
In the next step call the coroutine after set isMissile active to true inside ActivateMissile.
Go to the SpawnManager create GameObject variable call it missile Variable.
In the next step create a coroutine called spawnPowerupRarely, then create a while loop to check if the player is alive.
Inside a while, the loop creates a random position to spawn.
Instantiate the MissilePowerup very rarely, am going to put at least 15 seconds must pass before spawning the next missile Powerup but before anything, we need to wait for a long period before spawning missilePowerup.
Then call that spawnPowerupRarely coroutine inside start method in the SpawnManager script.
Go to the Inspector select spawnManager drag and drop missilePowerup to the missilePowerupPrefab field then Select MissilePowerPrefab set PowerupID to 4.
In the next step go to the Powerup script create a case 4 inside switch statement to check if we collide with missilePowerup, then we Activate the missile by calling ActivateMissile.
Now we need to disable missileIcon Inside the bubble create a new script Called MissilIcon then attach it to missileIcon.
Create a SpriteRenderer variable, then cache it in the start method.
In the next step create an OnTriggerEnter2D method, then check if we hit the Player set Disable sprite renderer to false this will turn off the sprite remember when Parent Object is destroyed so this Object too will be destroyed.
Let's See the Final Result.