How Make Pong Game in Unity
Unity is a powerful and widely-used game development platform and engine known for its versatility, ease of use, and ability to create stunning interactive experiences across various platforms. With Unity, developers can build games, interactive applications, simulations, and more for a wide range of platforms, including mobile devices, consoles, desktops, and even virtual reality (VR) and augmented reality (AR) devices. Its cross-platform capabilities make it a popular choice for game developers, indie creators, and large studios alike.
To create a project in Unity you will need to have Unity and Unity Hub installed. You can download it from Unity's website. To complete the project, follow this tutorial from start to end.
1. Create Project:
First, we open the Unity Hub and Create a 2D Project.
Let's get started by setting up our project in Unity:
Open Unity and follow these steps: From the welcome screen, click on "Projects". If Unity is already open, click on "File" > "New Project" instead Click on "New". Then you should see a dialog box like this: Name the project, for example, "Pong Game". Make sure to choose the "2D Template". Set the slider for "Enable Unity Analytics" to "Off". Click on "Create Project".
Once the project is created, a 2D grid should appear in the Scene view. Setting up Unity in 2D mode adjusts the game camera for a 2D perspective and imports images as Sprites, which will be helpful.
Remember the Unity Pong assets file you downloaded? Unzip it to find a folder named "unitypong-assets" containing various assets. Select all these files and drag them into the Project pane at the bottom of the Unity window.
Drag the "Background" image from the Project pane into the Hierarchy pane, just below "Main Camera". It should appear in the Scene view and be centered by default. If not centered, set its Transform Position to (0, 0, 0) using the Inspector pane.
With the Background selected in the Hierarchy pane, make the following adjustments in the Inspector pane: Under Transform, change its scale to (0.75, 0.75, 1) for a better apparance. In the Sprite Renderer component, manage the rendering order by creating a new sorting layer called "Background" and placing it above the "Default" layer.
Re-select the Background object in the Hierarchy pane and choose the new "Background" sorting layer for it in the Sprite Renderer component.
Make some improvements to the Main Camera: Select the Main Camera object in the Hierarchy pane. Under the Camera component in the Inspector, change the Size to 3 for a slight zoom effect. Click on the "Background" property of the Camera and set the color to black (RGBA: 0, 0, 0, 0) to cover the edges of the screen if the background isn't large enough.
Save your progress by pressing Ctrl+S (Cmd+S on macOS) to save changes made to the current Scene.
By following these steps, you've set up your project, imported necessary assets, adjusted camera settings, and prepared the background for your Pong game in Unity.
2. Make Paddles:
Now, let's move on to creating our paddles:
Locate the image named "PongPaddle" in your Project pane.
Drag the paddle image from the Project pane onto the Scene view. This should create a PongPaddle object in the Hierarchy menu.
Click on the PongPaddle in the Hierarchy. In the Inspector, rename it to "Paddle1" Set its Position to (-4, 0, 0) and its Scale to (0.5, 1.5, 1). Set the Tag dropdown to "Player".
Add components to the Paddle1 object: Click on the Add Component button, then select Physics 2D. Add both a Box Collider 2D and a Rigidbody 2D. These components enable collision with the ball and allow the paddle to move.
Customize the Rigidbody 2D component: In the Inspector, change the Body Type dropdown to "Kinematic". This prevents the physics system from affecting the paddle's movement.
public KeyCode moveUp = KeyCode.W;
public KeyCode moveDown = KeyCode.S;
public float speed = 10.0f;
public float boundY = 2.25f;
private Rigidbody2D rb2d;
Now, let's create a script for paddle movement: Make sure Paddle1 is selected in the Hierarchy pane.Go to Add Component > New Script, name it "PlayerControls", and click Create and Add.Double-click the script icon to open it in Visual Studio.
Inside the script, you'll find several lines of code. Let's break down the key parts:
void Update () {
var vel = rb2d.velocity;
if (Input.GetKey(moveUp)) {
vel.y = speed;
}
else if (Input.GetKey(moveDown)) {
vel.y = -speed;
}
else {
vel.y = 0;
}
rb2d.velocity = vel;
var pos = transform.position;
if (pos.y > boundY) {
pos.y = boundY;
}
else if (pos.y < -boundY) {
pos.y = -boundY;
}
transform.position = pos;
}
Define variables for paddle movement keys (W and S), paddle speed, upper and lower bounds, and the Rigidbody2D component. In the Start() function, set up the initial state, including initializing the Rigidbody2D. In the Update() function, handle user input to move the paddle up (W key) and down (S key), and ensure the paddle stays within bounds.
Save the script in Visual Studio.
Return to Unity and play the game by clicking the play button (►) at the top of the screen. Use the W and S keys to move the paddle. This enters Play Mode and lets you test the game mechanics in real-time.
Note: Changes made during Play Mode are temporary and will be lost once you exit Play Mode. Always exit Play Mode before making significant changes to your game.
After testing, stop Play Mode and make any desired adjustments to the key bindings, speed, and other settings in the Inspector.
To create the second paddle, duplicate Paddle1:
By following these steps, you've created and customized paddles in Unity for your Pong game. This includes adding components, setting up physics properties, scripting paddle movement, and testing your game in Play Mode.
3. Make Ball:
The Ball in our game is more complex than the Paddles as it involves bouncing, scoring detection, and interaction with paddles. Let's go through the steps to set up the Ball:
Drag the Ball image from the Project pane into the Hierarchy, similar to how we added the Paddles. The new object will be named "Ball".
Add a custom Tag to the Ball: In the Inspector, click on the Tag dropdown > Add Tag. In the Tags & Layers window, click the + icon to add a new Tag called "Ball". Select the Ball object in the Hierarchy, go to the Tag dropdown, and choose "Ball".
Adjust the Ball's scale to (0.5, 0.5, 1) for a smaller, faster appearance.
Add components to the Ball: Click the Add Component button, then under Physics 2D, add a Circle Collider 2D and a Rigidbody the Circle Collider, set the Radius to 0.23 for a tighter collider around the ball. Apply the BallBounce material to the Circle Collider 2D. This material ensures minimal friction and maximum bounce, giving the ball a lively behavior.
Configure Rigidbody 2D settings: Adjust the Rigidbody 2D settings to achieve the desired ball behavior. Refer to the provided image for the specific settings.
Create the BallControl script: Select the Ball object in the Hierarchy, go to Add Component > New Script, and name it "BallControl". Double-click the script to open it in Visual Studio.
Inside the script, you'll see several sections:
Recommended by LinkedIn
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour {
private Rigidbody2D rb2d;
Import packages and class declaration. Declaration of variables used in the script. The GoBall() function that starts the ball moving in a random direction.
void GoBall(){
float rand = Random.Range(0, 2);
if(rand < 1){
rb2d.AddForce(new Vector2(20, -15));
} else {
rb2d.AddForce(new Vector2(-20, -15));
}
}
The Start() function that initializes the Rigidbody 2D and calls the GoBall() function with a 2-second delay using Invoke().
void ResetBall(){
rb2d.velocity = Vector2.zero;
transform.position = Vector2.zero;
}
The ResetBall() and RestartGame() functions used for handling win conditions and restarting the game.
void RestartGame(){
ResetBall();
Invoke("GoBall", 1);
}
The OnCollisionEnter2D() function that adjusts the ball's velocity based on its collision with paddles.
void OnCollisionEnter2D (Collision2D coll) {
if(coll.collider.CompareTag("Player")){
Vector2 vel;
vel.x = rb2d.velocity.x;
vel.y = (rb2d.velocity.y / 2) + (coll.collider.attachedRigidbody.velocity.y / 3);
rb2d.velocity = vel;
}
}
The script is responsible for ball movement, collision handling, and game restart functionality.
With these steps completed, you'll have functional paddles and a bouncing ball. The provided script "BallControl.cs" should resemble your finished script.
By following these steps, you've successfully created and configured the ball for your Pong game in Unity. The ball is now capable of bouncing, interacting with paddles, and responding realistically to collisions.
4. Make Walls:
At this point, you might have noticed that the ball can easily go off the screen. To prevent this and create boundaries, we'll set up walls around the game area.
With the walls in place, the next step involves creating the game's core elements such as a score system, score display, win conditions, and a reset button. This phase is crucial in transforming your project from a ball-bouncing scene to a fully functional game.
5. Scoring Interface:
To proceed, let's establish a Heads-Up Display (HUD) for our game. Follow these steps to set up the HUD and integrate it with the game mechanics:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public static int PlayerScore1 = 0;
public static int PlayerScore2 = 0;
public GUISkin layout;
GameObject theBall;
void Start () {
theBall = GameObject.FindGameObjectWithTag("Ball");
}
public static void Score (string wallID) {
if (wallID == "RightWall")
{
PlayerScore1++;
} else
{
PlayerScore2++;
}
}
void OnGUI () {
GUI.skin = layout;
GUI.Label(new Rect(Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1);
GUI.Label(new Rect(Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2);
if (GUI.Button(new Rect(Screen.width / 2 - 60, 35, 120, 53), "RESTART"))
{
PlayerScore1 = 0;
PlayerScore2 = 0;
theBall.SendMessage("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
}
if (PlayerScore1 == 10)
{
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
} else if (PlayerScore2 == 10)
{
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
}
}
using UnityEngine;
using System.Collections;
public class SideWalls : MonoBehaviour {
void OnTriggerEnter2D (Collider2D hitInfo) {
if (hitInfo.name == "Ball")
{
string wallName = transform.name;
GameManager.Score(wallName);
hitInfo.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver);
}
}
}
By following these steps, you'll have successfully set up the game mechanics, scoring system, and HUD elements for your Pong game. This marks a significant milestone in transforming your project into a functional and enjoyable game.
6. Setup Game:
To make your game playable outside of Unity, follow these steps to create an executable file:
In the Player Settings, you can:
For example, you can uncheck all aspect ratios except 16:10 and 16:9 to ensure your game displays correctly.
After configuring the settings, click the "Build" button and choose where you want to save the executable file (e.g., desktop). Give it a name (e.g., "Pong v1.0"). The executable file will be created, and you can find it at the location you specified.
Congratulations! You've successfully created a playable version of your game outside of Unity. Enjoy playing your game and feel accomplished for completing this tutorial. If you're interested in learning more about Unity or coding in general, be sure to explore further resources. If you'd like to see a completed version of the code, you can check out the sample code on GitHub.
If you like the article please 👍 it, wants to refer somebody 📤 with him/her. We also provide Services of 2D/3D Game Development, 2D/3D Animations,Video Editing, UI/UX Designing.
If you have questions or suggestions about the game or want something to build from us, Feel free to reach out to us anytime!
📱 Mobile: +971 544 614 238
📧 Email: wahhab_mirza@vectorlabzlimited.com