[GODOT TUTORIAL] Semi-automatic ladders for 2D platformer game

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today i'm going to show you how to add semi-automatic self-setting ladders for your platformer let's say you created a character that can run jump and shoot fireballs but his or her life can become very miserable in very short time when they realize that they can reach high places just because you invented this thing called gravity to help your character you can give him or her ladder for example i will show you how i have done it in my game so let us begin but first of all i will explain you what do i mean by the ladders the ladder is area2d uh with combined with static body 2d on top of it and when player enters that area his gravity is changed to zero he's he's not affected by the gravity he's not falling and he can clamp up and climb down he can he will also be able to jump on the ladder but when he's still pressing the jump key he will fall down until he release it or whatever the ladders will be setting themselves automatically all you all you have to do is to place them on the bottom of the ladder and set the number of tiles you want them to have it will be done in ready function so when your game level is loaded everything will be working as it should so let's make our ladder scene add a new scene add 2d node name it ladder add area 2d and name it ladder area you can name it whatever you want but i'll try this keep consistent so this will be another area add a chart to it collision shape 2d and rename it to ladder area collision set the shape to rectangle shape to d scale it to be the size of your tile in my case it's 16 pixels by 16 pixels next add a chart to a ladder node and this time it will be static body 2d name it ladder top and add a collision shape to it also as a child and set the shape to rectangle to d scale it now you need to move your ladder area up so origin of this ladder area is [Music] half of the size of the tile above the origin of our root node for another scene similar the the ladder top can remain in this position but we need to well i can rename this also another top collision and the latter topical the ladder top can be just right here but we need to scale it [Music] just just like that i'm changing the modulated value because i want to see it better and that's it that's our ladder you can save this i will save this as a ladder toot as a tutorial and let's add actually and let's add a script to it so in our script script for the ladder we'll need a few variables so let's [Music] declare them we we want to know what the what will be the height of our ladder so export variable ladder tiles because we will be setting the height of our ladder in tiles in my case the default volume will be one it will be one tile height you can set it to whatever you want it can be set uh later and it should be set later when you are placing the ladder in your scene and next we need to know what's the tile size so let's take our declare tile size in my case it's 16 pixels and we need to know the ladder to store the ladder height in pixels so it's ladder height and it's equal to ladder [Music] tile multiplied by tile size [Music] next we need few helper variables and we need some references for our nodes over here [Music] so already our ladder area is equal to this [Music] on red i will copy this four times we because we need [Music] four of these this will be for collision collision this will be top well you can leave those question marks it doesn't really matter collision and this will be top or top collision as i said when the ladder is loaded and ready in your in your game we'll be calling a function that will set up the ladder [Music] automatically so we will write a function called ladder setup call it so let's write the body of this function [Music] uh at the beginning we need to set the ladder's height and it will be exactly what we wrote [Music] over here so let's just copy this now we need to set the extents of our ladder [Music] rectangle shape to this width and height are the doubled values of x and setting extents is causing the width and height to draw or shrink from the origin and point of our rectangle shape to d so to set the proper dimensions of our ladder we will use our tile size and ladder height in pixels variables and half them so to shape the extent to to set the shape extents we'll use our helping variable shape [Music] extends is equal to vector 2 tile size divided by 2 and another height also divided by 2. i think we should store those extents in a variable so let's declare one ladder area extends is equal to let's copy this get extends [Music] now we need to set the right position because now we'll have something like this revert those changes because remember this is the ground level this is uh the ladders bottom over here and we are setting uh we are set we we set the height in tiles so for example we have three tiles so we change the number of dials like that and the ladder is the position of this area is still in the same place so we need to move this a little bit up [Music] so let's do this and this area larger area extends will help us doing this so platter area position position on y-axis is equal to ladder area extends [Music] y-axis but we need to move it up so we need to take the negative value of ladder area extends y [Music] next we'll do the ladder top or we can also declare we will not be changing the extents we will be changing just the its position so let's declare [Music] helping variable and this will be another top collision shape extents [Music] okay and now let's set its position [Music] and the top position y is equal to negative value because we are moving it up a flatter area [Music] rather top extent why and since [Music] so by moving a ladder top by the value of negative top extends y we will move it only here to the half of the height of of our ladder so we need to multiply it by two and also we need to extract uh extract the extents of our ladder stop so plus ladder top extents why let me explain this to you so we set the position of our ladder stop with a double value of extension of all no i just made a mistake hurry up yeah this should be area because another top position so we set the ladder stop position from here the value of doubled uh extends on y-axis of a ladder area collision so it sits just right here but we also need to move it a little bit down by the extent on y-axis of this ladder top collision because we need to move this like that oh it's not [Music] but it's not higher than the ladder area you can see i have moved something oh okay so that's it for the ladder and well we can also set the ladder top collision [Music] collision one way [Music] collision margin to something small 0.1 will do oh and i just forgot you need to set this as a one-way collision so you can see its arrow over here and that's it for our ladder next we move to our player's code as for your player you will need a few notes if you don't have one please add some raycast rename it to to your liking and enable remember to enable this also you need to add area 2d i will call mine detector and i will add a collision shape to it which and set the shape i need to scale this remember this shape has to be inside placed over over the collision shape of your player but it it can exceed on any axis so place it inside and modulate it so it's more visible oh i think i center it move it on the y-axis so it's something like that just because i'm lazy and don't want to write it all over again i tell you what you need to include in your character's code first declare a variable that will contain information if the player is currently on the ladder it should be a boolean variable and its default value should be false you may also want to declare a variable for the player's speed while the character is moving up the ladder climbing requires strength so the character should move slightly slower than normal on the ground for convenience declare yourself variable that store references to the detector object and raycast next you need a function that checks if the player is standing at the top of the ladder for this we'll use our raycast in the function body declare a collider variable and set its value to no next we need to check if the raycast collides with any object this is what the if statement is for we are checking if raycast collides with something and if yes then we are assigning this object to collider variable now we can check if this object is actually the top of the ladder we know that in our ladder scene the top of the ladder is called ladder top so we can check if the string ladder top is in the name of the object that the raycast collides with if it's true we return true otherwise we return false i added the upper method here because i never remember if my object names are uppercase or in lowercase to uppercase method changes given string to uppercase so problem is solved next function will check in each frame if the character is within the ladder area in this function we'll use our characters detector we need to save colliding areas on a list we can do this by using the get overlapping careers methods which returns a list of overlapping areas next we check if the list is not empty which means its size is greater than zero if it's not empty then we can check each element on the list using a for loop one by one we are assigning each element to a real variable and we are checking if the string ladder is in our real name but we also need to check if the player is not jumping if he's and he's not on top of the ladder if those three conditions are met then we can set the on ladder variable to true if the list of the areas is empty we can set another to false at the end of the function we can also return the value of ladder you never know when it will come in handy and you can always delete this line of code the jump function is simple first we are checking if the player is underground or on a ladder if yes then we check if he press the button for jump action if yes then we set another variable to false drive it to normal value in my case it's 10 and we are applying jump action now we will deal with the function responsible for moving the character on the ladder this is where you are going to have to feel around because i don't know what kind of platformer controller you have chosen but there are things you need to take into account first we need to check if the character is on top of the ladder and wants to go down by pressing the move down action key add that one to your input map and assign the key for it in the project settings so when those conditions are met we set our gravity to 0 and on ladder to true now we need to move the character down well we're not actually moving the character we are just changing its position by a small amount next we check if the character is in the ladder area if so we set the gravity to zero then we move the character up or down if the character does not move then we stop it next we need to call our jump function if the player is not on the ladder then we set the gravity back to normal value i also have this function which is responsible for moving my character to left and the right this is where you have to call those two functions that we just wrote jump function and ladder movement function and also remember to call proper functions in your physics process function so apply your gravity check if the player is on ladder and apply your movement and i hope everything will work for you i tried my best to explain everything and if you have any questions feel free to ask i will try to answer them it will be actually much easier for me to answer in writing than talking about this but yeah i hope you like it and see you next time [Music] you
Info
Channel: bad-sad
Views: 792
Rating: undefined out of 5
Keywords: huri, mightytom, tomek, hurifein, badsad, godot, game, tutorial, ladder, 2d, platformer
Id: AYghSIwec-E
Channel Id: undefined
Length: 23min 45sec (1425 seconds)
Published: Wed Aug 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.