2D Pong Game Tutorial – Unity3D (C#)


2D Pong Game Tutorial – Unity3D (C#)

pong

 Play Demo

We will make a classic 2D Pong Game in this tutorial.

Open a 2D project in Unity and begin to create our objects. We need a ball, a paddle, a brick and 5 borders. Place 2D borders, 2D paddle and 2D ball as seen on picture below. Add Box Collider 2D to borders and paddle and Circle Collider 2D to ball. In additional, add Rigidbody 2D to ball and paddle.

Screen Shot 2015-03-10 at 16.45.57

Rigidbody of PADDLE should seem like

Screen Shot 2015-03-10 at 16.56.10

and rigidbody of BALL should seem like

Screen Shot 2015-03-10 at 16.56.20

We use 0.0001 mass for ball because we don’t want ball to push paddle on collision. And we don’t need gravity force in this project.

Create a Brick prefab to instantiate bricks on run time. Create a new tag named Brick and assign it to Brick prefab. We will use this tag to destroy bricks when ball hits them.

Create a C# script named Paddle. We make paddle move on X-axis between borders with this script. Write code for keyboard input.

Using FixedUpdate() for moving Rigidbody items is better than Update(). Attach this script to paddle and test it. Your paddle should move right and left direction and it should stop if it hits wall.

To make ball bouncing on walls,Screen Shot 2015-03-10 at 17.03.18 bricks and paddle, the easiest way is, create a 2D Physics Material named Ball and edit it like this. Assign it as material of ball’s collider.

 

Create a C# script named Ball.

Attach it to ball and test it. Your ball moves and bounces on wall and paddle. Now we should different moves for different hit points on paddle. Divide the paddle for 3 parts. If ball hits middle part, it bounces fixed angle. If ball hits right or left part, it moves right or left direction. We manage this movement by comparing ball’s X-position and paddle’s X-position. It means, if (ball’s X-position) – (paddle’s X-position) < 0, hit left part, if (ball’s X-position) – (paddle’s X-position) > 0, hit right part, if (ball’s X-position) – (paddle’s X-position) = 0, hit middle part.

Create a function to get hit part.

And call this functon in OnCollisionEnter2D()

We need some bricks. Lets instantiate them. Create new script or add bricks in Paddle script.

Destroy bricks when ball hits them in OnCollisionEnter2D() of ball script.

Enjoy it!

Download source code here.

©Coffee Break Codes – 2D Pong Game Tutorial – Unity3D (C#)


Leave a comment

Your email address will not be published. Required fields are marked *

6 thoughts on “2D Pong Game Tutorial – Unity3D (C#)

  • Gleb

    Thanks for this tutorial, but have some troubles reproducing it myself. The paddle keeps going through walls, ignoring 2d box colliders. Any hints how to fix this?
    Thanks in advance.

      • Gleb

        Actually it has been unselected at start, so no it does not help.

        After some search I have found out that transform.Translate kind of teleports object rather that actually moves it using physics engine. Therefore colliders should *not* be expected to work when using it.

        I have managed to make them work, however, by using Rigidbody2D.velocity instead of transform.Translate. Setting Rigidbody2D.velocity.x to n if RightArrow is pressed, to -n if LeftArrow is pressed and to 0 if none of those arrows are pressed worked fine.