Learn about movement, layers and collision with the Godot engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to be talking about movement layers and collision getting your character up and running around quickly having them to deal with collisions within the environment and how to handle obstacles that are more complicated than the standard just putting an object and running into it by the end of this video you should be able to handle and create more complex scenes with varying collision types to get your next game underway first things first you need to go to the github url on the screen go ahead and pull that down and then import the project all right the first thing you should do is input the project after you've downloaded it from the github link once you have it downloaded and extracted click on the import link find where you have it downloaded and find the project dot co dot click open and then click import and edit once that is completed you should be presented with this screen now you'll notice that there's already a character and a simple tiled green uh grass with some water on the end you'll also notice a big exclamation point with a triangle and if you click on it you get this error this is mainly just saying that there's no collision associated with this character uh it's already linked up to f5 but if you need to do that you just go to project project settings and then you click run main c open and make sure the tutorial scene is set otherwise you could just press f6 but when you run this and then you try to move with any characters wisd after noticing that you can't move menu keys we should solve that to do that let's go ahead and exit out of here and then what we need to do is set up the inputs to even make these actions occur so go to your project project settings and go to your input map and notice that there is no if you scroll down there's no no item here telling you which way to go uh we want the character to go up down left to right we can type in up click add and then click the little plus button here as well uh do a key you can use the up arrow additionally you can even support the wall stick keys at the same time using the same event so hitting w uh the plus key and then key w now both up and w's are support the up action and now let's go ahead and do that for all the other actions okay remember to save the scene anytime you see a little star apparent parentheses that means it's not saved and if godot ever crashes or your power goes out all the work you've been doing is unsaved okay from here what we need to do is work on the character himself to get him moving in the actors folder go to player and you'll see a player.tscn after opening the player scene you'll still see that same warning but they're not being collision shaped again we'll get to that the first thing we need to do is focus on movement so in the top corner here this green plus which allows us to create a new script uh layers of kinematic body 2d which is okay and we're going to go with a player.gd which just takes the name from the root node and since it is that click create by default you will get all this extra commented code to try to help you get started it's nice and all uh but we don't need it we need to get rid of all of it we don't need the ready function okay so now what we want to do is that we want to be able to move the character around the screen to do that we have to give a direction and have the player move in that direction over time now we can do it in this function provided which as it says here this is called after every frame the delta is the time between the last frame start with this speed say we want the character to go speed say 200 and you can play with the speed yourself and decide what feels good for your character there's no hard-pressed rule what the right amount of speed is we will also need the concept of velocity to move the character you then have to get the input that was associated with those actions we set up earlier so to do this is actually fairly simple we say if the input okay the key we just tapped or the key and then we say is action repressed and now you'll see that all those items we've created plus the ones that were already there that are available and of course up is the year two is just hidden so we drop up down and then we can just say velocity dot one plus equals one now when we think about let's get us back here to our scene here to tutorial scene when we think about movement the corner here when you look when you have a new map you'll have these like four quarters i generally like to have it in this corner here because this represents zero zero and so if anything were to go see we had to move this here up to here to move and we decided to move it this way anything beyond this item here is going negative into the x-axis so this is now negative which if we open up the transform you can see that it's now negative 40. when it's here it's zero zero so this virgin point here represents zero zero so to go down we need to add so down this way increases the y whereas going up decreases it so that is why uh we make the y a plus one and not a minus so they committed the top one or zero zero so if we go back to the player click on script we say velocity plus equals one oh and remember to put the colon at the end of your statement okay and then from here we don't need this pass just passing anymore we say velocity there and then we take now i'm going to tell you what why we do not just modify the transform so i just showed you the transform on that uh character on the character on the scene i just showed you the transform on that scene as we were moving it around and your first impulse might be take this value and change the normal position [Music] to be the y we just increased so let me show you why you don't want to do that now i'm going to press down on the keyboard notice that the character does stop and the transform wasn't very clean what you want to do is actually use a method called move and glide and then you can then just pass it the velocity but we're not we're currently still not taking into account the speed or the fact that this action press and velocity just keeps increasing so what we need to do is take the velocity set it to us normalized multiply it by the speed after you do this run it can you find out holy moly that didn't work why did that work well there's this handy thing in here called delta if you've ever used anything with unity or unreal engine uh you need to multiply your velocity as well by time delta time but this is just time since last one since the last frame so that needs to be part of the calculation so you're not speeding to the bottom like we just did so we can just pull like that as well and now when we run it again we notice he doesn't zoom off like before but he still doesn't stop to fix that we can actually change the way this method works instead of using it actually pressed and then a constant one for the velocity we can actually change this to get action strength now what this does is if you hold the button down it goes from zero to one so as you hold it down over time it sets that value and you can actually change this from an if condition and actually just get rid of the plus one set it to equals if it's now the velocity will be between zero and one if you have it down it's one if you let go it goes to zero so then if we run it now i'll put the down button and let go now we can quickly implement that for the other positions and directions now to go up and down and left to right you can actually subtract between them so we know that positive number down so this should be positive minus input action strength up because going up is in the negative direction so if this is so we don't try to stack them and have just multiple lines because you could override them and it won't behave the same way but this is the same way of saying if i'm holding the up button this is saying it should go zero minus one in the direction which we know negative one on the y-axis actually subtracts it and makes it rise up so we can try this [Music] down and stops go up and he goes up and we can do the same thing for the other direction right is the problem left is the negative if you ever forget you could just go back to your scene and just play and look at the transform and see that this is zero and you can move it and see that it's negative positive and again go down and up left and right i notice it stops because that actually straight okay so now i want to bring into your attention there's another method out there [Music] called move and slide these are nearly identical [Music] except [Music] in this case you want to use velocity but the movement slide already takes into account the delta and this might seem like the same solution we just used and you might ask yourself what's the difference between move and slide and move and collide and the answer is move-in slide slides on a collisionable object where move and collide has you hit that collision object and then you can write the code and decide what happens for instance you might hit a uh a spike attack or something like a spike wall and when he collides with movement collide you might want him to bounce backwards and not just slide against the wall however if you want to use a just a flat dungeon wall you might want him to go up to that wall and then continue to slide in a direction as if he's walking and hugging that wall you just have to be aware that one takes into account delta and one does not because they want to make sure you have full control over what you want to do with that another good thing to do is to actually move your input logic outside of your process so let's create another function and just call it get in [Music] you can just move this object into it remember not to forget after every picture physics processing item not an input item so we can leave this here which then leads me to my next topic there is a process [Music] and there is a physics process it's one of those cases where they seem like very similar functions that do the same thing which one should you use i am wrong to use the physics process for a very specific reason so we can move this comment back so we all know this is actually associated with associated with the process now we read this this is called every frame and the delta is the time between the previous frame now what this means is if anyone who's playing your game has a different frame rate than someone else then you cannot really rely a standard timing so one frame of time might be different on one machine to another the way to get around that is actually to use the physics process and that's because the physics process is actually called at a constant frame rate of 60 times per second by default regardless of the person's frame rate they can continue to have the same exact experience as everyone else and everything is consistently the same speed so all we need to do is to the physics process [Music] and get rid of this one we don't need it and run it don't forget to call your git inputs otherwise you will not have only anything happen when you click in and that's part of this too uh i also am not cutting this because we all make mistakes and we all have issues and just because i've built this before and i'm showing this now doesn't mean that you're gonna have have not an issue when you develop there's going to be something you forget i forget the colons all the time i moved a function over and i forgot the method to add and now you see that it works now you probably don't see any difference between the two but you would see the difference between someone has a frame rate of like 30 frames a second versus someone who has like 260 frames per second so that would be an obvious difference [Music] so that's the basics of of movement on a kinematic body now there are other types of bodies out there [Music] there is area to be which really just provides detection and like influence you want to use area 2d objects or nodes for figuring out if something's at range you want to create an action you like a spike trap or you have an event that when two things cross each other that you're going to detect that it occurred but there will be no physics there will be no action applied from like a physics point of view you mainly want to use this for determining that an event has occurred next up is a static body 2d now as the name implies static means non-moving what this means is it provides collision detection but physics does not move this object you can apply a force or anything to it and it will not move but it will still detect if it's been collided with this is perfect for environmental objects walls and things that you do not to have any effect on if physics is applied to them the third type is rigid body 2d now this is your good old physics based body it takes on gravity impulse and unlike the kinematic where we have physically moving and telling it movement you actually apply a force to it and think of it like the asteroids game where you move them around and then they get pushed and they slow down over time based on physics simulations okay now let's talk about layers and masks now look at this character here you'll see if we this physics body 2d there's a collision tab and in here is the layers and masks now i've already gone ahead and filled this out but you can change your settings at any time project settings general and in 2d physics layer names you can change them to whatever you want them to be now what is a layer layer describes what this is so since this is our player we would then describe this layer this object as a player the mask underneath it here and every physics bot 2d has legs whether or not you choose to enable them is up to you based on your needs of your project the mask is what layers they interact with so as a player you would want it to interact with enemies objects and the world you wouldn't want your character to get stuck based on your hud and there's no reason to really have it interact with itself unless you had other characters and you had multiple players and you want them to collide into each other so now when we talk about collision i mean finally to this warning it talks about a collision shape 2d what you want to do is click on the player and do control a and look for that provision shape 2d click on create notice it still has but now it's moved down to here now it says basically what you've done is you've created this you actually haven't specified the shape so if you come over to the inspector come and click on the capsule and then now you're provided with this the warnings have gone away but this is actually too big for your character what you want to do is take your mouse and resize this click on that click on the to move it around or w and then just really kind of get what you want the character space to be i like to focus on the feet since it's a character it's okay to have it pop out a little bit we're not actually worrying about uh putting it on the head or anything unless we wanted him to be jumped on then we could talk about detection and do area 2ds but this is just a collision shape and once that's done uh one major thing you need to be aware of is if you open the transform you look at the scale you always want the scale to be one by one if this ever changes you could have as good as the documentation but unknown side effects things could happen in your game that may not work right now play not play correctly and you don't know why that would happen so we'll save that and then we'll press f5 to run it and we'll notice that nothing has changed and that is because we don't have anything to interact with so let's scroll down and find our tree stop now this is actually really simple we just got done this with our player so we go to layer okay it's not a player it's an object and what does it interact with well it interacts with players probably interact with enemies potentially the world but that's up to you such as a coin you might have it uh drop from an enemy and bounce against a wall so now let's add another collision shape 2d let's do another capsule and then just kind of put it into a good shape here and again i've already provided the asset and set it up uh sprite here so no worries about that if that is not working leave a comment and let me know if you have any issues speaking of it so there's good save it back to your tutorial scene take the treestump.tscn just drop it on save it press f5 and then notice that you now slide around it and that is because of the layers uh move this slide so if you change this to movement alignment apply delta again and run you'll notice that he doesn't slide around the object it just stops because it's waiting for you to determine what needs to happen [Music] now the preventing the movement and all that stopping that's occurring is happening automatically for you in these two functions so you do not have to worry about when the collision detection occurs how you do the math you don't have to do that at all it's handled for you so we can stick with this or you can stay with the line it is up to you how you want to do this this is your game in the end from here we then need to think about more complicated uh collisions so we go back to the tutorial scene look at here we want to create a more interesting scene first we really want to stop the character from going off the screen in general to water so click on the level uh also you probably want to keep these to be the same level you don't really want to lower them and embed it personally because then you'll want to do you'll learn about other things about why sorting and properly having your scene laid out for collisions and character perspectives so but for now just leave them at the root level so we have all the tiles out here but you have no collision so when you put in the water as we saw i'll show you again you just go right in the water which is not a behavior we want you might notice that the we can't we can run off over there but the camera doesn't follow you that's all set up for you based on camera limits so on the player notice there's a camera and i have this limit and that's where you can change that if you want but that's already set up for you back on the tutorial scene here if we go and look at this level go to the tile set and click edit you'll see the tile map you can click on it and you can bring this up increase the zoom here and you can just select all of any of these to kind of get some information now you'll probably be on region by default but you can click on collision and then here's like your collision shapes so let's say we went to this water scene you'd want to click on this water scene use the square double click this gives you the collision baths for that time so now save it go back to the level and make sure your world is set as your layer you do not need it masks because the world itself doesn't have any scripting to interact with anything so it doesn't require a mask [Music] and then from here run boom f5 notice now that he stops there and if we come down he stops on the screen here so now he's bound to the screen and cannot go into the water [Music] now it might it's best to go in here and look at all of these tiles and most water ones you can then just do the same thing and set them all as legit but when you get to elements such as these you'll notice that if you do this now this grass area is collide it's collision and now you can't actually get onto that grass what you actually want to do is actually disable the snapping and keeping this this like snap and show grid and then when you double click and add that in here you can use the arrow tool and then you can reduce the size and this will look sometimes a little tricky but you can reduce the size of that and make this just the collidable area now this one keeps the polygon in here so if i didn't have this enabled kind of like goes out of the bounds but once i enable it and once you bring it back if you enable it then if you try to bring it out it does not let you after you've completed this the next type you're going to run into is like this now if you set up a rectangle you're not really gonna get the shape you want cause in some way you're just gonna be blocking what you don't want so that's where this polygon based collision works keeping this one enabled you can just click once click again and test read shape and then double click or just click once you fix this to be exactly how you want it to collide and then you just have to do that for all of your tiles why don't you go ahead and i'm gonna really quickly run and do this [Music] [Music] okay now that's a long tedious process but in the end it's rewarding you can make this work any way you want as difficult you want the next thing we want to do like setting up the scene and you can you can do anything you want from this i just wanted to show an example of carrying across two elements so we go to the tile map here and then let's just say you could expand this out too to kind of see all the tiles you can work with and here's this one so here's a few and then let's say we're working with this might even be a couple of corner pieces here here let's start with this and as you can see you can cross it okay let's go look at our bridge that's under the bridge horizontal dc in our object folder it's mad about collision okay well let's let's start with what you would think you'd want to do okay it's a object and i will create a collision shape 2d we'll make this one a rectangle but you might even say wait a minute we don't want to do a collision here well we kind of do want the the beams right so we don't want them to go across that so we can create two of them you can find that right spot where you want it as big as you want thinking about the collision of your character here and we know it's an object and you could double check to see if your player aligns with objects here c does and then go back to the tutorial scene and just for fun let's just drag him out over here [Music] and let's just see what happens over here notice that your character isn't big enough yet or your collision on your character is too big so you can look at this and say oh well let's shrink this bit and press w or click on this one here you can move it because really it's closer to the feet yeah let's come back here and look at that he goes across the bridge just fine but he can't go up and down the bridge like that locks him alright alright well let's have him go across the water here for the tutorial scene grab your bridge make sure he's in the main here into the meat scene not embedded just pop it over here let's see what happens i'm over here and wait a minute what's happening why can't he cross the bridge he was able to do it earlier well it's actually really simple it's uh you can actually see what's going on if you go to debug you can show visible collision shapes and then turns on all those collisionable objects now notice might be a little hard to see the screen but the water here is technically still colliding so he starts to go in but is still colliding with the water even though it's technically an object above to solve this we actually need to do something a little complicated we go to our bridge horizontal here and we want to detect when the user actually or the player is actually stepping in on the bridge so we want to create an area 2d which is for detection and 2d physics influence okay we can even rename it as uh for instance or even just crossing [Music] and it will need its own collision 2d [Music] to know what shape it's actually uh colliding with so it knows that this is the space in which to detect the the event so again we can shrink this down and just put it in between here and detect when this is occurring and what you can also do is go to the visibility and change modulate to a different color now it's a multiplier so it does it's not an absolute color but you can change it to now say this is a bearing 2d this is the collision so you can say detect crossing then what you want to do is actually create a script by tag clicking the green plus button here put in the object bridge then you have the script come back look at this here click on the area 2d click on the node these are signals signals are ways first to send and bent when something happens now what we want to do is detect when the character player steps onto the bridge and when he steps off the bridge so we can do that with on body entered and exited so if you double click on this node for oddbody entered say we want to use the script associated with it on crosstalk byte entered connect then you can just print out when it's actually occurring come back do the same full exit and you'll notice that it actually you can tell it's connected because it'll have this little connect here and when it's connected and exited we can do the same thing and then [Music] and when you come and look and run it and you check down here you will see it says on body entered and exited as you leave this now lets just know who's on the bridge and into the bridge when he goes on the bridge what we want to do is turn off collision with the water and we exit the bridge we want to re-enable collision but we also want to keep collision with the object which is why we have the object layer and the object ask so that we know that in the player mask so that they're not the same collision layer that gets disabled we still want him not to go off the guardrails so all we need to do is really simple uh we need to look at which layer this is so if we look at the collision we see this it's one part zero waste so zero one two three the world being the third layer we would create a very constant variable world layer set it to three uh this just makes it easier so you're not just keeping a number three around not sure what it is later then here you can just say body dot set collision mask bit world player false and all this is doing is this little when you click this these are bits so this is the same as saying but setting it to this blink where it's nothing where it's not checked and when you if you hit this this bit just here this bit is now uh selected now it's not going to be doing it on the bridge what it's doing is it's saying the body so it's saying slayer's collision with the world is turned off and so if we go to player go down to collision player collision you see that it's enabled so if i turn this off it's the same effect as this so when you turn it off i can now run through the water [Music] so if we turn it back on [Music] we want to do the same thing let's just turn collisions back and now we're running with that but here we're playing with the water but then we go on the bridge right across and we can turn off the visibility [Music] now that it's working and then realize how cool this looks where you can go across the bridge but you can't go across here and now i recommend just playing these scenes [Music] [Music] [Music] so i've also provided logs and rocks and all they require is a collusion with 2d shapes to do what you did for the other objects add a relationship 2d rectangle [Music] and then don't do what i just did which is forget the layer sam this is an object and it collects with player enemies of rock as well is a object and it collides with player into the piece which then allows you to come in scene log into your scene [Music] and then f5 around see that your scene works and voila now i'm curious to know what you built in your scene you can build it there you want also provided a bunch of extra assets experiment let me know what you come up with what kind of level can you create and thank you for hanging out stay safe stay awesome [Music] oh you're still here um [Music] there should be a video in one of these places click them maybe hit subscribe hit a like all right i i'm gonna get back to work
Info
Channel: Hex Blit University
Views: 494
Rating: undefined out of 5
Keywords: godot, game development, tutorial, godot tutorial, godot engine, godot tutorial 2d, godot ganes, godot games, Godot rpg, game engine, godot engine games, godot engine tutorial 2d, godot engine 2d game tutorial, getting into game dev, godot tutorial rpg, godot tutorial gdscript, game development time lapse, game development process, game development tutorial, game development for beginners, godot physics layers, physics layers, godot physics, godot collision tutorial
Id: HaQBVRvKdFg
Channel Id: undefined
Length: 50min 8sec (3008 seconds)
Published: Sun Jan 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.