5 Tips for Better Platformers in Unity (With Code)!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

That was really useful, thanks!!

👍︎︎ 2 👤︎︎ u/Maru1pir 📅︎︎ Feb 20 2020 🗫︎ replies
Captions
hey everybody this is games plus James and today we're gonna take a look at five tips that you can use for your platformer games to make them feel better and make it feel more interesting for your players as well as some code examples of how to implement them in your own games so without further ado let's go ahead and jump in and start looking at what we can do to make our platformers feel better so first up on our list here is controllable jumps so at the moment if I just go ahead and play here I've got a very simple platformer project set up and if I jump no matter how long I press the button then I hold it for ages or if I just tap it he always jumps the same height and for your players that doesn't feel great because sometimes you want to do a tiny little jump and it really feels good to be able to do that so what we're gonna do is very very simply to be able to control our character's jump all we have to do is make it so that when the character releases the button that makes them jump we just stop the player going up as fast it's very very simple so I'm gonna jump into my playercontroller script over here and here I have the code that controls the jumping so when I press the jump button down if we're on the ground then I apply some velocity upwards and right below that I'm gonna write if input the get button up so when we release the button and when we release the jump button we also want to make sure that we're currently traveling upwards because obviously if you're going upwards that's when you want to slow your jump down if we're already moving downwards then we've already let go of our we're already finished our jump we can't make it jump any smaller at that point so here we're just gonna say the rigidbody that velocity the RB is just the reference I have to the rigidbody2d up here so the orbit dot velocity dot Y as long as that is greater than zero so that means we're moving upwards if we're moving upwards and we release the button then make an adjustment to the rigidbody velocity so we're gonna say there is your body dot velocity equals a new vector2 we won't make any changes to the x-axis but on the y-axis we'll say take the current velocity of the y-axis to read your body duffel a CDOT why and multiply it by 0.5 so we're basically just saying hey go up at half the speed you're currently going up at so if I save this jump back into unity let it compile and you'll see we're just a couple of lines of code we now have the ability to if I tap us we'll do a little jump you can see beside the ground here but if I hold us I do a big massive jump so very very simple very easy to do and really great for improving how the game feels to your players number two on our list is adding hang time or some people refer to as coyote time basically a little bit of time after you walk off the edge of something where you're able to jump because there's nothing more frustrating than walking over the edge in a platformer and thinking oh I press the jump button but it didn't count for some reason so technically you'll be in the air when you want to jump but you want your players to be able to go oh I press it jump slightly too late but that's okay it doesn't matter you want to be extra generous you always want to be generous to your players so they don't feel that they've missed out on an input so let's jump into our script and see how we can easily handle that so again open my playercontroller script at the top up here I'm gonna add a couple of variables in I'm gonna add a public float for hang time and this is how long we're gonna allow the player to continue to press the jump button after we've left the ground after we've walked over the edge of something then we're gonna add a private flow that we'll use to keep track of it that we'll call hang counter so then down here before we actually jump up in the air we're gonna add in a little check here so we're gonna check if we are on the ground so I already have here we're setting and is grounded variable using some physics checks so where we can already know if we're on the ground so I'm gonna say hey if I'm on the ground then set my hand counter to be equal to the length of time we want our hang time to have and if I am not on the ground that means I've walked over the edge so make the hang counter count downwards so Hank enter minus equals time Delta so then when we want to jump instead of checking to see if we're on the ground what we do is check if I just delete this we'll say hey if we press the jump button and the hang counter is greater than 0 at 0 F like so I'm actually just gonna go up here I'm gonna set a default time for the hang time to be lets say 0.2 F so you have 0.2 of a second after you've walked over the edge will allow you to do an extra little jump so let's go back into the game then and see that an accident as you can see very very simple to do but it really helps feel make the game feel a lot better for your players it's something invisible the player won't really notice it but you really know it was that if it's not there so it's something that's included by default with most platformers it's not something people mention the whole time but it's just a way to make the game feel a bit better so now if I run over the edge here you can see oh there's a whole extra bit of time here obviously you can tweak this if you don't like like if you think that's too long of a time you can always shorten it down if you want to but I find 0.2 of a second is actually pretty good too to be generous to your players and allow them to have to save any missed jumps that can happen when you walk over the edge so there you go a little bit of hang time our coyote time number tree on our list is sort of the opposite of hang time which is allowing you to press the jump button a little bit before the character hits the ground so there's not more frustrating than if you're running along to your game and you jump and you go to press the jump button again and you're like oh no I press the jump ever so slightly too early so right here I'm pressing jump just before I land that should really register as an extra jump that a player wants to press when they touch the ground so what we'll do is add a tiny bit of time that if the player presses the jump button before they touch the ground it'll actually be registered as a jump so let's stop this running go back into our script again I'll my flare controller script here we're gonna obviously need a couple of variables for this so let's go back up to the top and below where we have the hang time variables I'm gonna a public flows for that's called us to jump buffer length so this is the jump buffer it's not what we're gonna use to say hey we're jumping for where we press the jump button right before we hit the ground so jump offer length is gonna be how long we want it last then a private flows for jump buffer count will be hell on glass or how we keep track of that number as we go so what we'll do for this is if we scroll down here below where we did our hang time stuff here I might comment in here to manage hang time and then down here we're gonna manage the jump buffer and then here we're going to say if the player presses the jump button down so if in push da get button down jump so if the player presses the jump button I put my brackets all over the place there there we go if we player press the jump button then set the jump buffer counter to be equal to jump buffer length so let's say we set this to be we're gonna set this to be equal to point one of a second so then we're setting the are counter to be point one I just realize there's only one F and jump buffer camp there we go we set it to be point one and then down below that we're gonna say basically if we haven't pressed the jump button down then we're gonna say else we'll make it count down so jump off counter minus equals time - Delta time so we're making a countdown as we go okay so with the counter nail counting down obviously what we need to do down here is when we want to jump in the air instead of detecting that the button has been pressed what we're gonna do is I'm gonna delete all that we're just gonna use the buffer to keep track of this so now we're here we're gonna say if our jump buff counter is greater than or equal to zero we'll say if it's it can be exactly zero as well that's that's all we'll be extra generous we always want to make sure we're kind of erring on the side of caution so it didn't below here we're basically saying hey if I jump offer is a has a value then we're having a jump and we're also saying if our hang counter has a value so we're no longer checking here to see if we press the button and we're not checking to see if we're on the ground we're now keeping track of two counters and this is what controls our jumping so then then below here we need to also make sure that we say jump of counter should now be equal to zero because obviously we don't want our jumps to continually happen over and over again we want to make sure hey if we've registered a jump then set our buffer down to zero so that we don't continue jumping up in the air so say this jump back into unity let that compile and now if I go in here I can jump and just before I hit the ground I can hit there so we'll actually be a little I'll just be a little bit more generous just for the purposes of things showing this off so let's say we have half a second if I jump and there we go if I press jump a long time before I hit the ground I do an extra sort of bounce as we go there we office with a nice big jump but also if I'm just on the ground we do legs jump up in the air and we still have controllable jumps and we still have our coyote time walking over the edge like that so perfect we can now jump lace and jump early in our game and basically give our player more control over what's going on number four on our list is moving the camera ahead of the player when you're playing a platformer game like this if I'm moving to the rice here I have the camera focusing on the player in the middle the counter has a very simple script on the camera that just has a target which is the player and moves with the player on the x-axis but we don't want the player to be right in the middle if you're in a platformer game like this moving to the rice you want to see what's ahead of the player on the right and equally if you want your player to move back to the left you want the camera to show what's ahead of the player to the left and we can do this very very simply there's lots of information out there about customizing your cameras and putting extra work into your camera is some that is really really worthwhile to do and to be honest could be a whole series of videos but we're gonna do a very simple trick to make the game immediately feel a lot better so I'm gonna stop this running I'm gonna go into my player script and if I dropped my player down here I have a cam targus object which is a child of the player if I just zoom in here it's just an empty object directly in the center of the player at the moment we're gonna move that forward and make the camera move to here so so I'm gonna go to the camera up here and I'm gonna make sure that the camera target object is the target of the camera script that I have set up here again this is very simple script doesn't really do anything fancy it just follows the player and then I'm gonna go back to my player go into my player controller and I'm gonna add a few variables up the top up here I'm gonna add a public transform for the camera targets because we're gonna move that ahead of the player we're gonna add a couple of floats one for how far we wanted to move ahead so ahead amount and we'll have another one for ahead speed and that's gonna be how fast it moves to that position ahead of us and then all we're gonna do is scroll down here and we'll do it after all this stuff here before I do it animations and things like that I'm gonna do it in this section here so this is gonna do be what we move the camera point this so when you want to move the camera point all we're gonna do is check and see hey have we do we have any input going on at the moment so if input does get access raw on the horizontal axis so if we're moving anywhere on the horizontal axis and we're gonna say is that is not equal to zero so that means that if there's any input left alright doesn't matter which way we want it to go then what we're gonna do and here is say the camera Targus we're gonna move not just the position of it but just the local position so we're gonna move it relative to the to the player itself so here we're gonna say the camera target local position is equal to a new vector3 and on the x-axis we're basically just gonna move it forward or backward depending on water impetus so we're gonna say that we wanted to move to on the x-axis ahead amount which is how far we wanted to go ahead x whatever the current input is so input that get access raw horizontal like so and then we're going to make the under y-axis obviously we don't want to change it so we're gonna say can target dot local position dot y and on the z axis the same the camera target local position dot Z now I'm not using the ahead speed variable just for the moment but we're just gonna see what this does and then we're gonna come back and make one other little change to make it a little bit nicer so I'm gonna go in here save this I had a man let's say we'll move it ahead and to say I know SEP there's a head speed just to be one for the moment so now when I play oh we haven't got our camera target assigned to just realize so camera Tigers into that slot on the player there we go now when I move ahead there we go you can see it does move ahead of the player but it kind of snaps and jumps around so that obviously doesn't feel great so we don't really want it to kind of snap into position like that so instead what we're gonna do is go back into our script and in here instead of just moving or setting the x-value directly what we're gonna do is make it LARP to that position so we'll say Madoff LARP so we need to know where we start at and where we sat at start at is the camera targus dot local position X where we want to go to is the ahead amount multiplied by the input that we press and how fast we wanted to get there is the head speed multiplied by time dot delta time and then we'll close that brackish save this back into unity let it compile and once it's done here we go we can play and now when we move ahead you can see the camera moves ahead of the player like so so now we have we can see a little bit more ahead of what we're doing and when we turn back the other way the camera knows which back to the other way so let's make this move a little bit faster because that switch is not very much and we'll move it ahead a good bit more you can obviously play around with these values and see what works nicely for you so now I can make it move a good bit ahead of the player there we go we have loads of area that we can see any dangers coming up ahead of the player and we switch back the other way we can now switch and see the dangers coming in whatever direction the players facing ok so the fifth and final platformer tip is to add some dust into your world basically impact little dust effects when your player is moving around so I've got two particle systems set up very very simple we've got a footsteps effect so if I turn it on here if we zoom in and just place you can see it creates this little effect of dust not into fancy one really important thing to note with it is that you need to make sure that the where are we the simulation space is world and not local so be local by default but if it's local as we move it around you can see all the particles kind of stay relative to each other but if we switch it to world as we move it around you can see it creates the particles and leaves them in the right position in the world so with that done we've got that in place let's set it up so that this will work with the player so I'm gonna have a turn on here by default and one other thing to note is in my emission here we have a rate over time 35 that's how fast our particles are appearing so obviously if I press play here now there we go we get nice little dust practical effects as we're moving around I'm actually just going to just to make things a little bit more simple I had quite high values here I just realized for our camera I'm gonna make that a little bit more straight forward so when I move around there you can see a little dust trail behind us but also the dust gets left behind as we jump up in the air so we don't really want that so let's jump into our scripts to fix that so I'm gonna stop this running jump into our playercontroller script and up the top up here we're gonna create a couple of variables for our practical effects so I'm gonna a public particle system that will call recall list of footsteps and then to be able to make changes to how fast those little and the the rate over time is happening so how fast the particles are being generated will have another private variable called particle system dot emission module and we'll call this for emission and the reason we do this is because we can't make this a public public variable that we can assign but what we can do is make this a public variable that we can assign that particle system to us and then in our start function here say the photo emission will be equal to our footsteps particle system that we create and within that will access the emission variables so just to demonstrate that I'll just save this I'll go back into unity here and if we go to our player let's assign the footsteps particle effect there like so so and when I press play what it does is it'll access this footsteps effect and I'll go in here and go to a mission and I got a debug view here yeah it doesn't show up as a variable the particle system that emission module because it's just not something that can be represented like that in unity that's why we have to assign us to the parent kind of object and then access the emission within it but now that we have access to that if we go down here and below were moving our camera point let's do our practical effects stuff in here everyone say this is what how we're going to show the footstep effect and we're going to then say if our inputs to get access raw horizontal is not equal to 0 so if basically if we're pressing any inputs and we are on the ground we're just going to use the fact that we're on the ground we're not gonna use our jump over anything like that because we're not trying to wait for a jump here we're just gonna say as long as we're on the ground and our we're pressing some input then we're gonna make sure that the force emission the rate over time is 35 so that was the value I had set in the editor otherwise so if we're not pressing any inputs and when our were not on the ground if any of those things aren't not true then we're gonna say first emission the rate over time equals zero so basically this will stop spoiling things if we're not pressing any input or if we're not on the ground but otherwise it'll allow us to have our particles show so save this jump back in here let it play and now when we walk there we go we've got particles appearing but if I stop no particles and if I jump in the air no particles so that's perfect that's one little impact effect that looks really nice but the other one that's really important to add as well is when we jump and hit the ground we want a little puffs of dust to pop at so I've got another particle effect here called the impact effect let's turn this on and if I play this I'm going to turn on we leave on Newton for a moment press play you can see all it does is create a nice little puff little cloud of particles in the air so what we're gonna do is I'm going to stop this looping first of all and what we're gonna basically make happen is that this will move to wherever the player is when we hit the ground now at the moment the way it's set up is over here under stop action it's important to note that we have disable marked obviously what this means is once it finishes playing true its duration which is two seconds long it'll go to our stop action and I'll say disabled so after two seconds it'll basically deactivate itself like so so what we're going to do is reactivate us at a new position whenever the player it's the grip so let's go into our scripts and let's set that up so first thing obviously we're gonna need is back up the top here a reference to that impact effect so let's go here and say public particle system impact the effect like so and how do we know whether we're on the ground and in pitch time or how do we know that we've just hit the ground well we're gonna create a private bool that will tell us whether we were on the ground in the previous frame so we're gonna say was on ground like so so then down the bottom down here underneath where we're showing our foot type effects we're gonna manage here showing the impact effect like so and we're gonna do an if statement here in a second but before we do that we're just gonna say here was on ground equals is grab so we're just saying hey at the end of this frame updated so that whatever on the ground is using the current stored value so then let's imagine let's imagine we're in the air on one frame so was on ground is false because we're not on the ground here so it gets set to be false then on the next frame the player hits the ground so then it does the is ground a check and says hello we're on the ground now so then down here we can do some code here where is grounded as true and was on ground is false so here we're gonna say if was on ground is false and is grounded is true so that means we were in the air in the previous frame we're on the ground now so that means hey we just hit the ground so let's do an impact effect so then in here we have our impact effects first thing we're gonna do is activate the objects of gameobject.setactive true to make sure that it gets reactivated in our scene then to make sure that just in case the object was already active that we're not just resetting it or we're not just making it continue to be active in the world because then the two we'll count down and it would just deactivate and we would get no cloud at all what we're gonna do here is say impact effect does stop so that stops the impact from playing we're gonna move the impact effect to wherever our player is so impact effect - transform dark position on our trails dot transform dark position where do we want to move it to we don't want it to just move to the player what we'll do is move it to where the footstep effect is so we already have the footsteps particle system effect let's move it to where that is because we know that is at the player's feet so we're gonna say impact effect that transformed our position equals footsteps dot transform position why does that keep auto good I don't know why that keeps auto completing trails but anyone footsteps are transform a position and then we'll say impact effects - play so it'll start the effects playing again ok save this jump back into unity let it compile and now once we play oh I just realized we didn't assign the impact effect in here there we go so we can hit play in the real you can see the player hit the ground and we got a nice little cloud of little impacts happening as we go so very very simple to do as well you can see we've got a whole bunch of extra techniques now to make the game feel a lot better for your players even though all you've done is 20 minutes of work and made very very simple changes to the game now as I said these changes are very simple but they have a lot of impact on the player and really helps make your platforming games feel a lot interesting so there you go thanks for watching this video I'll back some more tutorial goodness very soon coming up this weekend if you haven't already is the game + jam tree game jam our community game jam it's gonna be a whole lot of fun I can't wait to do this and we're gonna make some really cool games one weekend so make sure you sign up for that at the link in the description down below I will be back soon with more sorrel goodness as I said and in the meantime keep being awesome
Info
Channel: gamesplusjames
Views: 65,569
Rating: undefined out of 5
Keywords: unity, unity tutorial, learn unity, unity games, unity school, unity beginners, learn to code, unity c#, c#
Id: 8QPmhDYn6rk
Channel Id: undefined
Length: 26min 37sec (1597 seconds)
Published: Wed Feb 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.