Using the Environment to Start the Game in Unity

Take advantage of the game element to trigger actions

Rusben Guzman
2 min readJun 26, 2021

Objective: Create an asteroid and start spawning when the player destroys it.

Let’s drop the asteroid sprite in the scene and create a sprite for this object, in the script we are gonna add a constant rotation movement using the function transform.Rotate().

In the spawn manager I started the spawning in the Start() method, now we have to move this code to a public function StartSpawning().

For visual purposes we are gonna create an explosion prefab to instantiate it when the player destroys the asteroid. Is as easy as the first step, we just have to drop the first sprite, create the animation and drop the object to the prefab folder. We can erase it from the scene because we have to instantiate it later.

Going back to the asteroid script we have to create a reference to the spawn manager game object and for the explosion prefab. After that we have to use the OnColliderEnter2D() function to check for the laser, call the function to start spawning and destroy the asteroid.

Good to go!!! Now we can try the new functionality, select play and start the real game after destroying the asteroid.

--

--

Rusben Guzman
Rusben Guzman

Written by Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

No responses yet