Pong Extended Instructions

Creating Text on the Screen

To create a text field that displays on the screen, go to the Hierarchy window.

Press the Create button > UI > Text

Note that the UI Canvas is different than where you were working on your game! You might have to zoom out.

Settings for Text Script:

Place your text element in the correct space within the UI square (not your camera square).

Example UI Element:

Rename the Text to LeftScoreDisplay.

A note about the next section: PAY ATTENTION TO UPPER AND LOWER CASE LETTERS.

Select your Left Wall, and add a new script LeftScore.

We're going to add two variables to the script: a counter for the score (leftScore) and a reference to the UI Text (leftScoreText). We have to include using UnityEngine.UI; at the top so that Unity knows what a UI Text is.

Code for LeftScore script:

Changing the Ball Direction

For our ball object, we would like to add in a random direction, rather than always having it go right.

We need to open up the Ball script and alter it to set the ball's velocity to a new random direction. We pick a new random angle between zero and 2*Pi (because we need radians, rather than degrees). To change velocity, we create a new Vector3 with three new component arguments. We set the x-component to be the cosine of that angle, and the y-component to be the sine of that angle. (The z-component doesn't change from zero.) We replace the direction in the start of the Ball script to use this new method, rather than just going to the right.

Code for newRandomDirection in Ball script:

For the wall, when there is a collision with the wall, we need to jump the ball back to the center. We need to alter the position of the ball. We do this in an OnCollisionEnter2D event, which is triggered when the ball enters the collision box of the wall.

In the LeftScore script, we add the jump which puts the ball back in the center by changing its position Vector3. We'll need a reference to the Ball object, which will go at the top (attachedBall). For the jump, we change the x-value of the ball, but leave the y and z values the way they are. We will also add one to the score and update the text in the display.

Make sure you attach the UI Text to the leftScoreText variable by dragging LeftScoreDisplay from your Hierarchy into the leftScoreText box in the LeftScore script.

Also, drag the Ball object from your Hierarchy into the attachedBall variable in the LeftScore script.

Code for Moving the Ball to the Center in LeftScore script:

Finally, we want to pause the ball in the center, before it starts moving again. To do this, we set the Ball's velocity to zero on the collision, start a timer for two seconds, then pick a new random velocity for the ball object.

Timers involve starting a coroutine, then saying what will happen after a delay. Coroutines allow timers to start without the entire game stopping, while still allowing some action to happen out of sequence.

Complete LeftScore script:

For your right wall, you will need to develop a similar script, and add a UI Text for the right side score.