Pong Clone!
About
Here is my first proper game made from scratch. It's pretty simple, just a clone of the game "Pong". I only had to create one original asset for this game, a simple 10x10 white square. I was then able to import this into Unity and manipulate it to create the ball, walls, paddles and mid-line.
Once all the scenery and assets were in place, I could start writing the scripts for each object. The paddles needed to be controllable by the player. They only needed to move up or down, the controls for which are already included in Unity. One player could control their paddle with W and S, while the other player used the Up and Down arrows.
I wrote a basic player logic script that decided which paddle would use which controls.
if the paddle is named "player1" then use the Vertical control scheme (W and S). Otherwise, (if the player is player2) use the Vertical2 control scheme (Up and Down).
So there, player controls complete. Each paddle also had their X and Z axis' frozen, so that they were forced to only move up and down and not be spun around by the ball if it hit them. Each paddle also had a Rigidbody2D component added to them so that they could actually interact with the ball.
The ball was the most complex component of this game. It needed to know which goal it entered, how to interact with the paddles and how to launch itself at the start of the game.
I used a script to cause the ball to start moving in a random direction at the start of the game. I also modified this script to reset the ball's position when it made it into either of the player's goals.
So at this point I had the most basic form of pong. Now I needed to add a scoring system. I found the best way to do this was the write a new script for the ball that determined which player to add a point to when the ball passed through either of the goals.
This obviously relied on 2 TMPro objects to exist in the game, so I created a canvas and added both objects into it. At this point, the game really is starting to look like Pong.
So now when the ball passes through the left goal, the right player scores a point and vice versa for when the ball passes through the right goal. I actually didn't realise until writing this blog that the score counter was inadvertently counting the number of times a player had missed a shot and allowed the ball to pass through their own goal, which obviously isn't quite right...
And with that bug now resolved, the game is now finished. 2 players can now duke it out in this intense 1v1 tennis game!