2D Pong Game Tutorial – Unity3D (C#)
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.
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.
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.
1 2 3 |
float Hitpart(Vector2 ballPosition, Vector2 paddlePosition) { return (ballPosition.x - paddlePosition.x); } |
And call this functon in OnCollisionEnter2D()
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.
1 2 3 4 5 6 7 8 9 |
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; } } |
Destroy bricks when ball hits them in OnCollisionEnter2D() of ball script.
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#)
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.
“Is Trigger” box should be unselected in colliders.
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.
How to make a 3D pong game in unity
Pingback: Создание игры 2d Pong
.