My journey becoming a Unity game developer: Asteroid starting 1st wave
Objective: Add an Asteroid game object that will rotate around 360 degrees until the player destroys it. When its destroyed, the enemies and powerups will be allowed to spawn in the game.
To begin we created an Asteroid game object using an Empty object. Then we added a Circle Collider 2D and a Rigidbody 2D components to it. Make sure to adjust the collider around the Asteroid, and turn on Is Trigger for the lasers collision. With the Rigidbody 2D, set the Gravity Scale to zero to prevent it from falling down. Last thing is to create a new C# script name Asteroid, and attach it to the Asteroid game object.
Inside the Asteroid script, create a SerializeField Type Float variable name _rotateSpeed. Also, create a handle variable Type SpawnManager name _spawnManager to access the Spawn Manager script component.
Inside the Start() function, use the _spawnManager to Find the Object Type of SpawnManager, and Get the Component of the SpawnManager. Inside the Update() function, get the Transform of the Asteroid and Rotate it along its Vector3 forward. Multiply this rotation by the _rotationSpeed and the deltaTime of Time.
Play the game and see the Asteroid rotating at the preset of 3 meters per second. This was too slow for me, therefore I adjusted the _rotationSpeed to 10 meters which was more like it for me.
Next, we want to destroy this Asteroid when the Player’s laser collides with it. Also, we want the enemies and powerups to spawn after the Asteroid is destroyed.
Inside the SpawnManager script, create a new Public Type Void method name StartSpawning(). This method will control the spawning of the enemies and powerups. Cut and paste the coroutines of SpawnRoutine() and SpawnPowerUpRoutine() inside the StartSpawning() method to Start their Coroutines here.
Delete the Start() and Update() functions from this script as we don’t need them for now. If we don’t, Unity will go through the process of checking in on them because of their Order of Execution for Event Functions. This can affect performance if this process has to be done a good amount of times.
Inside the Asteroid script, create a new variable with a SerializeField Type GameObject name _explosionPrefab. This variable will give us access to the Explosion prefab that will be created soon. Now create an OnTriggerEnter2D to check IF the other object’s transform is a Laser by using CompareTag() method. If it is we will Instantiate the _explosionPrefab at the Asteroid’s transform position with no rotation. Destroy the other object colliding which is the Laser, and call on the _spawnManager’s StartSpawning() method. Finally, we can Destroy the Asteroid itself in 0.5f seconds.
With the Explosion script, inside the Start() function simply Destroy this game object in 3f seconds, and delete the Update() function.
To create the Explosion prefab, use an Empty game object, Reset its Transform, create a new Animation, drag the Explosion animation sprites into the Dopesheet, and make sure the Animator is set up properly. With the Sorting Layer, change it to Foreground and Order Layer to 1 which will place the explosion on top of the Asteroid.
Select the Explosion animation, and turn off the Loop Time to keep it from looping over and over.
Play the game, destroy the Asteroid and see the explosion playing on top of the Asteroid. Also, notice the enemies and powerups spawning right after the Asteroid is destroyed.