UNITY FIRST GAME | PLATFORMER 2D (EASY) | Basics & movement

Published: 01 January 2024
on channel: Kotorman Entertainment
582
15

Instagram:   / kotorman_entertainment  
Telegram: https://t.me/kotorman_games
Google Play: https://play.google.com/store/apps/de...

Hi! Today we are going to create your first simple game using Unity. We will learn the basic structure of the editor, setup a scene and make a player movement script. This is one of the tutorial series for complete beginners. Subscribe for more content!

Great! Now it’s time to create a GameObject. It’s simple – just drag sprite into the scene. Excellent! Object appeared in Hierarchy view. Let’s call it “Player” inside Inspector. Now you can launch your game, wow we did it!
Each GameObject could have a list of Components – special code blocks that perform some operations. I want to add physics to player, so I can attach Rigidbody2D. It will add mass, gravity, drag etc. Now our player falls down, wonderful!
I think it’s a great idea to make some floor. Drag another sprite. Add component called Box collider that makes object solid. You can edit bounds, but in my case Unity automatically resized them to perfection.
Scale it a bit using Transform component. Also you can use these tools: View, Move, Rotate, Scale, Rect, and Universal tool. Switch between using QWERTY keys.
Now launch your game, as you see, player still falls through obstacle. That’s because he also needs a collider to interact with floor. Add collider to Player. Launch again – it works, great!
But player doesn’t do anything, game is quite boring. Let’s add an ability to control our character. In previous steps, we used built-in components, but now it’s time to create your own. Add folder, name it, then click Create, C# script. I called it Player, but you can give any name you want. Double click the file and you will launch Visual Studio.
If it doesn’t work, check the menu Edit, Preferences, External Tools, and here you should select your code editor.
There are two functions. Start is called immediately after game launch and executed only once. Update method called each frame. For example, if your game runs at 60 FPS, then Update will be executed 60 times per second. There are lots of other built-in methods, but now we need only two of them.
To make player move, let’s add speed variable, for example, 3. At the beginning, let him just move right forever. In update method, type transform Translate Vector3 right multiply by speed. we take component called Transform, and say him to move object to the right. Vector3 dot right has the length of 1, so we need to multiply it by speed. Now we need to attach script to player gameobject. Click on Player, and drag our script into the Inspector. You added new component to player by drag and drop. Launch game. As you see, player flown away so quickly that it’s hard to notice that. Now player moves 3 units per frame. So we need to multiply it by Time delta Time. That will turn the speed of player into 3 units per second.
Test again. Now all is good.
Let’s increase the floor length, just copy paste floor blocks. Great, character has more space to move.
And now I can control the character to move left and right, amazing! That’s your first simple game.
If you open editor, you will notice that our variable become visible in inspector. Now you can change character speed during the gameplay and find optimal value. But remember that all changed variables will return to initial values after game end.
Also I can change the background color. Select camera – the object that make you able to see all objects during gameplay. Then click on Background and select optimal color. You can switch to game view and see color changes in real time.
Finally, I think our happy cube would like to jump. Jumping is a mechanic that requires control through the Rigidbody. Add public Rigidbody 2D rb. Also we need some force, let it be 10. You can’t just create Rigidbody with constructor. To attach it, you should go to inspector and drag the Rigidbody component into that field. Great, now it linked with our script.
I will control jump with Control key, but you can assign any other button. And now, we should apply force to the object up, multiplied by the amount of force. Launch the project and click jump key – yes we did it! Now our cube can jump high to skies!
Now we have a problem – if you hold the button, character will jump higher, but how to jump only once from the floor? Well, I’ll show you that in the next tutorial, where we will improve current code and add some more mechanics. Subscribe to our channel, play our games, and see you soon, thanks for watching!


#unity #gamedev #games2024 #design #2d


Watch video UNITY FIRST GAME | PLATFORMER 2D (EASY) | Basics & movement online without registration, duration hours minute second in high quality. This video was added by user Kotorman Entertainment 01 January 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 582 once and liked it 15 people.