A Web Developer’s Attempt to Make Pong with Unity in One Night

I’m a solid developer. Don’t get me wrong, I’m not the best in the world, but I can think logically and solve problems. Still, I find myself in awe when I look at the work produced by the giants in our field.

As a web developer I, along with millions of others, depend on a few tools conceived in the mind of Linus Torvalds. Without Linux and Git, my job would be a lot more frustrating. But Linus is only the first of many people that come to mind. So many important languages and tools are the creations of single, brilliant individuals.

Perhaps most inspiring of all, yet often overlooked, are video game developers. My theory is because they often work in large teams they don’t always get the individual credit they deserve. Guys like John Carmack and the founders of Naughty Dog have worked on the bleeding edge of technology while creating software that has brought more joy to people than the latest front end Javascript framework ever will. You should read the story of how Naughty Dog created Crash Bandicoot. It’s incredible.

Getting Inspired

I’ve always romanticized game development. I think every kid who grew up playing video games has dreamed of building a masterpiece themselves. Even as an adult I sometimes dream about undertaking such an ambitious project.

These thoughts have inspired me to delve into that world as a bit of a hobby. You all have probably seen my previous game tutorials using web technologies, but I wanted to challenge myself a bit more with something different and closer to the real thing.

I decided to build pong in one night (or two) using the Unity Engine (Version 2019.2.18f1). Unity is C# based game development engine used by a lot of indie game developers. I’ve followed some in-depth Unity tutorials in the past, but never built something on my own. Let’s just say that the finished product is not quite ready for the market, but it’s as done as I care to be for this blog post:

Unity is free to use for individuals, and you’re welcome to download my project files below and boot them up. This post won’t be a full tutorial though, just a higher level overview of what I did and what I learned as I struggled with a new development environment.

How I Made Something Kind Of Like Pong

I started by following this helpful tutorial, and then customized it to be a more complete game. I find that to be a helpful tactic when solving new problems: find somebody to hold your hand at first, and then spread your wings from there.

Main Camera

Camera for Pong

I built everything with 3D objects that I created directly in Unity. The main camera is fixed in position and used for showing the game as if it were in 2D. This trick seems like it could be useful in other games as well.

Directional Light

Directional Light

This object was there when I created the project. I assume it is providing light to the scene, but I really don’t know how important it is!

Bumper (2)

Bumpers for Pong players 1 and 2

These are controlled by players one and two and are used to hit the ball back and forth. They are controlled by the following script:

Bumper script

Every frame we check to see if we need to translate the position of the bumper. If it is within the acceptable range we translate it by a factor of the speed and input direction of the bumper as well as the time in seconds since the last frame. Otherwise we force the bumper back to within its acceptable bounds.

Wall (2)

Walls for Pong

These are the outer walls that keep the ball in play. Pretty straightforward stuff.

Background

Pong background

A big black sheet providing a background that provides contrast with the rest of the game.

Ball

Ball

The ball is what the players are hitting back and forth. It has an attached script:

Ball script

This script initializes the position of the ball in the middle of the screen and sets it off in a semi random direction. The ball object also contains a method called “Halt” which will stop the ball and fix it in the middle of the screen. This is called when the game ends.

Goal (2)

Pong goals

The goals are where the players need to get the ball to. Each goal object has this script attached:

Goal script

The script checks for collisions with the ball. On each collision it checks to see how many goals have been scored and, if the game is over, displays the winning message and stops the ball in the middle of the screen. If the game is still going on then it resets the ball to the center of the screen and sets it off in a semi-random direction.

I also included a check for user input here on every frame. There is a probably a better place to put this, but I’m just a humble web developer bumbling my way through this. You can press “r” to reset the game, and “escape” to close the game.

Score

Score

I used 3D text objects for displaying the score. They look really bad. I imagine there must be a better way to do this. Here is the script to control them:

Score script

Every frame I’m just setting the score text to the appropriate number of goals.

Winning Message

Winning Message

Finally, the last object. I’m really tired of uploading screen shots. This message is invisible until the game is over. Then it displays either player one or player 2 as the winner.

Important Project Settings

I changed a few defaults in the project settings to make everything work reasonably well.

I had to make the physics bouncy by default.

Physics settings

I also had to setup keyboard controls for player 1 and 2 via the input settings.

Input settings

Lessons Learned

  • The physics engine is powerful and can help you a lot, but I need to understand it better to use it to its fullest extent. Sometimes the ball just rolls perfectly horizontal along the wall, and I’m not sure how to fix that. I’ve even managed to get the ball to stop entirely.
  • Zero gravity was important.
  • Collision detection is easy to get wrong. I know I need to use Rigidbody objects, but I don’t totally grasp the fundamentals quite yet. I had to increase the mass of the goals to a huge number to prevent them from moving on collision with the ball. I have the feeling I’m doing something silly there.
  • I probably could benefit from researching some Unity best practices. Often I solved the problems but felt uneasy about the solutions. Maybe something like this would help.
  • The 3D text objects look really bad. I bet there is a better way to do that.
  • Some of the design patterns I learned in web development are relevant, but I get the feeling that game development requires a different way of thinking a lot of the time.
  • A trusty search engine is your friend.