Asteroids!! — Some New Immersive Logic

Objective: Introduce a new asteroid prefab and logic to control the enemy spawning

This is mostly for fun. What if the enemy wave started after a particular event?

Let’s say we have an asteroid in the middle of the screen, everything is quiet… but then we destroy it and thus we wake those scumbag enemies!!

There is going to be a lot of work, but it will be worth it.

First let’s create an Asteroid prefab using a sprite, attach a C# Script, add a collider (circle, no trigger) and a rigid body component and the explosion animation as did for the enemy. Let’s go quickly over stuff already done. Adjust the collider size if needed and all other settings as you want.

Now, we create a folder, Materials, to contain a new 2D physics material specifically created for the asteroid. In the inspector there are 2 values than indicates the physical properties of the body in a collision: namely the friction attribute in sliding collision and the bounciness factor (1 -> totally elastic, no energy loss).

Let’s know write the code for our purpose: destroy the asteroid and then start the wave.

First, we are going to add some rotation to the rock. Since it is a rigid body and obeys the law of (Unity Engine) physics, we can assign a value to the angular velocity and, if no other interaction occurs, the body will continue rotating indefinitely. No need to use Update at each frame.

I told you, you’re going to love it.

Let’s continue with the collision behaviour. Since the asteroids are colliders, not triggers, the interaction between two rocks will be resolved as a 2D bodies collision according to physics (bounce, rotations and so on), but it can still interact with triggers such as lasers, enemies, player… So, we can still use the OnTriggerEnter method.

Usual switch case, if lasers hit the asteroid there is a system of lives (already seen), when below zero the asteroid is destroyed and also score given to the player. Notice the add force method: each laser will provide some linear impulse to the rock, changing a bit is state of motion!!

The real destruction is carried out by the AsteroidDestruction method which acts just like EnemyDeath, but with some readjustment of velocities when the animation plays (just an esthetical choice).

The table is ready for dinner… now for the main course!

If we want to control the spawning, we have to modify the spawning manager script — of course.

Sorry, but there will be some rework to do. Follow me here, I came up with a logic that will be good for enemies, power ups and anything else is to spawn.

So, I wrapped all the logic in an if clause, checking a value returned by ShouldSpawn method, basically saying if spawning is allowed. The while condition is changed too, now checks for a bool I called override.

Don’t forget the else after the ne if clause: without it, in the case of no spawning allowed the method would have no yield return!!

The ShouldSpawn method will check, in this order:

  1. The override bool, which is a sort of switch that turns on or off the spawning
  2. If the player is alive, as before
  3. If the way is clear for the wave.

The last condition is just a check over all asteroids present on the scene (I tried to think ahead): asteroids are provided with an ulterior bool isWaveAsteroid, true if the purpose of the asteroid is to hold the wave. So, if there is at least one wave asteroid on the scene, false is returned and consequently ShouldSpawn method returns false. When all wave asteroids are gone, the way is clear for the wave.

The same logic will be applied to the power up spawning coroutine, so everything is held until the asteroid is destroyed.

Let’s see the result.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniele Quero, PhD

Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams