How I recreated a ping pong game playable on browser on my own(most part).
Hi all,
I started learning how to make games in Unity a few months before. I chose Unity because it has the most user base and is ideal for a small crew or one person and in my case, that fits my situation. and Unity has tons of tutorials and a very active community, so if I needed to know how to do something, there's already someone who asked that question and somebody has probably given the answer in comments. since I had no prior knowledge of programming I had to choose an engine that runs on a beginner-friendly programming language. unity supported c# which is a beginner-friendly language I heard. so ultimately I chose unity. I followed some tutorials but nothing worked out, people are suggesting me to recreate some simple games to learn Unity properly. so I made ping pong.
made using veed.io
first, I need to create a player you can control with basic input, and since the player only moves up and down, an arrow up and down or W and S keys would suffice. so I created a 2d square sprite and adjusted its scale to my need which is a bit lower on the x-axis and longer on the y-axis.
then I added physics into the sprite by adding rigidbody2d and box collider 2d components. they define the shape and how the physics affects the object. but now I need to make it behave like the player object based on our inputs. so i made basic input actions mapping for WASD and arrow keys.
after creating the action map now I need to create the code to move the player when the input has been triggered.
I first made some variables t store the speed of the player, and to reference the input action map I created and the rigid body component I added to the player. then i use getcomponent method to refer the rigidbody 2d in awake method. I also set paddle(player) limit for 4.1f which is the ingame unit for distance, so the player cant go above or below that distances aka screen.
I used transform.position method for movement, there are more ways to make the player move such as adding force to rigid-body, transform.translate, etc. We only need the player to move on Y-axis so I freeze the x-axis movement in rigid-body component. when I press w or s, the input action map registers it as +1 or -1, depending on the input, the code add the value or subtracts the value from the y-axis of the player object, thus moving the player on y-axis depending on our input.
Then i made top and bottom boundaries by creating a static physics object and scaling it to match screen size. i also made side walls but they are not rendered, they only have hitbox and when they register hits it resets the ball and give the score to who hit the walls.
i used separate script for game manager which is responsible for keeping track of the score and resetting the game and ball, its also responsible for some Ui elements such as game over screen and you win screen.
then i created the ball, main element of this game, nailing the speed and natural feel of ball was very hard at first, but it took some time to create it. in unity editor i created a dynamic physics object and created some code to add force to it at starting of the game.
I made some calculations to make it more challenging to predict the movement of the ball, when you hit eh ball with paddle, the force of the ball get more faster progressively. I also added some code to add force to the ball at the start of the game. i also delayed adding the force so that players have some time to prepare. i used the start coroutine method to delay the balls force. also ball checks the name of the object it collided and triggers the code depending on it. example if it collided with boundary it wont do anything, if it collided with side walls , it calls the function to reset the ball positions and add the score to the object that hit it to the side walls. when the ball hits player or enemy , the balls velocity gets altered and gains more speed.
Finally i added enemy AI to predict and move towards the ball. since I am a beginner i was having hard time coding an ai by myself , so i asked one of my friends to help me. he helped me in coding the enemy by adding some calculations to predict the ball and move towards the ball. he sent me those calculations to predict the ball. it worked but it was not fun at first so i altered the code and added some my own code to alter the prediction time and speed of the enemy.
as you score more points. the more speed enemy gains and hard to beat the enemy. then the game felt more fun and challenging. after adding the enemy, the game is almost finished and the final stages of exporting the game are very boring and hard part, I have to sit around and export it many times until I get the right screen view on the devices, after researching I found a tool called cinemachine and it is very useful to keep the scene view always on screen regardless of the size and resolution of the screen.
Finalli i exported the game into a WebGL build . if you want to play the game Click this link to play it on browser.
If you had any idea on what game to make and had any doubt about this post, let me know in the comments
Hope you learned something new and had fun reading.
Thank you and Have a nice day!!!.
TEAM 1
Congratulations! This post has been upvoted through steemcurator04. We support quality posts , good comments anywhere and any tags.Thank you for the support!!!.
Hi @vickyspartan, it is nice to see you active again after a long time. There are a lot of changes since you are back. The is a good support that provided by the Steemit Team through various Initiatives like Steemcurator04 that I am in a team of and also there is a Booming support that we have in WOX. The users who do Power Up they have priority for such support, therefore, I would encourage to Power up your payout. Also it would be good to know what country you are from, many users put it as a tag, that also makes easy for curators to know that you are a serious user. You can put your country on the top of your blog. Hope that helps,
I am from india, sure I can put my country as tag on next coming posts, but i never seen an indian curator supported me, to be honest, I've never received support except from this community, so I'm only posting content in this community for the time being. Correct me if I'm wrong.Thanks for the support once again.
@vickyspartan, my dear friend I can understand your feelings;
but the fact is steemit is a family for all.
where we can't categories county, for me every steemian is my family member.
You can join us for further more details but believe me, if you work hard with honestly, people will certainly noticed you, just keep patience.
Our discord link: -
merai dia discord link
TEAM 1
Congratulations! Your Comment has been upvoted through steemcurator04. We support good comments anywhere..Thank you for suggestions, I'll keep them in mind and be patient and work hard.
It is nice to find that you are from India, there is a community called: "Incredible India"
https://steemit.com/trending/hive-120823
Also @sduttaskitchen she is an Admin of that community. It would be good if other curators also notice you.
Here is a post of Steemitblog with challenges of the week, also all the news about any supportive Initiative you can find in @steemitblog
I am grateful @stef1 for mentioned me, I will surely look into this matter.
Incredible india community seems friendly, I'll definitely participate in the contests. Thank you.
Wow, thank you for exposing me to this community. I really appreciate it.
Hello
@vickyspartan
I am the moderator of the Incredible India community
So it would be our pleasure to have you in our community.
Our discord:- Meraindia discord link
Regards,
@piya3 (moderator)
Incredible India community
I am more than happy to join this community, I'll provide as much as engagement as possible, thank you for this oppurtunity.
Most welcome my dear honourable friend.