Nerd For Tech
Published in

Nerd For Tech

Creating the Bubble Blaster Part 1: Random Physics

My objective for today is to create a new type of special weapon for the player in my Unity game, to fire off at swarms of enemies. I want a stream of bubbles that float forward and up, with some randomization to get a more natural bubble cloud look.

The first thing I need is a trusty bubble asset.

I place the bubble PNG image in the assets > sprites folder in my Unity project and prepare it for import.

Maintaining the transparency of a bubble is really important, so I set the Texture Type to Sprite (2D and UI).

This bubble asset is going to be used as a projectile, so I give it a Circle Collider 2D component with Is Trigger set to be true.

After pressing the Edit Collider button in the above image, I edit the size of the collider in the scene view to be really tight around by bubble image.

I want this weapon to have a new physics based movement all of it’s own, so I create a new script called Bubble Blaster and attach it to my bubble game object.

I will eventually want the enemies to react differently on collision than with my Tusk projectile, so I create a new tag called Bubble_Blaster and assign it to my renamed Bubble Blaster game object.

Now that my new weapon is nearly setup, I make it into a Prefab by dragging it from the Hierarchy into the Prefabs folder.

At the top of my Player class in Visual Studio, I add a variable to hold the new Bubble Blaster prefab. I serialize the field for easy assignment in the inspector.

After saving in Visual Studio, I go back into Unity and assign the Bubble Blaster Prefab to the serialized field in the inspector on my Player script.

Back in the Player class, I add another variable of type bool, to change the bubble blaster to be active or not.

In the Fire Weapon method in my player class, the third else if statement checks to see if the Bubble Blaster is active bool is equal to true. If it is, Instantiate the Bubble Prefab at the position of the player. My regular tusk weapon now has an added condition to fire, which is needing the same bubble blaster bool to be set to false.

After saving in Visual Studio, I set the Bubble Blaster is Active bool to be true in the inspector, so I can test how my new weapon is coming along.

So far so good! I am using Tusk Ammo, which I will want to by-pass for unlimited ammo here soon. I also need some movement for my bubbles, and some on collision information so let’s get to it!

In the Enemy class, down in the OnTriggerEnter 2D Method, I check for the Bubble Blaster tag on collision. For now I just send a Debug.Log message to the console when the enemy collides with a bubble.

In my brand new Bubble Blaster class, I add an OnTriggerEnter 2D method, and check for the enemy tag. I also send a Debug.Log message to the console here.

An initial Debug.Log test shows that both the Blaster and Enemy colliders are detecting each other. Great!

Back in my Enemy Class, in the OnTriggerEnter 2D method, a few things happen now on collision besides just a Debug.Log message. The enemy destroys the other(bubble) object, sends the Debug message, tells the UI Manager to add 10 points, disables the collider, sets the sprite tint to blue, flips the sprite vertically and activates gravity.

In my Player class again, I want to differentiate my Bubble Blaster from my Tusk weapons, and that includes how fast it can fire. I make two more variables to give my Blaster a faster fire rate of it’s own.

To revise the Fire Weapon method, I begin with an if statement that checks all of the conditions having to do with the Bubble Blaster being active, as well as using the new fire rate. The else if part of the argument, handles all of my previous Tusk weapon behaviors if the Blaster is inactive. Also to note, is now there is no ammo being depleted when the bubble blaster fires.

Finally, it’s time to address the movement of the bubbles. In my new Bubble Blaster class, I add three variables to handle Thrust(min, current and max). I also add two variables for min and max values of the gravity, and a final variable to store the Rigidbody2D of the bubble.

In void Start, I use GetComponent to access the Rigidbody2D. The Thrust amount is assigned a random range between min and max values, as is the gravity. The thrust is the physics force that will propel the object, and the gravity value is what will float the bubbles toward the top of the screen. This happens in Start, meaning when each bubble Instantiates, it will be assigned a random value for those properties, and keep them for the life of the object.

It’s best to use Fixed Update to handle physics based movement, being it’s not framerate dependent. The bubble has force added to it, pushing it to the right, at the random amount of force assigned between min and max. The regular Update method destroys the object if it floats out of visibility range at the top of the screen.

Here is the new Bubble Blaster at work! As intended, it will act as some nice floating shield cover for the player to clear the screen behind. Now all I need is a powerup to activate the bubble blaster for a short period of time. More on that in my next article. Thanks for reading!




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

Distributed Systems (Part 3)_Time and Synchronization

Variables in Java

7 Reasons Why You Need to Ditch Excel as a Non-profit

How to Host Unity Games on the Web

Stack Overflow — A Great place for Junior developers

Server Provisioning with Vagrant and Ansible — Apache server setup Example

Split a String in Balanced Strings

Set Up Local Kubernetes Cluster with KIND (Single node as well as multi node)

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
Jared Amlin

Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.

More from Medium

Gamedev Log: “Slash’n’crack” #1 (Unity/C#)

A Brief Introduction to Cinemachine and Timeline in Unity

Voice Recognition using Dictionaries

Saving Data in Unity3D Using Files