Example: Scripting Pong

To build Pong, let’s break the game down into its core elements: we need a ball that ricochets back and forth between the paddles at increasing speed, we need a scoreboard that knows when the balls have passed the paddles, and we need a mechanism for restarting the ball when that happens. A good first step would be to add a non-kinematic rigid- body to the ball, two kinematic rigidbodies to the paddles, disable gravity for all of them, and assign an appropriate physic material from the standard assets (‘bounce’ with ‘bounce combine’ set to ‘multiply).

Below, you can view the script for the ball with explanatory comments. The ball needs to accomplish some basic goals: it should bounce in a complicated pattern, always maintaining movement on both axes, and it should accelerate at a challenging but not impossible pace in the horizontal direction.


Next, we need to script our paddle, which you can, again, view below. The paddle needs to move up and down in response to key presses (but not outside certain bounds). It also needs to trigger the particle system when it collides with something.


Next, we need enemy AI: something that will cause the enemy’s paddle to track towards the ball at a fixed rate. For that, we’ll be using Vector3.Slerp for maximum simplicity. We’d also like the same particle behavior that we see on our own paddle.


Finally, we need a script to update the scoreboard and reset the ball when it goes out of bounds.


With those scripts attached and the references filled in, when we run our game of Pong, we experience gameplay!

You can download my Pong demo, if you’d like to see everything I’ve outlined in action. It runs on Windows, Mac and Linux systems.