Day 6… Little Wins

Adrian Wilson
10 Unity MVPs in 30 Days…
4 min readMay 8, 2021

--

The objective — make a game that made its debut in the early 70’s, Pong. The point of doing the 10 MVPs in 30 days is to not only be more comfortable with the challenges that programming throws at you, but also to be able to say I’ve created something and I did just that.

The start was simple, I needed to make a gameboard, luckily the entire game consisted of one 2D solid sprite duplicated and stretched into different lengths and made different colors.

Player moves vertically, I attached a Rigidbody2D to it and gave it vertical input and speed aka velocity.

I didn’t want to make player two have controller inputs instead I wanted it to be a computer opponent. This was the most complicated part of code as there is a plethora of ways to get the computer to move which brought a lot of trial and error, however, landed on the MoveTowards method and had the computer move towards the ball’s position on the Y axis.

This unfortunately wasn’t flawless, first I couldn’t seem to get it to only be the Y axis the computer moved on. After about thirty minutes I decided to tie it down knowing in a real project this type of code would be unacceptable.

The other bug I was encountering was the computer would build too much momentum and end up bouncing extremely fast eventually flying off the screen. So again unable to create a perfect fix for it I cheated with code.

Basically if it started to bug out I would reset it’s position then freeze it quickly unfreezing it later and killing its momentum.

Next was to get the ball moving, I did this similarly to the players movement I added velocity.

Thus far everything has a collider and physics materials (turning off friction and on bounciness) for the ball to bounce. Now we need to make the interface and goals to calculate the score. The interface consists of two texts for the scores and two buttons; draw to reset the ball when it gets stuck, and quit.

I made a UI manager and made it into a singleton for easy use.

Now to add score I made the method in the UIManager and called it whenever the ball hit the green wall behind the player/computer.

UIMANAGER SCRIPT:

BALL SCRIPT:

As you can see in the code I also reset the ball whenever someone scored.

I also started to code how to make the ball move faster throughout the game however the balls velocity is only called during the start of the game or when it’s reset which made it difficult to get the speed moving, here’s what I tried before thinking of a better way.

It didn’t work, I thought about the physics materials and since the ball had a bounciness of 1 if it were to hit off an object with higher bounciness it would inevitably move faster. I made the player and computers bounciness to 1.10 and it worked! Every time the ball bounced off the player or computer it picked up speed and reset it’s speed every time there is a goal scored.

To my surprise that is the game completed and I have the win for the day.

Today’s goal:

Move onto the next project and write a concept to a target shooter game.

Thank you for the read —

Adrian.

--

--