2D Pool in Unity | Game Dev Tutorial

This is a very simple tutorial on how to make a pool/billiards/snooker game. You can use it as a starting off point to make a more complex game and learn about 2D Colliders and Rigidbodys.

Getting Started

The first step is to watch “DRAG & SHOOT in Unity” from Muddy Wolf Games and make the project. We are going to use that exact project and add pool elements on top of it.

“DRAG & SHOOT in Unity” from Muddy Wolf Games

Setting Up

Now that you have the drag-and-drop system set up, we can add some pool.

The first thing to do is delete the platform from the tutorial project. To make the table copy this image and drag it into your assets folder. Then drag it onto the scene. If you want to make your own, pool tables are typically a 2:1 ratio. Resize as needed to fit your screen size.

poolTable.png

Click on the image in the Hierarchy and add a Rigidbody 2D a components. Set the Body Type to Static. This will let it collide with other objects, but never move.

Rigidbody2D on the pool table

Next we’re going to make a Physic Material. Right-click in your assets folder and create a new Physic Material.

Creating a new Physic Material

Select the new Material and change the Bounce to 0.8. This lets the balls bounce off the walls. If you want to add more bounce, just increase be Bounce value! Leave the Friction at at 0.4.

Now select the pool table again and add a Polygon Collider 2D component. This sets the pool table’s collider on the visible areas of the object’s sprite. Since we’re using a PNG with transparency it ignores the the middle part.

Drag our newly-created Material into the Material field of the Polygon Collider 2D. At this point save everything and run the game. You should be able to hit the ball around the table. Colliders and Rigidbodys can be tricky, so double-check the values of those if things aren’t working.

Adding the Balls

Next select the ball and make some change to the Circle Collider and Rigidbody components. For the Circle Collider add the same Bounce Material we made earlier.

On the Rigidbody change these values:

  • Mass: 2
  • Linear Drag: 0.5
  • Angular Drag: 0.5
Making alterations to the ball object

These settings change how the ball will interact with the environment. Heavier objects will have more impact, and the drag settings determine how much friction there is. Move the values down for a lighter ball that will move more and faster, or increase them for a heavier one.

Duplicate the ball and remove the Drag and Shoot script. Save up, hit Play, and try hitting the ball. It should bounce around when you hit it and respond to the walls in the same way. Duplicate the balls a few times and you have a pocketless game!

Adding Pockets

Duplicate one of script-less balls. Delete the Bounce Material and check the Is Trigger box.

Circle Collider for pockets

Make a new script called BallManager. Select all the balls except the cue ball and drag this script onto them. Open it up and replace it with this:

using UnityEngine;

public class BallManager : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Destroy(gameObject);
    }
}

This says that any time the ball comes into contact with the pocket (i.e. the area defined by the Circle Collider) it triggers the function and destroys itself.

Save your work, hit Play, and you have pool! This is where the tutorial ends.

Ideas for Where to go Next

  • Add an AudioSource to the pool table to play a sound when a ball collides. Use the OnCollision2D component to access the collision in the same way we did with the pockets.
  • Add sound to the ball in the same way.
  • Change the drag position to the cue ball.
  • Replace the ball on the table when it goes in a pocket.
  • Make it into a real game.