Day 17
What I’ve learned so far from the coursework is that game development is a system of checking it once and checking it twice. There have been moments where I am able to create codes and execute them without errors and then there are movements where my gameObjects aren’t positioned or interacting with one another appropriately. One thing that I appreciate is how the coursework is designed to help us think critically by challenging us to create codes for ourselves in one video followed by a review in the next. By the end of the day, my brain is mush from coding.
Asteroid Behavior
Today’s focus was developing an asteroid that appears at the start of the game and once destroyed, the game begins. To get started, I dragged the asteroid sprite into the hierarchy and added a circle collider 2D and rigidbody 2D components. A circle collider 2D allows the gameObjectA (player) to collide with another gameObjectB (asteroid) within a circle. I then looked within the circle collider 2D and ensured the trigger is set to active so the player is able to pass through the asteroid. This is adjusted though the Edit Collider. To prevent the asteroid from falling downwards, I go into the rigidbody 2D settings and set the gravity scale to 0.
Once I have the asteroid sprite all set and ready to go, I created a C# script for the asteroid behavior. Once created, I dragged the C# script to the inspector. From here, I wanted to create rotation for the asteroid. To do so, I open the C# script and create a variable and rotation method — private float _rotateSpeed = 3.0f; For the method, there are a couple of ways to do this. Similar to the way I created enemy movement, I used the transform.___ but instead of transform.position, I used:
- transform.Rotate = new Vector(0,0,1);
- transform.Rotate(Vector3 .forward * _rotateSpeed * Time.deltaTime);
Both ways function the same way, for my preference I chose the second code. Once coded, I go back to Unity and ran the game. While playing, I began to notice the asteroid ran a little too slow for me. I went back to Visual Studios, into the asteroid C# Script and serializeField the private float to be able to view the script on Inspector via Unity. On the inspector, I changed the rotation speed. Once I was satisfied with the speed, I ran the game again.
Exploding Asteroid
My next step is to develop the explosion animation that will occur once a laser collides with it. I go into the asteroid C# Script via Visual Studios and created a private gameObject _explosionPreFab; -> serializeField the gameObject to be able to view it within the inspector. This gameObject is designed to instantiate an explosion within the void OnTriggerEnter2D(Collider 2D other). I created an if() statement to inspect who “other” is and in this case “other” = “laser”. To be able to destroy the asteroid the following code was used — if(other.tag == “laser”) { Instantiate (_explosionPreFab, transform.position, Quaternion.identity); (reference lines 24 to 28). Next I inputted the code to destroy the gameObject (Asteroid) using — Destroy(this.gameObject); reference line 27.


Once those were coded, I wanted to prevent any other gameObjects aside from the asteroid from spawning into the game until the asteroid was destroyed. Here I go into SpawnManager C# Script and remove the StartCoroutine within the void Start and place that code within the public void StartSpawning (). This will call from the asteroid C# Script in this method. Within the asteroid C# Script, I am able to reference the spawn manager compenent by — private SpawnManager _spawnManager; and coded this into the private void Start (). I then head back to the asteroid C# Script and coded void OnTriggerEnter2D(Collider 2D other); — this lets the game know when to start spawning the enemies and power ups.
