Unity Crash Course: Basics of 2D Game Development | Christopher Navarre | Skillshare

Unity Crash Course: Basics of 2D Game Development

Christopher Navarre, Software Instructor

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
10 Lessons (47m)
    • 1. What to Expect in this Crash Course

      0:50
    • 2. Unity Basics: Scenes, Game Objects, and Components

      2:07
    • 3. Making a Player Game Object

      5:35
    • 4. Adding a Game Background

      3:14
    • 5. Tilemap Level Building

      7:54
    • 6. Building User Interface (UI) Functionality

      6:15
    • 7. Simple Platformer Player Movement

      5:01
    • 8. Building User Interface (UI) Functionality

      6:15
    • 9. 2D Sprite Animation Basics

      9:50
    • 10. Wrap Up

      0:27
16 students are watching this class

About This Class

This course is meant to introduce you to the Unity game development engine with a series of sequential tutorials that show you many basics of the editor while getting to the start of something that resembles a functional 2d platformer. It's a very short course so rather than trying to explain everything, in this one we focus on learning the essentials for absolute beginners. The videos are kept short, sweet, and easy to understand.

Topics covered include

  • Unity specific basics like understanding Scenes, GameObjects, Components, and how they tie together
  • Adding in a player to the game with movement, sprites, and an animation controller
  • Example functionality of UI buttons and learning the how Canvas UI elements work different from normal game objects in 3D / 2D space.
  • Some basic scripting concepts like finding game objects in the scenes, modifying component properties, and setting property values in the Unity inspector.
  • Basics of Prefabs - Essentially stored copies of a game object and their components that can be reused anywhere in your game.

Prefab art assets for the first few videos can be found hereĀ https://assetstore.unity.com/packages/2d/characters/sunny-land-103349

Transcripts

1. What to Expect in this Crash Course: Hello, everybody, Christer. And in this video, I want to give you guys a quick start into the Unity Game development engine. So some of the topics I want to cover really quickly in this video just to give you guys a rough overview of what can be done within unity include explaining game objects and scenes , game objects, being objects with that existence site of the scenes and have components attached to them to give those game objects functionality. So we'll be talking about components such as strike renderers, which allow you to vendor an image for a game object or a canvas you element tile mapping in order to draw your game levels with two D tiles, the very basics of how to set up a animation controller with animations a tiny bit about see shop scripting in creating a player movement script. And we'll also be talking a little bit about the game world versus the canvas You I, which layers on top of everything 2. Unity Basics: Scenes, Game Objects, and Components: So when you create your first unity seen, you're going to see in the hierarchy the scene name itself and under that is going to be the main camera game object. Do you know you're looking at a game object? Because next to it is going to be this little cube shape and you can left click onto a game object in order to see details about it showing up in the inspector, which by default, is on the right hand side. So under the main camera game object, you have different components, which contain either default unity, functionality or custom scripts, which you've created. And you attach multiple of these components generally to a single game object in order to give it off the things that it can do. So what every game object is going to have is a transformed component or, in the case of the U element, it will have a direct transformed component for normal game objects. In the transform component will have position notation and a scale. So before you add other components to a game object, it has nothing really attached to, except for the position in three D space and whatever rotation are scaled that a game objects should have. But without extra components, like a Sprite render. These aren't really going to do anything. You can see that the main camera game object has a camera component to it, which obviously allows it to viewer certain area of the game world. And render that to the screen and you'll notice I'm on the game tab in this preview area, and what it's previewing right now is the output of the main camera, so you can see that by default. This has a blue background, which, of course, is something you could change your different color by clicking on the color selector quite easily. But if we go into seeing view, it's going to look a little different. And probably what you saw at the start was this scene view, so I can double click on the main camera, and what you'll be able to see here is that gives most indicators showing basically the bounds off the camera view area. But there's nothing else inside of the game scene now. Generally, a game is going to consist of multiple scenes to you might have a scene for the main menu. You might have a scene for each level, or just one main scene, which you would load the quote unquote levels on top off, but at a base level. What? The scene is just a default collection of game objects which are going to be loaded into it by default. 3. Making a Player Game Object: So inside of my Assets folder in the bottom project area, you'll see that there's a package here called Sunny Land. So I picked this up from the assets store. It's really available, and on the asset store, you can actually find a lot of things, such as two D Sprite hacks, which may contain animations for a character. Scripts for adding functionality to your game without you having to code everything yourself. What three D models if you decide to go with a three D game, but just note that you can use the Unity Assets Store if you want to be, do some of the amount of time you have to put into the game by coating everything from scratch. Um, and by the way, for Windows like this in Game and seeing in the Inspector. If you don't ever see any of these, you can open them up in the window menu so the asset store could be window and then asset store and then other tabbed windows can be opened up in categories such as General over here, so once you have some are assets for your game, and you may create that in Photoshopped ASAP. right? Or for three D models, something like Blender. Then you can load those into your game as asset files. If you wanted to get started by creating a new game object for a player, you could go into the artwork for your game, find the strikes and generally you'd want to start with the Idol Sprite. So I'll just drill in here and you have player one idle. You can bring that into your game, seen as a brand new game object, and you'll see that with this game. Object and immediately attach is a sprite Grender there, and it will have that image you dragged into the hierarchy attached. We can double click on that, and you can see that it is actually really tiny right now inside of the game. If we go to the game view, you can see that the strike is rendering really, really tiny, and that's probably going to be too small for the game. So one way of adjusting this would be to increase the scale of your game objects, but might have to do this for every game object. If you're bringing in these small strikes, you can also click on each of the images that you bring in for your project and change the pixels per unit from 100 to something smaller, like 16 or 32. But if you do that generally gonna want to be consistent, so you want to apply that to all images in your game, and you can do a multiple selection off, argue images and then change the value at one so you don't have to do it individually. But another option, if you only want to change things once, could be to go to the main camera and to decrease the camera size. So if I drop this from 5 to 1, you'll see that the camera view space is now a lot smaller. But it's still gonna be putting that out to whatever screen size you have. So in a sense, your game objects will look a lot bigger because the camera is showing less at once. So with this game object, I could recreate everything from scratch here, adding components to it. But I have a bit of a prefab already created, so it Free Fab is a game object which is saved to your assets folder and will contain defaults for a certain type of player, enemy or other game object within your game. So with three fabs, you can actually drag them into any scene, and you could create multiple duplicates if you want it as well. But you contract these into your scene and copy basically the defaults for this prefab straight into your scenes. So you can see here that this prefab has a strike. Render has a capsule collider, a Vigen body and a player controller script, and we'll get into those more in a minute. So that could be a bit of a time saver if you ever want to be used any of your game object assets and once you've created something as a prefab, but you want to be able to go into an editor where you can change the defaults for the prefab, you'll be able to see that the game object in the hierarchy now has a blue Cuban set of a white cube, indicating that this is a prefab and they'll be an arrow over here on the right so we can click on that arrow, and this brings us into a prefab editor. So now whenever we make changes to this prefab and this mode. Those changes will be applied across the board to any instance of the treetop or a copy that's inheriting values from the pre vamp. Now you can assign overrides, which means that the copy will have a different value than the prefab, even if you change it over here. But you can completely define which properties for which copies should inherit from the prefab or have their own values. So why I'm in here editing the prefab? You may have noticed that the capsule collider and we should go over this scene view in order to better see our prefab. If I click on the game object over here, you can see this green capsule collider going around the character over here, and you can see that the bounds of this collider don't really match this right. So I can edit collider and adjust the shape of the capsule collider, using these gizmos tools, basically clicking on one end and bringing it down, and then that will give us a better shape for the capsule collider to match thesis right of the character. And now, if we click in the back arrow and the hierarchy and we go back out to our scene. You can see that the capsule collider in our scene is updated to the same values that it has in the prefab. But we can customize any values we want to as a override, by just typing in a number so I could have position of five set for the position for this player. But we go into the prefab and you see that it's as zero over here. So now this player in the scene has a override. And if you don't apply all of your custom values into the prefab, I guess transform isn't included. You can click on this overrides menu, which will show you all of the changes, and there'd be a little apply option. So if I change the sorting layer just surveying the property, I could apply all of these overrides to the prefab to copy those same settings to all other instances of the prefab. But I'm gonna take the sorting layer and put that back to zero for now, and we'll also take the position and put that back at zero 4. Adding a Game Background: and one of the next things you're probably gonna want to know after adding a player into the scene is how do you add a background? So the camera is going to render game objects to the screen for the player. So what we could dio in a to D game is put a background image behind this player, and then the camera would show that to the player as well. So in order to add a background image, we can right click on our seen hierarchy, and we could go down to two D object and then Sprite to create another game object. You could also drag a background image straight into the hierarchy for pretty much the same effect, and I'll call this background or Sprite. I'll click on this selector here, and I'll find the background image inside of the project. So just click on that there. And so now we have a background image that's rendering as a two D plane in three D space. So what I mean is, when you have a two D project, you can click off two D View and you can actually see that every object inside of the game is still a three D object, even though you're creating a two D game, so everything still exists in three D space, technically speaking. But as long as your camera is looking directly at these two D planes and strikes, they were generally be no functional difference than if you were using a purely to D engine . So one thing that you often run into if you're creating a game in two D like this is that you'll need to render the right objects in front. And a great way to do that is by using the sorting layer settings in order to determine which order things should be rendering. So the background should be in the back, and then anything else should vendor on top of that. So if I click on sorting layer here, there'll be no other layers by default. But I've already gone ahead and created one. So if I click on add sorting layer, it will pause over to this tag and layers menu, and you can see the in sorting layers. You have plus and minus buttons, toe Adam and move layers, and the layer that is on top here is actually the one that's gonna be the most background of the layers, and the ones that are at the bottom are going to render on top of everything else. So we would want our player or default layer to be below the background layer in this case , Now what we need to do for any object that should be a background layer strike. We just changed the sorting layer to background and then that'll allow anything that's like a player or a tile map to vendor on top of that. Okay, cool. So there's a couple more components inside of the play of prefab you can see here a rigid body to D. Now it's set to dynamic by default, which allows the unity physics engine to interact with this character, which is really cool. You don't have to program your own physics engine or anything you can see in constraints. I've set freeze rotations eep, and the reason I've done this is that we always want the character to be upright. In this case, generally, the case with more classic kind of Pixar games. You wouldn't really want them to rotate on their side, though. If you do want that, you can just kind of not have freeze rotation checked here, and it will be up to rotate about. So note that there is a gravity scale here, so the physics engine will automatically apply gravity to your game objects that are set to dynamic mode. So if I go to the game view and a hit play, the character is just gonna fall off the screen and about one second. So in order to not have your character immediately fall off the screen, you have to have some kind of ground for it to stand on. 5. Tilemap Level Building: so a really great way of creating ground or the base level for a seem to take place and is to use tile mapping in order to take one tile and repeated a bunch of times in order to create easy ground. And you can kind of customize the shape by using different tiles. So in order to add in tile mapping to unity, you need to go up to the window package manager and you're going to need to type in and find these two packages if they're not installed already. So two D strike and two D tile map editor. You need both these so you can just search up here in the toolbar type season and install them. Um, these are official unity. Package is they're just not included in every project by default. It wouldn't really make sense if you took every package and loaded it into every game you make. Because obviously not every game is gonna need every functionality. But this can also be a cool way to find other tools to check out such a sinner machine, which is a really handy camera tool. But once you have those installed, you should be able to go up to window and two D and open up a tile palette window. So over here, next to Inspector, you can see that I have the tile palette window, and in here we can create a style palette which we attach images to in order to create tiles. And the tile palate is basically the manager for all of those tiles that we could draw onto a tiled grid inside of our seen in order to create the level and store that within the scene, what I'll do is over in the Assets folder. I'm going to right click and create a new folder and I will call this pallets, and this will be where I store my tile palettes. So I want to create a new palette here, and I'm just gonna call it to toil one. Leave it as a rectangle and automatic. In this case, I know that you can also do hexagon and isometric tiles if you're more into that. But rectangle is probably standard for this kind of perspective. So let's go ahead and hit create here, and I'm going to store this tile palette file in the pallets voter and you can see that it's been stored in there now. So in order for the top al it actually be useful. We need to attach some images to it. So inside of the environment Porter, there's a tile set image and a tile set free sliced image. Now, generally, if you grab a tile set from somewhere around nine, it's not gonna be pre sliced. You'll have to do it yourself. So I'm gonna click on the tile set that's currently on sliced and go into the strike editor . But first, I'm gonna change Sprite mode from single to multiple. You do have to have the two D strike package from the package manager in order to change it to this mode. So let's go into the spray editor. I had apply to reply that multiple mode setting, and from here we want to slice up the image based on the number of pixels per tile. So I know that the number for all of these tiles is 16. You can see a kind of tree done it, but if you want to re slice it, you go up to the sliced button up here at the top. Change it from automatic, too, grid by cell size and change it to the pixel size for each of your tiles. So I'm gonna slice it here. And so these single tiles, such as the ground, are now their own individual selection. Some of the objects in this tile set actually take up multiple tiles. You can see here for this little log that comes out of a tree that actually placed that in your scene. You would need to place three tile spaces, so this should be fine as long as you're tile set was designed with a specific unit in mind , such as 16 pixels by 16 pixels at 32 pixels by 32 pixels. So let's click on the X here and apply to change this into the multiple slicing mode and go over to the calliope palette. So, in order to properly bring in a tile set to a tile palette, what you should do is instead of dragging individual images such as holding shift down like this and selecting off the images individually, you dragged the image itself the original image into your title map editor, and then this will keep the positioning of all of the tiles that you had in the tile set image. So if I dragged us in here and we go to the Palace folder, we need to store tiles in here. So I'm going to say to toil one, and I could add tiles to that. But I think it's kind of self explanatory, and I select we generate the 128 tiles into that. But you can see that the positioning of all of the tiles is exactly like it is in the image , and that will make your life a lot easier. So now we can draw onto a tiled grid with any of these tiles and basically create whatever kind of game level we want, which is super cool. So in order to start doing that, we need a tile, create game objects. So I'm going to right click in my seen Go down to two D object to belief. And when you add a time at game object to the greatest created immediately under that title map, child game objects so you could see the hierarchy. Parents are at the talk Children at the bottom. Same thing when you get in to see sharp scripting and you can see that on these game objects. There are different components attached to the grid has only agreed component and the time it has a time out in the tile map render for the time at vendor. It might be a good idea to create a sorting layer for it, so I can add in a sorting layer here, well hit. Plus, I'll call this tile map and we'll put this between the background image and the default player slash enemy layer. So now we can take the tile map and change its sorting layer to time it, so that will just keep the rendering more organized. As things get more complex, know you'll notice that the tile grid is really, really big in its tile sizing. So what we want to do is go over to the grid, and because we haven't changed the pixels per unit of any image within our game, that means that it's defaulting to ah, 100 pixels per unit. But we know from cutting up the tile map that each of the tiles has 16 pixels in its data. So we need to shrink the south size of the great accordingly, so 100 pixels down to 16 pixels is gonna be a 0.16 for X and Y. And just like that, our great is now set up properly, and the tiles that we try to draw onto the screen are going to be in the right position in the right sizing. So now we just go into the tile palette and we can start drawing on the grid. So I take this style a click on the grit, and we add that into our game. We can add a whole bunch of the middle top ground, and we can add one for the right edge. We can go down here. I note the organization by dragging in the tile map properly allows us to keep things organized, and we can just keep drawing this little island for a platform or type game and feel in the middle section with a bunch of black spaces. And then when you have a lot of space, is you don't have to draw them all individually. You can use the paint bucket tool, and you can just feel in a empty region with the same title just like that. So now we have a ground But the thing is, it's not actually going to stop the player from falling to their death yet because there's no because there's no collider on the tile map, So if I play here, you'll see that it will follow straight through the ground. So in order to change that, and you may not want a time map glider on every tile because some tiles you actually want to be able to fall through, such as Water and Mario Game. But we can add the collision to attire meant by clicking on Add component Here and tile map collider to D. So if you want some tiles to be collide, herbal on some to not be just straight to tile maps on the grid, and that should be really easy solution. So now, with this style map collider and in the Player, the rigid body and the Capsule collider attached, a song is this is a two D, then these should be able to interact with each other properly in the physics sentience. So when the player falls down, he stops right at the top off the ground, which is what we want 6. Building User Interface (UI) Functionality: Okay, so next let's talk a little bit about canvas. You. I elements when you want something to draw on top of the screen but be independent from the game itself, as in a button that isn't really something. Your characters would collide with their care about in any way. Then you put that on the top layer called the canvas so the canvass will contain buttons, text elements for displaying tax such as you characters, HP or that kind of thing. And we can add a canvas to our seen by right clicking and going Down to you I and canvas. So with this can this game object? It will vendor everything nested under it. All of the child game objects to the camera, regardless of where the camera's looking at. So the canvas is kind of separate from the main game world and its overlay that was set on top of your game in the prefect's folder. I have created something called a launch button, so I'm gonna drag this prefab onto the canvas. This is basically just a button canvas element. If you want to create something like a button by default, you can right click on the canvas, do you, I? And adding a button a image if you want, like background for your main menu text elements, slider, scroll bars. All those kind of things basically works the same way that similar elements would be on a website so we can see the in the scene that canvas has the button over here in the bottom right hand corner. Um, so the way it's determining that is with the vector transform So the rector transform is going to allow you to set anchor points to the canvas. You could kind of think of the canvas as the screen itself because the canvases, size and position is going to basically perfectly match the screen itself. So if I go in the game view and I changed the ah resolution here to stand alone, you can see that the canvas adjust the position off off the canvas, your elements based on the screen size. But if you go back to seeing the, you can see that the canvases, shape and size is dependent on what resolution you have set for the game. So if we want to take this button and move it to the top left hand corner. I can click on the anchor presets button over here, and I'm gonna do control shift to set the pivot. The position and the anchor point did the top left hand corner, which puts it squarely up there in the top left hand corner, regardless of what size the screen is. So if I change it back to 16 by nine ratio, then it's still gonna be up there in the top left, and we can adjust its offset position by adding an A X value and why value. And I think we want negative 40 here in order to move it down for the Y value. So it's not too hard to position canvas your eye elements. And if you want your you elements to scale with the size of your screens, so they will appear much bigger on a big monitor with high resolution, then you can take your canvas you element in the canvas scaler, and you can change thes scale mode from constant pixel size, which will mean it's always the same size, regardless of the resolution to scale with screen size, and you would wanna have a reference resolution set so you know the default size for everything on the screen based on that reference resolution so I could do 1920 by 1080 which is a pretty common resolution. And then I just need to take each of my canvas you elements and change their size so that it fits the reference resolution. So, for instance, it can go into the text game object and increase the font size from something like 14 to 46 . You can change your front here, too, by the way, and now you can see that the text is not fitting the buttons. So we need to increase the size of the but himself. So did that by increasing the width and height of the button. So something like 400 by 60 year 400 by 80 should be pretty good. And with those settings, you should get a pretty consistently positioned and sized you, I element. So now you might be wondering what the Launch Player button does, so you can see under the button script, which is attached to every button you I element by default. It has a on click event and this on click event. You can add functions to it, which basically references script and then reference what method you want to call from that script and executes it when the button is clicked. So the script in referencing is the player movers script, which I've attached to this button as well, and we can open that up by clicking on the settings icon over here and then doing edit script. So this player movers script has a launch factor, which is basically whatever settings you want to add to it, the magnitude in the direction off this launch, so to speak. And then when the launch All players method is called, it's gonna find objects of player type player controller, basically all game objects that have a player control a component, and it's gonna get reference to the player control, a component within those game objects within the scene. This isn't necessarily the best practice way of referencing the other game objects within your scene, but it will work for simple self like this. So we have a list of all the player controllers and for each of those player controller scripts, we want to find a rigid body to d component and add the force to it the launch factor up here in pretty much the same way that we had for player controller. We can see that this had time dot doubt to time as part of the calculations for this input . But rather when we do it this time, this is more of a one time force, so we don't bother with time down to time at all. But rather we just add the force all at once. So if we hit play on our scene, we can actually use this button to launch the players and our again. So I'm gonna hit launch player and you can see it makes it how, If we had other game objects in the scene that had a player controller component and a rigid body two D components, then we could be ableto launch all of them simultaneously with this. But so that's a pretty cool way of having things interact with each other. And now no. One thing When I was making this player controller script, if I opened that up, I had require component type of Richard body to D added up here at the top. So what this will do is that whenever you add a player controller script to any game object , and I'll show you just by opening up this background randomly on DWI. Add in a player controller that it will automatically add a rigid body to D along with it. Because this script depends on having a rigid body to D, and you can see we can't even remove it until we remove the player controller. So that is one way you can tell unity about that so that you don't accidentally forget components that are required for something like a player to work. 7. Simple Platformer Player Movement: Now, this player already has a really simple player controllers script attached to it that allows it to move left and right so you can see here hit left and right on the keyboard, and it accelerates as time goes on, as long as I hold those two keys down. So if you go off the time map, they can still, of course, fall down indefinitely. So let's open up the player controller script and take a look at it. It's not really too complicated here. You can see that the player controller script inherits from model behavior. So as long as you're working with game objects instead of the entity system, which I don't think is 100% out yet anyway, then any component you attached to a game object is going to be a mono behavior script. So whenever you create a script, it's gonna have this by default, and then you can take these scripts and attach them to a game object, such as a player. After that, you can see it declared a couple of variables. Note that any public variable is actually edible inside of the inspector for a unity game objects. So if we go back over to unity. You can see here for this player script that I've attached to the player, that the move speed is creditable. So although I might declare a certain move speed in the script, I can override that by changing the value and the inspector for a player. So it's really handy. You don't always have to go back into the script if you just want to change some of the values as long as you make those variables public. We also have reference to a rigid body, and this Richard body to D is a component. So whenever you want to get a component inside of unity to D you call, get component rigid body to D or whatever the type of the component you trying to get is. And then this will look at the game, object and see if the component is attached in return, a reference to that. So I store that reference in this rigid body to D variable, and with that, I am able to reference the rigid body to D variable and elder methods or functions that are attached to a class for unity to D script. So in order to get the input from the keyboard or whichever controller happens to be attached to a game. We canoe input dot get access raw. Then we get the horizontal import from the control. In this case is the keyboard. So you'd be looking at left right arrows and we assign that to afloat. Variable afloat is just a number that supports decimal points. If the import value is not zero indicating we're getting some kind of input and if we are, then we move on to creating a move factor. So vectors are gonna be super important in game physics. They have a magnitude and a direction. So the idea is that whatever the X and y values of the vector gonna be is how much you wanted to move in either acts, horizontal movement or why vertical movement in this case. So Victor isn't always gonna be a movement value, but it's always going to be a direction and magnitude or the value. So the idea here is that if the import is negative, as in I press left on the keyboard than the imports gonna be negative. And if I first right on the keyboard that imports gonna be positive. We multiply that by a move. Speed value. How fast we wanted to start accelerating. We could actually rename this variable to something like acceleration, and that might make more sense. So why don't we do that in visual studio? We can me name both of these at the same time by right clicking and doing rename. So I will call this acceleration and hit apply and we take those values and we multiply it by time dot doubt to time. So what don't two time is is the time since the last frame update in a fixed update method . Theoretically, every frame should be an exact amount of time since the last one has occurred. But but in a void update method, uh, the time dot delta time won't be consistent. So by multiplying our values by time, down to time, whenever updates, we we modify the amount of force that it's applying to the object based on how much time since the last frame. So this will help the movement look a lot more consistent regardless of how often the update method is running and by the way, for mono behavior, these will automatically run by default awake will happen whenever a game object is added to the scene initially, whether it's enabled or disabled. If you're curious, you could look into unity, model behavior, life cycles, but not really to relevant for right now. So anyway, we take our movement vector, and then we add it to the rigid body. We added to the ridge body because the rigid body works with the unity physics engine in order to determine how a character should move. So, for instance, if you try to move up against a wall, even if you add the force to the rigid body, it's still not gonna move because there's a wall there. Now, if you wanted to have jumping or that kind of thing, then your vector would need a value for the why parameter over here. So why is vertical? And this one on the left is X value for horizontal, but in this case, we're only trying to add force to the object to make it move horizontally. So, in a nutshell, that's how our characters able to move you. I'm gonna change the X elevation value back to 300 here because it seems like it needs to be higher for the character to move at all with the physics engine there. So at at 300 we get a reasonable move. Speed, as you can see here. 8. Building User Interface (UI) Functionality: Okay, so next let's talk a little bit about canvas. You. I elements when you want something to draw on top of the screen but be independent from the game itself, as in a button that isn't really something. Your characters would collide with their care about in any way. Then you put that on the top layer called the canvas so the canvass will contain buttons, text elements for displaying tax such as you characters, HP or that kind of thing. And we can add a canvas to our seen by right clicking and going Down to you I and canvas. So with this can this game object? It will vendor everything nested under it. All of the child game objects to the camera, regardless of where the camera's looking at. So the canvas is kind of separate from the main game world and its overlay that was set on top of your game in the prefect's folder. I have created something called a launch button, so I'm gonna drag this prefab onto the canvas. This is basically just a button canvas element. If you want to create something like a button by default, you can right click on the canvas, do you, I? And adding a button a image if you want, like background for your main menu text elements, slider, scroll bars. All those kind of things basically works the same way that similar elements would be on a website so we can see the in the scene that canvas has the button over here in the bottom right hand corner. Um, so the way it's determining that is with the vector transform So the rector transform is going to allow you to set anchor points to the canvas. You could kind of think of the canvas as the screen itself because the canvases, size and position is going to basically perfectly match the screen itself. So if I go in the game view and I changed the ah resolution here to stand alone, you can see that the canvas adjust the position off off the canvas, your elements based on the screen size. But if you go back to seeing the, you can see that the canvases, shape and size is dependent on what resolution you have set for the game. So if we want to take this button and move it to the top left hand corner. I can click on the anchor presets button over here, and I'm gonna do control shift to set the pivot. The position and the anchor point did the top left hand corner, which puts it squarely up there in the top left hand corner, regardless of what size the screen is. So if I change it back to 16 by nine ratio, then it's still gonna be up there in the top left, and we can adjust its offset position by adding an A X value and why value. And I think we want negative 40 here in order to move it down for the Y value. So it's not too hard to position canvas your eye elements. And if you want your you elements to scale with the size of your screens, so they will appear much bigger on a big monitor with high resolution, then you can take your canvas you element in the canvas scaler, and you can change thes scale mode from constant pixel size, which will mean it's always the same size, regardless of the resolution to scale with screen size, and you would wanna have a reference resolution set so you know the default size for everything on the screen based on that reference resolution so I could do 1920 by 1080 which is a pretty common resolution. And then I just need to take each of my canvas you elements and change their size so that it fits the reference resolution. So, for instance, it can go into the text game object and increase the font size from something like 14 to 46 . You can change your front here, too, by the way, and now you can see that the text is not fitting the buttons. So we need to increase the size of the but himself. So did that by increasing the width and height of the button. So something like 400 by 60 year 400 by 80 should be pretty good. And with those settings, you should get a pretty consistently positioned and sized you, I element. So now you might be wondering what the Launch Player button does, so you can see under the button script, which is attached to every button you I element by default. It has a on click event and this on click event. You can add functions to it, which basically references script and then reference what method you want to call from that script and executes it when the button is clicked. So the script in referencing is the player movers script, which I've attached to this button as well, and we can open that up by clicking on the settings icon over here and then doing edit script. So this player movers script has a launch factor, which is basically whatever settings you want to add to it, the magnitude in the direction off this launch, so to speak. And then when the launch All players method is called, it's gonna find objects of player type player controller, basically all game objects that have a player control a component, and it's gonna get reference to the player control, a component within those game objects within the scene. This isn't necessarily the best practice way of referencing the other game objects within your scene, but it will work for simple self like this. So we have a list of all the player controllers and for each of those player controller scripts, we want to find a rigid body to d component and add the force to it the launch factor up here in pretty much the same way that we had for player controller. We can see that this had time dot doubt to time as part of the calculations for this input . But rather when we do it this time, this is more of a one time force, so we don't bother with time down to time at all. But rather we just add the force all at once. So if we hit play on our scene, we can actually use this button to launch the players and our again. So I'm gonna hit launch player and you can see it makes it how, If we had other game objects in the scene that had a player controller component and a rigid body two D components, then we could be ableto launch all of them simultaneously with this. But so that's a pretty cool way of having things interact with each other. And now no. One thing When I was making this player controller script, if I opened that up, I had require component type of Richard body to D added up here at the top. So what this will do is that whenever you add a player controller script to any game object , and I'll show you just by opening up this background randomly on DWI. Add in a player controller that it will automatically add a rigid body to D along with it. Because this script depends on having a rigid body to D, and you can see we can't even remove it until we remove the player controller. So that is one way you can tell unity about that so that you don't accidentally forget components that are required for something like a player to work. 9. 2D Sprite Animation Basics: Okay, so one more thing that we can touch on is the basics of doing animation. So in the sunny land package, we have all these wonderful sprites like climb animation, crouching animation, um, jumping, running and that kind of thing. So in orderto have your character animated, it needs to a have a animator component attached to it. And that animated component is gonna have an animated controller. And the animator controller will be responsible for going between different animations, depending on whatever state your characters in like if it's actually moving or it's just idling in place. So if go to the player game, object and let's actually do it at the free fab levels, so open the prefab asset so that this applies to all players. I can add a component for the animator by typing an animator for the ad component, and this is gonna take a new runtime animated controller so I could create a folder in our assets to contain those animator controllers might call this characters or something like that and organized all related character assets into subdirectories. But I'm gonna need animated controller for the player, so I'm gonna I'm gonna right click, do create and then dio animated controller. So just call this player underscore and made a controller or animated control or underscore player whatever you want. So we need to attach this animated controller to the and made a component. And inside of this animator controller we linked together, The animations in orderto have the images change out properly as the characters moving around doing things. So let's start by adding in the idling animation. If we go up to window and animation, you can open up animation. Here are alternatively control six on the keyboard So there we have a animator controller. We have no animation clips so we can click on this Create button to make our first. I'm gonna store this dot animation file in the Characters folder, so we will call this idol dot and, um, for animation. And now we need to add into this animation timeline the frames that the character should be playing through while the character is idling. So inside of the sunny land package artwork. Streit's player and idle. We have four frames, so we're going to need the psycho between these frames in some way. The easiest way to add them to the timeline is just a select them all. So I'm gonna left click on the 1st 1 hold shift and then left click on the final one and drag these into the timeline, and you can see that there's now four frames that have been created for these different images. You can expand it to see those little strike frames in the timeline, and now we basically have a simple animation. If I go over to this scene view to see the prefab object over here, um, and then we go to frame one and hit play. We should be able to see the character animating now. Right now, it's really, really fast, and we need to slow that animation way down so the dope sheet is animating with 60 frames by default for every second. So if I have the strike change every 10 frames or so, that should make the characters animation a lot slower here. So I just spread these out one at 31 at 20 and one at 10 and if I play here, you can see the idol. Animation has slowed down dramatically, and that's probably more like how it was supposed to look so we can stop that animation there. Every open up the animated window, which you could do by going to window animation. An animator concede the animation flow for the animator controller, which is attached to the character so the entry point is going into idle. But we probably want another motion in the characters animation, such as one for Money. So to create a new animation, we can click on the drop down and you create new clip in the animation window and we will do Runda and um so for this animation, we need to do basically the same thing, grabbing thief frames for the run and I'll grab all six of these, put them into this animation clip and spread them out as we think they should be. So because the voting probably needs to look a little faster, I'm going to make it every five frames instead of every 10 frames. So let's ah, get that moved over there, and we're also going to want to copy the first frame over to somewhere around frame 29 so that the animation gives the last stripe frame a few run time frames to actually be there so every it play, This should start to look right, and we probably want to also do that same thing for the Idol animation. So for the idol, I'm going to go to frame 39 here and copy and paste that in for the first frame so that when it gets to frame 40 it's just gonna look back to fame. Zero. And if we hit play here, we can see the character animating correctly as well with the run animation. So now we just need to tell unity how to go between Idol and Run. And we can do that by setting a parameter in the animator control so you can see that there is a empty list of parameters here. So we want to add in. In this case, it's gonna be a float, and we'll call it in for X. And so this input X value is going to determine whether it is in a idling state or run state. So in order to go from Idol Trevan, we need to right click and make a transition to run in order to set a path where it can change from one animation state to another and for the condition along the arrow. For this transition, we want to set a condition, which is that in for X is greater than, Ah, something like 0.1. We can test this and make sure that it's working nicely. And for run toe idol, I'll make a transition so it will return from Monday idle. If the condition of Input X is less than and we'll say 0.1 wasa turn off exit time for both of these transitions, so that if the condition is met, it will immediately change toothy other animation and turning that off for the Idol Turbo Nas Well, and now we just need to have a script. Tell the animator controller what the value in port exes. So the best way to do that. So the way to do that is gonna be to go over to player, find the player controller, and what do you edit script here? And we're going to need to reference the animator components so that we can set the parameters on that component. So I'm gonna need a private animator here and I'll just call this animator and just like with the rigid body would you animator equals? Get component Animator, which will find the and major component on the game object if it exists and what we'll do is check. If there is an animator, then we will set the value of that parameter. So animator dot set float to set afloat parameter is going to be the name of that parameter . So in for X, and then the value is in Port X and then the values in Port X without the quotations. And then that should be all we need to do in order to have the player controller update the animator. So let's make sure that the names are consistent, the parameter there, same naming code as it is in the animator window. So let's go back to the game and, ah, find our player here, go to game mode and hit play so you can see that the Idol animation is there by default, and it's staying there because there is no input X. But as soon as I start pressing left and right on the keyboard, that actually causes the character to enemy right now, it's only in the right direction because negative Input X is actually still less than 0.1, technically speaking. So what we can do to fix that is to use a math function to make it absolute value. So audio math f dot a B s for absolute value to take the absolute value, which means you remove the positive negative sign and you just take the magnitude of the value. And that's what we're actually set as the in four X float. So now the character should be able to move left and might. However, the character is always still facing to the right. So one more thing we can do is if the value of imports X is negative, we can tell the sprite render to flip acts so that will make the character face left. There will only bother to add this functionality if the inputs actually changing. So only add this functionality for the condition that the import actually exists. Otherwise, there would really be no need. So I'll just add below here after the rigid body at forced to set the sprite render to flip based on whatever direction the import excess. So this will be another component that we need to add in appear at the top So a Sprite men Durer and I'll call it s Surrender said that this no name confliction and we will get thestreet mender component so that we can set the flip value So the flip X value is a boolean. So we basically want to say what? That this is positive or not So weaken type this logic in But like this import X is a greater than zero and we evaluate that to either true or false. So if it is true ah, then it's going to flip X And if not, then it is going to set the flip X to not be flipped. So we actually want to reverse this. I'm going to do false and true because if the input is greater than zero, we want it to be facing to the right, which means we don't want it to flip. But if the input X is less than zero, we wanted to evaluate to true so that it will flip and face the left. But now we can go back into the game hit play one more time, and the character should be able to run left and right, depending on which direction we are pressing on our keyboard so you can see these fright render is updating their in real time, and that is super super cool. 10. Wrap Up: so already we have a lot of the basic set up for how we could create something like a simple two D platformer. You just need to add in game over victory logic how it interacts with other things such as collecting coins, maybe adding a function for out of jump, how our enemies should move around the stage and interact with the player and all that are the kind of thing. Hopefully, this video is giving you guys a good quick start into unity game development. So I've been Chris, Thanks for watching, and I'll see you guys in my future video content.