2D Pong Game Tutorial – Unity3D (C#)

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.

Rigidbody of PADDLE should seem like

and rigidbody of BALL should seem like

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.

void FixedUpdate () { if (Input.GetKey(KeyCode.RightArrow)) { transform.Translate(new Vector2(0.1f, 0.0f)); } if (Input.GetKey(KeyCode.LeftArrow)) { transform.Translate(new Vector2(-0.1f, 0.0f)); } } 1 2 3 4 5 6 7 8 9 void FixedUpdate ( ) { if ( Input . GetKey ( KeyCode . RightArrow ) ) { transform . Translate ( new Vector2 ( 0.1f , 0.0f ) ) ; } if ( Input . GetKey ( KeyCode . LeftArrow ) ) { transform . Translate ( new Vector2 ( - 0.1f , 0.0f ) ) ; } }

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, 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.

private float speed = 2.0f; void Start () { rigidbody2D.velocity = Vector2.one.normalized * speed; } 1 2 3 4 5 6 private float speed = 2.0f ; void Start ( ) { rigidbody2D . velocity = Vector2 . one . normalized * speed ; }

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.

float Hitpart(Vector2 ballPosition, Vector2 paddlePosition) { return (ballPosition.x - paddlePosition.x); } 1 2 3 float Hitpart ( Vector2 ballPosition , Vector2 paddlePosition ) { return ( ballPosition . x - paddlePosition . x ) ; }

And call this functon in OnCollisionEnter2D()

void OnCollisionEnter2D(Collision2D c) { if (c.gameObject.name == "paddle") { float x = Hitpart(transform.position, c.transform.position); Vector2 vec = new Vector2(x * 5, 1).normalized; rigidbody2D.velocity = vec * speed; } } 1 2 3 4 5 6 7 8 void OnCollisionEnter2D ( Collision2D c ) { if ( c . gameObject . name == "paddle" ) { float x = Hitpart ( transform . position , c . transform . position ) ; Vector2 vec = new Vector2 ( x * 5 , 1 ) . normalized ; rigidbody2D . velocity = vec * speed ; } }

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

public Transform brick; void Start () { for (int i = 0; i < 5; i ++) { Transform pf = Instantiate(brick, new Vector2(-2 + i, 1), Quaternion.identity) as Transform; } } 1 2 3 4 5 6 7 8 9 public Transform brick ; void Start ( ) { for ( int i = 0 ; i & lt ; 5 ; i ++ ) { Transform pf = Instantiate ( brick , new Vector2 ( - 2 + i , 1 ) , Quaternion . identity ) as Transform ; } }

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

if (c.collider.tag == "Brick") { Destroy(c.gameObject); } 1 2 3 if ( c . collider . tag == "Brick" ) { Destroy ( c . gameObject ) ; }

Enjoy it!

Download source code here.

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