Unity Tutorial – Make a 2D Bubble Wrap Popping Game Using Touch, Sound and Particles

Hi, do you like popping bubble wraps but always seem to be out of the good ones to pop? Well thats all history now, you can have your own infinite amounts of good bubble wraps to pop all day long.  Lets make a simple app/game in unity that will populate the screen with Bubble Wraps and we can pop them using the touch controls. Also we want the scene to reset once we popped all of them. It would be nice to have some sound and particle effects to go with it.  Yep we will do all of those for this tutorial.

Lesson objectives –

  1. Make a bubble wrap popper project in unity 2d
  2. Controls the sprites and 2d objects using touch controls
  3. Use 2D array to populate the camera area with columns and rows
  4. attach the sprite, sounds and particle to the single script
  5. add the game objects to a list and search through them
  6. reset all of the game objects if all the bubbles been popped in the scene
  7. play a pop sound
  8. create a particle from scratch called burst
  9. play a particle animation
  10. destroy the particle when its done playing
  11. enjoy endless bubble wraps, you’re welcome

Video Tutorial on How to make a Bubble Wrap Popper in Unity using C#

 

 

Download The Sprite Images and Pop sound here for this project 

 

Grid Manager Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System.Linq; // add this for the All search with the LIST

public class GridManager : MonoBehaviour
{

    int[,] grid;
    int vertical, horizontal, columns, rows;

    public Sprite fullBubble;
    public Sprite poppedBubble;
    public AudioSource sound;
    public GameObject particles;

    List<GameObject> bubbleCollection = new List<GameObject>();


    // Start is called before the first frame update
    void Start()
    {
        vertical = (int)Camera.main.orthographicSize;
        horizontal = vertical * Screen.width / Screen.height;
        columns = horizontal * 3;
        rows = vertical * 2;
        grid = new int[columns, rows];

        for (int i = 0; i < columns; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                SpawnBubbles(i, j);
            }
        }


    }

    private void SpawnBubbles(int x, int y)
    {

        GameObject tempObject = new GameObject("X" + x + "Y" + y);
        tempObject.transform.position = new Vector2(x - (horizontal + .5f), y - (vertical - .5f));
        var tempSprite = tempObject.AddComponent<SpriteRenderer>();
        var tempCollider = tempObject.AddComponent<BoxCollider>();
        tempSprite.sprite = fullBubble;
        bubbleCollection.Add(tempObject);

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {

                if (hit.transform.gameObject.GetComponent<SpriteRenderer>().sprite == fullBubble)
                {

                    hit.transform.GetComponent<SpriteRenderer>().sprite = poppedBubble;
                    sound.Play();

                    GameObject particle = Instantiate(particles, hit.transform.position, Quaternion.identity);
                    particle.GetComponent<ParticleSystem>().Play();

                    if (bubbleCollection.All(o => o.gameObject.GetComponent<SpriteRenderer>().sprite == poppedBubble))
                    {
                        Debug.Log("All the bubbles are popped!! now resetting");

                        foreach (GameObject x in bubbleCollection.ToList())
                        {
                            x.GetComponent<SpriteRenderer>().sprite = fullBubble;
                        }
                    }

                }
            }
        }
    }
}

 

 

 




Comments are closed.