Godot Endless-Runner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Interesting thanks!

👍︎︎ 2 👤︎︎ u/cy_anst_a_tistician 📅︎︎ Feb 23 2021 🗫︎ replies

How does mine look exactly like urs and have an error

👍︎︎ 1 👤︎︎ u/Kodycoyle 📅︎︎ Feb 23 2021 🗫︎ replies
Captions
hi so the other day i already uploaded a quick overview of how to make an endless runner in the go.engine today i'm making a full tutorial to show you step by step how you can start out so let's get to it first of all i am going to create a 2d scene just call it game and let's see what is this going to need we're going to want a no to deep child to be our player and we're going to want something to put game areas in so i'm creating another note2d and just call it areas this one will be empty for now but eventually it's going to be filled up with various areas via code so let's build the player i'm going to move the player down here around about and let's see it needs a sprite so we can actually see it um texture that's a bit small let's see about that now is it here scale oh tall three four four seems fine for the screen size you know get a fit now let's give the player a camera to d this is just so it follows when the player goes up and down the player isn't going to go left and right at all only gonna move up and down from jumping and stuff and moving up slopes and we can add another let's see we can add a raycast 2d i guess because we want to be able to actually see if we're colliding with the floor let's see make this like 15 long maybe the exact numbers are a bit of a matter of experimentation what works for your game but roughly like that seems fine now we need two area to d one two one is our hitbox if this gets touched by anything we die the other is our i don't know headbox i guess well then let's be consistent about the b the hot box is going to prevent us from moving up through ceilings now we make a collision shape give it a rectangle shape now this is the hitbox so we want it roughly let's see should of course touch things before the raycast can move us so roughly like this seems fine for now now the add box needs one too again just a rectangle shape will be fine make it flat move it up a bit something like that if we jump up and anything touches this we don't move up further and that's the jump yeah sure we can save the scene and i guess we're gonna want some scripts now first of all let's add a script to the player and type script and let's see we are gonna want a few things here unread d4 ray equals with our raycast 2d and yeah we're definitely going to need access to that one we don't really need a ready function right now so i will remove that let's go function physics process that's the important one well first of all we're going to want to not fall through objects so for that we need to check our ray cast so if rey dot is colliding then a few things should happen first of all we want to calculate the origin let's just shorten that not origin there that's where our ray cast 2d starts our collision point is ray.get point as a function yeah there we go and our distance then is just origin dot y minus collision dot y i'm taking absolute because that way i don't have to think about which one is bigger now var depth because the distance is how far if we go on the player how far away from the player our array is intersecting something that's the exact opposite of what we want what we want is how deep are we inside of an object so that's what the depth is going to be so we can say ray.cast 2 dot y since we are going to completely ignore the x axis we only care about y we can just use that minus the distance we just calculated and that tells us exactly how deep we are inside of an object so now if we do position position dot y minus equal step then that should work let's see is there anything missing here because i think it isn't well let's just try it i'm just gonna put a static body into the area this is temporary just for testing purposes put it down here give this a collision shape rectangle make it longer actually i'm going to rotate it a bit just in case there let's see if that works obviously it wouldn't right now and need to actually collide with it so let's see if that works and it did it actually pushed us up until we are standing properly on it now let's give our player some gravity to make this a bit more interesting okay for that let's see let's first define how strong our gravity is bar gravity is equal to 15 let's say and we need a y velocity we don't care about the x axis so i'm just gonna do y val starts out as zero and let's define a maximum because we don't want to start teleporting if anything goes wrong like if if we fall for a long time we would start falling through objects that wouldn't be very fun now i need anything else here i guess we only want to apply gravity when we are currently not touching the ground so far grounded is equal to false let's use that and then we can go in here and say if not grounded y velocity equals minimum of max y velocity and y velocity plus gravity how do i type this around so often right so in any other case else y velocity is gonna be zero because if we are grounded if we're touching the ground we don't want any y velocity and after doing this grounded is equal to false now in which case is grounded ever true grounded is true if this colliding thing here happens so here at the end i'll just say rounded is equal to true and i think that should be it the position still needs to be adjusted but i think that seems fine so let's go in here and say position dot y plus equals y velocity times delta because all of our movement should account for delta here and we are falling but we are stopping here nice and simple all we need no movement on the x-axis now we want to see what happens when this thing actually moves i guess so let's see actually before we do that let's just take a look at jumping quick so i'm adding another thing here far jumping equals fox because that way we can tell whether we are currently in a jump and then here i can just add var jump equals input dot is action just pressed jump and not jumping so the is action pressed the jump action i already defined that before so i can just show you here input map jump right here if you don't have that just edit i just put the space button but you can do whatever you want so if i'm not currently already jumping and this isn't the case then jump is set and then if jump jumping equals true and y velocity equals minus 500 let's say i should probably set that up here so let's see jump speed equals -500 jump speed now that's a bit cleaner now that part is done we still need to go in here and say jumping equals false and we need to make sure that our colliders here don't actually cancel our jump to do that we can just say if not jump do that stuff because if we don't have this then even though this stuff moves us up it is actually going to cancel our jump here unnecessarily it should only cancel our jump after we reach the ground again this does it doesn't matter for this by the way whether you are and we're jumping fine that's actually i think the player pretty much done we can take a look after the area movement is a thing so let's add a script to the game scene now what are we gonna do here do we need a ready function i guess we want at the very least to randomize in here and probably spawn in some stuff later right now we don't care about that yet though but we do want a speed for the map to move at let's set it to 200 and say funk funk physics process for area in areas so anything that's a child of this thing here is automatically going to be affected no matter what's in there uh we can't say dot areas we need areas dot get children i'm not saving this up here because this is going to change occasionally so for each area we can say area dot position dot why is this not updating x there we go minus equals speed times delta okay that's pretty much all for the movement so whatever is in there it's just gonna be moving to the left at all times at this speed up here so technically if you want the speed to increase over time just change this variable and this is gonna change let's play it and it moves and we get pushed up by our raycast simple now that is actually a big chunk of stuff out of the way the last thing we need at this point is to actually make some areas we can work with so i'm actually going to keep the static body to d here but i'm going to change the collider to a collision polygon simply because that gives me a bit more options of what it can look like so if i'm just gonna let's see start at roughly the player height and i can just define a few things make this slide up here and let's let's slide it up quite a bit go like that go down here and finish it so that's our first one now let's duplicate it actually can i just change this oh no i don't have to to make it unique here i only have to do that for collision shapes so it's going to start at the same point because that's where the player is at the start so that's a good place to start it then i can just do a few points you know i just want them to be visually distinct for now i'm not trying to actually go into level design you can figure out what you want your levels to look like yourself i'm just showing an easy way to get this set up for starters now right now i am making every every one of these chunks the same size which is the size of the window so i'm going to spawn one here then one there and then they're gonna move to the left until the one that spawned off to the side is here in the center and then the one here is gonna be deleted and the new one spawns here that's how i can ensure the endless runner does indeed run forever which doesn't mean i don't have to account for sizes or them going at different heights or something like that here right now you can totally do that there are plenty of options but i just want to show the simplest and quickest version to set this stuff up and the details can be adjusted later now let me make one more just so i have three examples to work with and for this one let's see i'm actually going to be yeah let's make this one pretty straight and add a second collision body which i can move up here and then move some points on it uh no i didn't mean to make new points and then just to move the existing ones so i can do this and i can put this up here and i can put this up here and that should be fine just to have some examples now i'm going to save each one of these as a separate scene um a all right i already had a segments photo well it's fine i'll just make a new one because that already has some stuff in there and now we can use it freely so this one here save branches scene in segments and i just call this hey now this one save branch of scene b now this one save branch of scene c save delete them all we don't need them here and the player is floating in emptiness once more now we're going to want these scenes to actually be known over here so what we can do is far scenes equals or i guess segment is more accurate segments uh preload press colon slash slash segment slash a dot tscn easy names to remember that's why i use them segment slash b dot tsen preload res pronounce segments c dot tscn there now our game actually knows what all of the possible segments are if we have more in that folder we can just go there and that should be fine now let's define a spawn fun spawn instance function and set some default parameters x equals 1024 y equals zero by default that way i don't have to think about it much later so actually actually doesn't matter because there's only like one case where this matters much let's just say x and y far inst equal scenes at position now we want a random instance from up here so we can just say random integer mod length of scenes list then we take the instance from that and if i don't have anything all right because i renamed it earlier consistency segments then this instance position needs to be set to vector 2 x comma y and arias gains this as a new child and that's really all we need to spawn in a new instance now at the very beginning we want to spawn in two instances right away so let's just say spawn inst 0 comma 0 and spawn inst 1024 comma 0. let's take a look okay i have the hydraul why do i have the right row actually this is where it should be for zero comma zero now position is not zero zero like this okay in that case let's make this zero zero so i wasn't paying attention there and just bring down the entire area and that should be fine like optimally i guess it would be great if we had the areas roughly around the player's feet because that way it's easy to say that anything at zero zero is exactly where the player is going to spawn at potentially and everything after that we can go ham sure let's see um is that fine it is fine so we can move up here we can fall down here everything working good and the map ends because nothing more is spawning nice now game script let us spawn something new and get rid of the old stuff so after we move here if area.position.x is less than minus 1024 so if our thing is more than one screen with off to the side on the left we can delete it q3 so it's safe to delete and spawn a new instance while we add it so spawn ends at area.position.x plus 2004 1024 times two comma zero and where it's going to be spawned off to the side basically two screens over now optimally we should never notice any of these disappearing or appearing oh that's not quite working is it all right i don't think i actually programmed in the uh ceiling check yet that's fine we can do that in a moment i also haven't programmed in depth though that's fine but everything else seems to be working as intended which is nice so let's do these last two things we already have the hitbox so let's just say if a body enters we trigger this do the same thing here if anybody enters three or this on the player please now if the hitbox is entered for now i'm just going to delete the player it's not going to stop the background from scrolling but you know you can put some more work into that i just want to make sure it's visible what is going to happen and here in the case that our head box had entered a ceiling we just say we don't go up anymore so our y velocity is the maximum of our y velocity and zero so if we're falling down we just keep our velocity if we're moving upwards we set our velocity to zero let's give that a shot okay we now don't get stuck when hitting your head now let's try to die i didn't haven't actually built in a whole lot of ways to die but if i just don't move here i'm gonna crash in here and die and that works nicely yeah that is pretty much the very basics of how you can start out with making an endless runner if you want to expand this you can adjust the system a little to account for different sizes of segments that way you can have larger segments smaller ones however you like and spawn them in and you can account for having them at slightly different heights so if you want to go up over a few segments or down occasionally that's fine as well lastly of course this jump mechanic here which always jumps the exact same height and doesn't have a lot of very smooth movement that bounces a bit when going downhill on slopes and stuff like that there's a lot more you can do to make this player controller feel better during gameplay this is really just the very beginnings of a endless runner don't use this as is use this as a basis and see how you can make it better a proper jump and with all of the things you can do to make it feel good would need an entire tutorial all on its own and well i suppose for more complex map spawning it would be possible to have a separate or an additional tutorial later showing how you can make these maps more interesting but for now this will be all for today bye you
Info
Channel: iaknihs
Views: 2,709
Rating: undefined out of 5
Keywords:
Id: 0amclwspR0w
Channel Id: undefined
Length: 26min 34sec (1594 seconds)
Published: Mon Feb 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.