Spawning Asteroids

Daniele Quero, PhD
4 min readJul 20, 2021

Objective: Manage asteroid spawning along with enemies and power ups. Set the behaviour of asteroids.

I found this part even more funny and interesting. We’re going to spawn also asteroids and take into account a lot of things around, from which we can learn a lot:

  1. Random movement direction, how to generate a random versor in the plane
  2. Random speed and angular speed
  3. Rigid body movement
  4. Use of Rect Unity object to check if the asteroid is out of screen and has to be respawned
  5. Respawn logic
  6. Impact between asteroids
  7. Interaction with other triggers.

First, I introduced a new bool to tell if the asteroid is instantiated by the spawn manager and has random movement or not (such as the asteroid at the start of game). Once this is clear, I’ll generate the kinematic properties of the rock. It is also necessary to evaluate the area outside which the asteroid has to be respawned in other position.

DefineAsteroidField is a method that will define the mentioned area, then a default rotation is assigned and then, if it’s the case, the kinematic properties are evaluated (InitAsteroid) and applied (Move). Let’s look into details.

Rect is a useful class: it represents a rectangle, with sides parallel to screen sides, defined by a vertex (the low left one, also called min) and its width and height. So I use the SpawnLimit calculations, already used for other objects, to determine all these things and define a rectangle which is a little bigger than the spawn limits.

Here we randomly select the asteroid properties. Starting from the scale, affecting its mass according to a simple law I found reasonable: since we are in 2D space, given a constant mass density (mass/surface ratio) if we scale a dimension, the mass will change proportionally to its square.

Then, we generate a random versor. Versors are vector of unitary length so basically I need to randomly generate points on a circle of radius 1, i.e. generate sine and cosine values, which range from -1 to +1. They will be respectively they and x components of the versor, which will be multiplied for the speed to get the velocity vector.

Here we apply the kinematics: we generate a random speed and angular speed and assign velocity and angular velocity to the rigid body. Since it is a 2D rigid body, only rotation around z-axis are allowed, hence the need of just a float and not also a rotation axis (mandatory for 3D rotations). These are initial kinematics conditions, they will remain unchanged if no force comes into play. In this game we will have impact with other asteroids and that will change the motion.

Now that we have the asteroid field and the motion, we can talk about respawn. This is the design:

  1. If the asteroid is outside the field, it has to be respawned (a sort recycle)
  2. The respawn will be carried out only if the asteroid is NOT exploding
  3. The respawn position will be a reflection with respect to asteroid field centre, but maintaining original motion.
https://github.com/daniele-quero?tab=repositories

Mimicking the spawning of enemies in terms of bools to check, we are going to apply some modification to the coroutine. Namely concerning the spawning position. The Spawn limits are calculated as usual and then the position is randomly chosen among four possibilities: the corners. Of course one can choose another logic and maybe in the future you’ll find it out on my repo.

Let’s finish with interaction with triggers: lasers, enemies, power ups and player.

According to the code, laser will damage for 1 life, so do entering the enemy (which is also destroyed on impact) and player (which is damaged for 1 life in return). Impact with power up will result in no effect while the shields will destroy the asteroid immediately. Score is gained only on manual destruction by laser.

Let’s now put two asteroids in the scene to watch them collide, bounce and respawn.

--

--

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