Building a PONG Game in Unity
Following along with the tutorial, I was able to understand better what basic functions are needed when building a “simple” 2D game in Unity.
I found it surprisingly difficult to to align the walls just perfectly pixel for pixel (this is also a struggle with any adobe product as well). I wonder if there is a better way to match the edges.
I found extreme joy when my rackets moved after added the initial script to them. #smallvictories
However, in PART 4 – when adding input, the initial settings for “VERTICAL” were not as they were in the video tutorial. This was the data that I saw:
No up/down in negative and positive. I do not know why. So I changed the settings according to the tutorial.
Then I realized there are actually 2 verticals outside of my Vertical2, and I had opened the second vertical. Why are there two verticals? Why are the settings different for them?
I applied the axis information and pressed play for the game and nothing moves but the screen. Code is exactly how it is in the tutorial. I went back to the input and saw that the Vertical2 name had not been changed when I went back and redid it. I tried play again and both sides moved with w & s and only the right side with the up/down keys. I checked this out – turns out the “alternative” buttons in the Vertical input had the w & s marked. (This is a great way to cheat – should I always play on the left hand side.) #fixed
Honestly the biggest struggle thus far is manipulating the rewind/forward option on the tutorial video. Very hard to go back to the correct spot in the video.
On to the Ball!
I understand that having a small mass on the rigid body makes sense to not move other objects when hit, HOWEVER I want to return to this later and see what happens if that happens.
Scripting the Ball
I created a new script and re-named it. However when I opened the script it was still calling its default name. So I just re-named it “Ball”. When the coding was done, it did not work on the ball. I got an error when drag and dropping it. I have a feeling it had to do with the name issue. So, do-over. Take 2 with the script and on play the entire game is frozen and not even the rackets work. Turns out “2D” was missing form <Rigidbody2D> in the code.
Adding the collision option to the script for the ball, initially it gave me an error. I went and fixed the typing errors. But then when I played, I was not getting any log and I could not move any of the rackets. All I heard was that error noise. It was because in the Debug.Log I put “bracket” not “racket” – I wonder why it didn’t work, when it should be able to log whatever is put in the parenthesis. However this is all very strange. When it was working right at this point, the ball was moving VERY fast. When I pressed play again without making any changes, the ball moved normally and again I was unable to move the rackets. And then without changing anything, it was working again. When the ball hit the rackets it spun wildly out of control. Also it is not logging any of the racket hits when they are hit. This is all very bizarre when everything is exactly as it is in the tutorial. I have gone over this various times already. I watched again the tutorial and I see that in that code it is actually asking to log “right bracket” and “left racket” – now I know it shouldn’t make a difference, but I am going to just make it the same anyway.
I am getting sometimes a crazy bounce when the ball hits the rackets in a certain way, and sometimes normal. Here is a video of where it changes halfway through:
Getting into Code Questions:
Now onto adding the Calculate Position code. Not sure how it knows racketPosition when we have no “racket” declared, rather, we have leftRacket & rightRacket. Also I am not sure why this math is in place for this calculation. Is this a standard physics calculation or something standard with unity?
Why does the code understand “direction” when we did not declare this in the script?
It was at this point that my ball with through my bottom wall! Unclear why, as all 4 walls have the box collider component. I tried again and it did not. Maybe my eyes are seeing pixels that are not there.
After I added in the code for the restart-scene, and rechecked it multiple times, I am getting an error to fix the code. I did a test run in the text editor and got this message:
Mystery solved. Needed “==” not “=”.
I got it to work, however, instead of resetting to a new scene, it just reset to the pong board without the ball. I am guessing that this is a similar but different version of the re-render issue on the tutorial.
Upon layering the items in the hierarchy, it worked!
Adding a different song and a different sound effect really changed the play-experience compared to that in the video game. (I used an old folky Italian song, so instead of being transported back to the 80s, I went back to – PICTURE IT, Sicily, 1921…
TESTING OUT MY OWN GAME
I am testing out my own game based on the structure of the last game, with a different storyline and possibly some new features.
I struggled with aligning the background center with that small white box in middle of the canvas that acted as the view guide.
I made my own assets and starting bringing them in.I created new rackets, “ball” and sampled music for the collision noise. I followed the steps per the tutorial. And suddenly somewhere along the way, my right and bottom walls disappeared. Looks to be a random layering issue. So I layered my hierarchy items now. I added the racket script and they are working!
I tested the “ball” out at a speed of 15 and he died pretty quickly before reaching the “racket.” Alas! It was a mass/interpolate/gravity/material issue on the ball. I went in and adjusted those and changed the bouncy-ness to 1.5 -it creates a crazy mad ball bounce that is gradual depending on how many times it the ball hits the rackets. Exponential is the word I am looking for.
I went ahead and changed the code to match the asset names in this particular scene.
I wanted to make a way for the game to pause mid-scene by using the spacebar. For some reason I have developed a pet peeve for websites and programs that do not offer this functionality. I found a great tutorial to set this up.
At this moment I paused myself to go and organize my assets, as the area was getting cluttered. I did this while the game was in play and learned that you CAN adjust the asset folders and it will not undo during play mode. Phew.
Before moving on, I tested out the game again and the “ball” got so chaotic it disappeared off scene and never came back – and the game did not reset. Not sure what that bug was. But I did see it when building the original pong game.
I started to import sound and had to re-save everything from .m4a to .mp3.
Then I added the sound and the spacebar pause function stopped working. I needed to add an empty component and bring in the pause feature to that. Also – it will not pause the sound. But that is fine. I want the song to keep going at this point.
I had the goal to create an opening scene where it leads to the game mode. After working with this tutorial for a while, and not having luck with the UI and text with everything from the text didn’t show up to not needing everything the tutorial was doing, my unity finally crashed.
I tried several more ways to create a start scene that leads to the main game, but that was not successful. I know there must be a way to make a simple title page.
Finally I found this lovely simple tutorial that I was able to create a button to start the game with!
My Fiend, Mr. Fish
Mr. Fisc (Mister Fis) is the long-time nemesis of Sammy “Milkbone” Dad and his poodle cohorts. No matter when or where, Mr. Fisc evades being caught. Even when he is ponged between two poodles!
Code for Pause Feature:
Questions for future:
In order to get new assets from the repo, am I supposed to fork again? Do I have to do this every time new data is added to the repo?
Using this same racket code structure, how can I get forward and backward movement? I tried this code that is // out, but it did not work. Cannot get a new axis input in the input manager.
How do I create a small animation with a static image?
Code for Adding Start Screen
Leave a Reply