Unity2D Circle Chain prototype step 2: adding explosions

Here we go with the second step of the Unity2D Circle Chain prototype. In the first step we saw how to:

* Create your 2D project
* Set up a custom resolution
* Import your graphic assets
* Create prefabs and add them to the game at runtime
* Create C# scripts and assign them to prefabs, together with graphic assets
* Update sprites position

Now, we are working on the game adding explosions, learning these new concepts

* Detect mouse click
* Get mouse coordinates
* Add tag to prefabs
* Select prefabs with a given tag
* Initializing prefabs with custom variables by communicating with them
* Destroy prefabs

In the same way you created the green circle prefab in first step, create a prefab called redbullet with the image of the red bullet and a C# script called redBulletBehavior.

To add four bullets when the player clicks the mouse, change placingCircles.cs – the Main Camera script – this way:

I highlighted the new lines, let’s see what they do:

Line 7: adds another global variable to instance the red bullet prefab, as already made with the green circle

Line 20: this is how we detect if the player pressed mouse button

Lines 21-22: get mouse coordinates, remember (0,0) is in the middle of the stage while mouse coordinates have the origin at the bottom left of the stage

Line 23: preparing the loop to create four bullets

Line 24: creation of the bullet itself

Line 25: placing the bullet where the player pressed the mouse

Line 26: sending a message. This will mean we are going to call Initialize function inside red bullet script (redBulletBehavior.cs) passing i*Mathf.PI/2 as argument.

Now that you have a new globar variable, don’t forget to drag red bullet prefab into the new placeholder of the script in Main Camera inspector. Everything is explained in first step.

Now, redBulletBehavior.cs is similar to circleBehavior.cs:

I would point you on these lines:

Line 16: here is the function called with SendMessage method in placingCircle.cs.

Lines 25, 28, 31, 34: this is how you can destroy the current object

Line 36: having to select all green circles to test for collision with red bullets, I added a “GreenCircle” tag to green circles, then selected them all with FindGameObjectsWithTag method. How can you add a tag to a prefab?

Really easy, in the Inspector choose Tag -> Add Tag then create a tag name – GreenCircle in this case – then again Tag -> GreenCircle.

The rest of the script is really similar to the creation of red bullets, it’s just that now we are creating green bullets, whose script greenBulletBehavior.cs is basically the same as redBulletBehavior.cs.

And this is the result:

Click on the stage to fire red bullets and hopefully start a chain reaction.

Next time, we’ll add some particle effect to give this old game a twist, meanwhile download the entire project.

  • Wow! Great way of prototyping! It is not very easy, but for game developers it’s really important to have a prototype that will clearly show if your concept is worth development

  • The thing is, the unity player is not cross platform- for example all i see above is ‘and the result:’ followed by a big blank spot :)

    I wouldn’t depend on this to demo or promo your game.