Unity 2D Platformer for Complete Beginners - #1 PLAYER MOVEMENT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a brand new tutorial series where you'll learn how to make a 2d platformer even if you are a complete beginner in these videos i want to focus on teaching you the basics of unity while making a game that has movement jumping wall jumping and sliding a shooting mechanic multiple types of enemies and traps a basic stat system and multiple levels keep in mind that what you see on the screen right now is just something that i made quickly in one day just to get the basics working the final game will look much better and have a lot more features so on this note we can start the video the first step is to install unity hub which is a program that allows you easily to manage all your unity versions and projects once that's done open the hub and click installs on the left then press the add button in the top right corner of the window and add unity 2020. if you have a slower pc you can pick 2018 or 2019. alright next up we have to choose all the modules and additional programs that we want to install first of all i recommend you install microsoft visual studio if you don't have it yet it's the program that we will use to write our code after that feel free to pick any platform that you want to build for i usually go for android ios mac windows and webgl basically what these modules do is that they allow you to take the games that you already made and put them on these platforms as a playable game when you're ready to continue accept all the conditions press done and wait for it to install once it's finished select projects in the top left corner then press new in the top right corner once you're done with that you're gonna see a scene that tells you to pick a template for this game we're gonna go with 2d because we're making a 2d platformer then you need to choose a name for your project and the location when you're ready to continue press create and wait for unity to load now we can take a look at the layout on the left side you will see the hierarchy window it includes all the objects that are currently present in your level so let's say that you have a level with one player three enemies four walls and a couple of obstacles and collectibles all of them will be present in this window to demonstrate how it works we're going to create our player to do this you need to right click in this window then select 2d object and sprite which is basically a 2d image once you do this you're gonna see a new sprite object pop up in the hierarchy window you can right click it or press f2 to rename it to player now we can select the player object by left clicking it you're gonna see that it becomes blue once you select it now we can assign an image to our player to do this you need to press on the sprite button on the right side and then select the background image for now now you see a small white square in the middle of the screen and this is our player and obviously it's not good so let's change it right now we are in game view which basically shows us how our game will look on a screen but to actually edit the object we need to go to the scene view most of our work will be done in this window so make sure to remember its position we need to zoom in on the player and to do this we have two options first one is to double click the player object in the hierarchy window and the second one is to use the mouse wheel once you're done with this i want you to focus on the transform component in the top right corner every game object in unity will have a transform component and it will allow you to change its position rotation and size for example you can see me moving the player left and right by changing the x position up and down by changing the y position and forward and backwards by using the z position which you can't currently see because it's a 2d game but in case you're making a 3d game you're gonna know you can also play around with the rotation and scale of the object in case you want to see how to rotate it or change its size once you're done with this make sure to reset all the positions and rotations to zero and all the scales to one next up i want to talk about the project window which you can see down below this window contains all the folders and files that you have in your entire project not just in new currency so every time you want to import a new image or a 3d model or write a new script that does something it's going to go into your project and you can use this window to navigate and find what you need i'm pointing this out because when you have big projects it's very easy to lose stuff because you misplaced it or you just forgot to put it in the right folder and because we don't want this to happen let's give some structure to our project first rename the scenes folder to levels and sample c into level 1. once you're done left click on assets then right click to create two new folders the first one will be called sprites and it's going to contain all the images in the game and the second one will be called scripts and it will contain all the c sharp scripts that will make our game work now let's import our first image into the game i have a black square that i'm going to drag into the sprites folder i'm going to put the link in the description in case you want to use the same image as i did next up open the sprites folder by left clicking it then select the player in the hierarchy and drag the new square into a sprite field now you will see that the screen is black which is caused by the fact that the player is too big so zoom out a bit if you want to see how big the player is in the game you can switch back to the game view and now we can adjust the scale of the player point five looks good to me so i'm gonna set it to 0.5 on all axes alright we have a player now we can create the ground i'm going to switch back to the scene view and select the player object in the hierarchy and i'm going to duplicate it by either pressing ctrl d or command d as a result we have a player 1 object which i'm gonna rename to ground now we need to change the position and size of the ground an easy way to do it is to press this button on the top bar which is going to allow you to drag the object around and change its size by dragging the edges when you're done you can check out the game view to see if everything is looking as you expected now let's get this to work select the player in the hierarchy then press add component on the right side once you see the popup you can type in box and select box collider 2d make sure to pick the 2d variant this is important if you zoom in on your player now while you're in scene view you will see that it has green edges which means that anytime this object is going to collide with another object that has a collider it's not just gonna go through it for example our player will not hit the ground and fall through it will hit the ground and stop now that we have a collider on our player we need another one on the ground as well so make sure to choose the ground press add component and selecting box collider 2d again we're getting very close to the result now now our player has a box collider which means it's going to register collisions but it's not going to be able to fall to the ground or move around because it doesn't have a physical body yet to solve this issue we need to select the player press add component and select rigid body 2d a rigid body does exactly what i just told you it gives an object a physical body that can be influenced by outside forces for example gravity if you press the play button on the top part of your screen now you're gonna see the player falls to the ground when you're done testing make sure to press the play button again to exit play mode otherwise your changes will not be saved the next step is to make our player move left and right and to do this we need to create our first c-sharp script make sure to open the scripts folder first right click create and see sharp script we need to pick a name so let's call it player movement now in order to attach the script to the player object we need to select the player and drag the player movement on it once you're done you can double click the player movement script so we can write some code every time i open a script i prefer to delete everything i don't need inside it so i can get it to a clean state this is just a personal preference but you can do it if you like then we can actually start planning on how to properly move our player we remember that we gave our player a rigid body to d now we can use it in order to apply forces to make the player go left and right in order to use the rigid body in code we need to create a reference to it this is why i created this variable of type rigidbody to d that has the name body now every time we're going to use the rigidbody from our player component we're going to type in body and call some functions but this is only half of the work now we need to show our code where to take this body from in this case i will use an awake method as you can see in the description right here awake is called every time the script is loaded so every time you start the game the script is being loaded and the wake method is being executed on to the next part which involves using the get component method you can see that i typed here body equals get component rigidbody2d but how does this exactly work and what does it do well in this case the player movement script is attached to the player object and the git component method will check the player object for a component of type rigidbody2d in this case it's going to find it right here take it and store it inside the body variable so this is how git component works keep it in mind because it's one of the most widely used methods in unity in order to get references you can actually use it to get references not only to rigid bodies but to all types of components like colliders audio sources etc our next step is to detect when the player presses left or right and move the body in the relevant direction to achieve this we're going to use the update method just like awake update is a method that's pre-built in unity which means that unity will run it automatically but unlike the awake method update does not run only when you load the script but it does run on every frame of the game which is great in our case because we're trying to check when the player presses left or right imagine if we were trying to check for arrow inputs only when the game starts in this case you would basically press the left key but the game would not do anything except on the first frame this is why we need update here it's going to make sure that on every frame of our game inputs are being recorded and the player is being moved accordingly alright next up i will use body dot velocity in order to directly change how fast our player body is moving and in which direction as you can see here in order to assign the velocity we need to use a vector which is basically a collection of numbers because we need to assign speed in all three directions left and right up and down backwards and forwards for the first coordinate which is actually the left and right movement i'm going to use a property called input.getaxis then i'm going to open the round parentheses and type in horizontal between quotation marks to help you understand what this thing does i made this sketch so basically input.getaxis horizontal is a value defined by unity and every time you press the left button or the a key it goes to minus one and every time you press the right button or the d key it goes to 1. i like to use this value whenever i can because it helps me avoid if else statements which usually are a sign of that code ok this allows us to move left and right for the second and third variable i'm going to use body.velocity and body.velocity.z which just means that i don't want to change the body's velocity on the y and z axis now when you go back into unity you're gonna see a red error message below this happened because of my mistake the player's rigid body is 2d which means its velocity is defined by vector 2 not 3 and it also means that we need to delete the body.velocity.z now when you go back into unity you will see that the error goes away and when you actually press play you can move the player square left and right but you can also see that the player is moving at an abysmally slow speed so let's fix that don't forget to exit play mode and go back into visual studio to solve our problem we're going to create a float variable that we're going to call speed and we're going to multiply it with the input.getaxis horizontal to make it easier to test and change the speed of a player i want to make it directly editable from unity and we're gonna do this by opening square brackets and typing in serialize field before the speed variable when you go back into unity and select the player you're gonna see in the bottom right corner that the speed is currently zero let's set this to 10. now you can press play and test out the new speed keep in mind that even when you play the game you can change the speed variable and you will see the updates right away the final thing for today's video is implementing the jump mechanic to achieve this we're also going to use the update method but in this case we're going to use an if statement to check if the space key is pressed you're going to type in input.getkey which is a boolean variable that returns true when the key is pressed and false when it's not and inside the curly brackets we have keycode.space and i want to point this out because you can use this same method to check forever key inputs you just need to change the space part for example you will type in keycode.a to check for the a key input and keycode.b to check for the b key input it's extremely intuitive now that we know when the space key is pressed we need to define what happens when it is and what we want to do here is change the body's velocity again but this time we don't change the velocity on the x-axis we change it on the y-axis so inside the vector we will type in body.velocity. so that our body maintains the same speed horizontally and for the second parameter we're gonna use speed which we know is equal to 10. and finally we can press play and test this out you can see that the player is going left and right and jumping around happily like it's supposed to do but we still have a few issues to solve primarily it's the fact that if you hold down the space button or if you press it repeatedly your player is going to fly off into space but this video is long enough so let's leave this for the second episode which is coming the next week i want to thank all of you for watching i talked a lot during this episode but this was because i was trying to explain everything thoroughly so you can understand everything let me know if you prefer this approach or if you want me to do more and talk less also make sure to subscribe and hit the bell if you don't want to miss the next episodes alright stay safe and keep making games [Music] you
Info
Channel: Pandemonium Games
Views: 63,958
Rating: 4.956624 out of 5
Keywords: Unity 2D Platformer for Complete Beginners, unity complete beginner, unity complete beginner tutorial, unity 2020 complete beginner tutorial, unity beginner 2021, unity 2d player movement, Unity 2D player movement 2020, unity 2d platformer tutorial 2020, Unity 2D Platformer episode 1, unity platformer complete beginner, Platformer for Complete Beginners, unity 2d platformer tutorial 2021, unity 2d tutorial, unity 2d game, unity 2021 beginner tutorial, unity 2d platformer
Id: TcranVQUQ5U
Channel Id: undefined
Length: 15min 48sec (948 seconds)
Published: Fri Dec 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.