How Make Pong Game in Unity

How Make Pong Game in Unity

Unity is a powerful and widely-used game development platform and engine known for its versatilityease 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.

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

Camera Component

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.

Rigidbody

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:

  • Right-click Paddle1 in the Hierarchy and choose Duplicate.
  • Rename the duplicate to "Paddle2".
  • Adjust the key bindings and Transform position (change X to 4) to place Paddle2 on the right side.
  • Run the game again to ensure both paddles can be moved separately.

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:

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.

  1. Create a container for the walls: Make sure nothing is selected in the Hierarchy pane. Right-click in the Hierarchy pane and select Create Empty. Name the new empty game object "Walls" and set its position to (0, 0, 0).
  2. Create individual wall objects: Right-click on the "Walls" object you just created and choose Create Empty. Name this new object "RightWall". Add a Box Collider 2D component to the "RightWall" object to enable ball bouncing.
  3. Duplicate the wall object: Duplicate the "RightWall" object three times. Name the duplicates "LeftWall", "TopWall" and "BottomWall".
  4. Position and size the wall objects: Set the position and scale of each wall object to create boundaries. For "RightWall", set Position to (5.8, 0, 0) and Scale to (1, 6, 1). For "LeftWall", set Position to (-5.8, 0, 0) and Scale to (1, 6, 1). For "TopWall", set Position to (0, 3.5, 0) and Scale to (10.7, 1, 1). For "BottomWall", set Position to (0, -3.5, 0) and Scale to (10.7, 1, 1).
  5. Organize the hierarchy: Clicking the arrow next to the "Walls" object in the Hierarchy allows you to hide or show the individual wall objects, helping to keep the hierarchy organized.
  6. When you play the game, you'll notice that the ball now bounces off the walls, staying within 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:

  1. Create a HUD object: Right-click on an empty area in the Hierarchy pane (outside the Walls game object). Choose Create Empty and name the new object "HUD". Set the position of the HUD object to (0, 0, 0). Add a new script to the HUD object called "GameManager."

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {        

  1. Define variables in the script: Import required packages and declare the class. Declare integer variables to keep track of the scores for both players. Create a GUI object variable to handle graphical user interface elements. Establish a reference to the ball object.

public static int PlayerScore1 = 0;
public static int PlayerScore2 = 0;

public GUISkin layout;
GameObject theBall;        

  1. Implement the Start() function: Use the Start() function for initial setup.

void Start () {
    theBall = GameObject.FindGameObjectWithTag("Ball");
}        

  1. Define the Score() function: Implement the Score() function, which gets called when the ball hits the side walls. This function will be triggered by another script we'll create shortly.

public static void Score (string wallID) {
    if (wallID == "RightWall")
    {
        PlayerScore1++;
    } else
    {
        PlayerScore2++;
    }
}        

  1. Implement the OnGUI() function: The OnGUI() function handles displaying the score and the reset button functionality. It checks if either player has won and triggers the ResetBall() function if needed.

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);
    }
}        

  1. Implement ResetBall() and CheckWin() functions: ResetBall() is responsible for resetting the ball's position after a player scores. CheckWin() checks if a player has reached the winning score and ends the game if so.
  2. Set up GUI Skin: Create a GUI Skin by right-clicking in the Project pane and selecting Create > GUI Skin. Name it "ScoreSkin". In the Inspector pane for the ScoreSkin, assign the downloaded "6809 Chargen" font to the Font variable. Adjust the font size for Labels and Buttons as desired.
  3. Assign the GUI Skin to GameManager: Go to the HUD object and select the GameManager script component. In the Inspector, assign the created ScoreSkin to the Skin variable.
  4. Script for side walls:Create a new script called "SideWalls" and attach it to both LeftWall and RightWall objects. Implement the OnTriggerEnter2D() function to detect collisions with the ball and call the score method in GameManager.

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);
        }
    }
}        

  • Configure Box Colliders:
  • Make sure both the LeftWall and RightWall have the "Is Trigger" checkbox selected on their Box Colliders in the Inspector.
  • Test the game.

  • Play the game to ensure that both players can score points.
  • Verify that the HUD displays the score and reset button correctly.

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:

  1. Go to the "File" menu at the top of Unity.
  2. Select "Build Settings".
  3. Choose the platform you want to build for (Mac, PC, Linux Standalone).
  4. Ensure that your main scene ("Main") is included in the build by clicking "Add Open Scenes" if needed.
  5. Click on "Player Settings" to configure additional settings for your build.

In the Player Settings, you can:

  • Add your project's name.
  • Choose an icon for your executable (such as the Ball sprite).
  • Uncheck "Default Is Full Screen".
  • Set a default screen width and height (e.g., 960x540).
  • Adjust supported aspect ratios to ensure proper display.

For example, you can uncheck all aspect ratios except 16:10 and 16:9 to ensure your game displays correctly.

Settings

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


To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics