Knockback & Stomping Enemies - Unity 2D Platformer Tutorial - Part 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Don't know if you noticed, but the first time you kill an enemy the player takes damage too. It happens at 18:20

👍︎︎ 1 👤︎︎ u/TitoOliveira 📅︎︎ Mar 06 2015 đź—«︎ replies
Captions
hey everybody this is Gaines plus James and welcome back to our 2d platformer tutorial here in your z5 whoo and so if you didn't see my short little video I made it should go up the same day as this video and just looking at the slight few changes that have taken place between the older version unity and the brand-new unity 5 version if if you're in the middle of upgrading to that and there's a few changes you might want to be aware of at the game gets slightly broken but perfectly fine easy to fix so just check out that video if you haven't already and we're going to continue on with today's episode and today we're going to look at and adding knock-back to our player when it gets hit because at the moment when he walks into an enemy it kind of looks a bit bland if we go over here when the enemy starts moving OOP we walk into him we just kind of push back and forward with a midol if we try walking to him we can push him but that's no good we want to get knocked back when we get hit something like that and we also want different ways to deal with the enemy for example we're going to do if you jump on the enemy and we hurt them that way or if you have a little weapon you can stab them with so we don't need a couple more enemies wandering around though it's kind of born just having one enemy guy so we're gonna take our enemy we're gonna change from any way we want to just enemy go to our prefabs folder here we're going to drag the enemy and to make a little prefab at him so now we can just drag in a couple more into the level pop them in there and hit play and those guys will wander around just like our original enemy does we see them deep there he goes whoo don't hurt me Tony problem is if they walk long oh oh no we walk we're stuck we walked into each other so we want them to turn around when they face each other or alternatively actually you know what probably makes even more sense we don't want her to turn around when they face into each other we want enemies not to collide with themselves so what we actually need to do first is give them a special layer so we'll just go to our first know we go through our prefab of enemy and we go to layers here we don't have a lay an enemy category set up if we go to add layer all you do is just add in enemy there I go back here I will just select enemy and we change yes we change whatever are inside him we should be just like the yeah just the two points for detecting now that'll have changed all these guys back here you can see layer enemy any wanna me to these guys are all enemy and then in our project sentence we go to physics 2d and will scroll down to enemy obviously enemy down the bottom here lines up with enemy at the top so we'll untick that so what that means is enemy and any two objects in the enemy lair if they collide with each other they'll just ignore any collision and just walk through which walk through each other so we'll see that here now when we hit play we go back in one stack I bounces off the wall and it goes meets us forms oh just go walk by each other which is perfect because we don't need her enemies climbing into each order there gentlemen enemies they know how to walk around each other so that's perfect so what we're gonna start off with is dealing with this kind of knock back that we want to give to the player and basically we need this to happen on the player himself we could we could try and do at the point where our enemy and our enemy know our hard player on contact it will involve this script we could make it go from there and say add a force to the player but the way we're handling movements is we're taking the MOOC movement directly from any keys they're depressed and making that set to the speed so what we actually want to do is kind of override that slightly so what we're going to do is create a couple of little components up here our components are variables is actually the word I'm looking for and so we'll start off with a public flows and we just say knock-back and a public flows knock-back camps so the knock-back will hold the amount of force that we're going to apply to player when he gets knocked back and a knock-back count will be just it that'll be our counter and thing i will actually put another one above knock back and we'll do public flows knock back and length because so knock-back length will be how long the player will be knocked back for in terms of time and the knock account will be will set it to that amount of time and a minus of likkle zero for example and and we also want and we want to know whether the player should be knocked back to the to the left or not back to the right I depend on where the enemy is that's nothing against so we create we just create a public pool and knock from will do capital F knock from right just to see if so we say like if the enemy is on the rice then we want to be knocked to the left and if the enemy's on the Left we want to get out to the rice which is relatively simple so down here we have our movement cold which is handling this is the bit is handling where our player is moving left to right and we want to kind of say we don't want when our player is knocked back we don't want the whoever's controlling them to be able to move them still because once you're not back in the air you should be not back for a second and then the player can move so what we're going to do is we say if knock back count is less than or equal to zero then then you can do this bit here so what that will be is so we have a knock-back count and the player gets hit we'll set the knock-back counts to say I know half a second or whatever and then that time will take down and then once it hits zero again then you'll be able to move the player again and by default it'll be zero anyway so you'll always be able to move to player until you get not so then we'll say so then if that's not true we'll say else I want to put a couple of curly brackets here so next thing you want to check is okay so if it's not knocked back then we know that the enemy's and just is after knocking into us but we need to know which side is the enemy knocking into us from so we'll say if if not from rice which is our boolean thing to determine if the enemy is on the right or left and so if not from right is true that means that our enemy is on our rice so then we need so if our enemy is on a rice then we want to knock our player to the left so we'll go my rigidbody2d dot velocity is equal to new vector2 and we have enough back at the our amount of nut back which is just knocked back but we don't want to add a positive knock back we want to say - because we want to move our pair to the left if the enemy is on the rice so we go - knock back and we also want the same amount of force to knock him up into the air as well so there you go that's perfect so if knock back if the enemy is on the rice then do our velocity will go to the left and so then obviously if if not knock from rice then we just have the exact same line in the code and we'll just paste it in there but instead of - knock back we just have knock back because then if the enemy's I've left the menachem to the rice which is perfect and then the final thing we want to do here is we know that our knocked back count is greater than zero so we want to make sure that the knock back camp is always going downwards so we'll say knock back count - equals time Delta time cool so now obviously if we save this we don't have anything the secondly knock back but we can test it out just by hitting play here oh actually you know what'll be useful if we actually had some values put into the player so on pause or on play on play as some people call that stuff but whatever and so we set our knock back we'll set this to say 10 and the length of our knock back will set - we just said at the point - a second I don't know if we'll be an offer we'll see and so if we hit play once it finishes there we go so obviously not happening just yet and what we're going to do is when the player gets hit we're going to use the hardware and contact scripts and we're going to add something that'll increase will make the nut pack enough back count equal to knock back length so here we're just gonna say naught point - oh you see that knock them up in the air just like that and uh it it looked like it knocked him up a good amount as well so we say na point two again oh well you have to said this one's enough point is I yeah that's a nice little knocker man so we'll just change knock from right here to test this so again we'll say not point two oh he gets knocked back to the left that's perfect just seems like a good amount of knock to me and I've said knock too many times here now so now we'll go to our heart player on contact script and we will open that up unless I already have it open how I do of course and so here we're going to again this is going to be nice and simple and keep it nice and easily and we already know it would in this little bit here that we're caught that the enemy is contacting the player and so what we're going to do is create a variable of player here and to determine what the player is because this is what we're going to be able to access the script that has add a knock-back in it so we just say player is equal to utter dot guessed component and then we'll find the playercontroller script so that will make player equal to the other part the other the player who will be equal to the playercontroller script that is attached to the player that word that we have gone so then it's very simple once that's found then you can just go player dot knock-back count is equal to player dos knock-back lint and and then we want to determine whether the whether we should knock the player to the left or the right so we have our knock-back our knock from right variable which is exactly what we want to do and very simply we just need to decide whether the player or in this case the other is on the left of the enemy around the rice so it's very simple to do that also we just go if other does transform dot x so that means the x position of the other that the enemy is colliding with if uttered a transformer de X is less than transform not capital T small T transform if I could spell yes transform here we go transform dot at position dos X and I don't know why I just had transformed that X here because it should be transformed at position X there we go so basically if the other thing is is less if the other objects X is less than the enemy objects X then that means that the other is on the left so if that's true then player does knock from rice is equal to true perfect and if not and then we'll just say else because if that's if that's not the case then otherwise we have to set player dot knock from rice is equal to false so now that should be everything that we need the handle and knock back on our player so if we go back into unity and we hit play and once everything compiles again we walk over here welcome to and me oh oh no he knocked us back you just let him walk into us Bam Bam Bam oh no look we're losing all our health and I'm we're dead but and knock-back is working perfectly and of course you can fiddle around with however much knock-back is going on their hair and what you kind of think is appropriate and the reason for putting the knock back within the player script is rather than just having it in the heart player on contacting you then you'd have to have a specific you have to choose a knock-back value every single time that you created another object that you wanted to hurt the player so it's much easier to just put it on the player then every time he gets hit no matter what he always has a little default little amount that he's knocked back by so let's actually just try reducing it slightly and see how it feels hit play again let me go over here Boop there we go if he's bored oh no but there we go that's the basis of adding a little knock back but those enemies are annoying us too much now they're knocking us back too much we don't want them doing that the whole time we want to be able to take our vengeance on them so what we're going to do is and they give ourselves the abilities it's jump on their heads and squash so at the moment on our enemies we'll just go through this guy here if we look we created a little trigger zone around him and but that's going to cause some issues if we want to jump on his head then we're just going to end up hurting yourself every time so what we're going to do on him is go to his box Collider and we'll check we get the one that is a trigger i what we're going to do is grab the size and just shrink it down like that cuz that's still gonna hurt to player when he walks into him as much as if he was if this as if this thing was upper up or down but as much as if it was bigger sorry and it's still gonna hurt the player is still gonna be able to detect the player just as well but now when the player lands if the player lands on top of them it won't necessarily be able to hit hard to player every single time which is exactly what we want and what we're going to do on a player object if we go to them here what we're going to do with him is we're going to add another under him that is going to be just below speech and that's going to hurt the enemies whenever we land on them so we're going to go game object create empty child I I will call this head Stamper and we want to add a box Collider 2d to him box Collider 2d and we don't want it to be anywhere near that big we want to squash the size down just like that and what we're going to do is drag that down so it just blows feet there so then when he lands on me with this box hopefully when we create script this is the box will hurt the enemy so obviously at the moment now if we go hit play and walk over here oh well look at that sir I didn't make a so you have to make sure and remember to make it a trigger but if we jump on top at the moment oh gosh all the students the enemies Hartness that's no good to us at all come on play this and make sure each figure is triggered it's selected so now we need to write a little script to attach this object that will hurt the enemies whenever we land on top of them and again much like all the scripts we've been making it's all very straightforward and very simple as we're just going to create c-sharp script and we call this one set of hearts player on contact we call this heart enemy OOP if it's about enemy enemy um contact now perfect so we'll just open this up here and this is going to only have a few little simple and a few little simple bits and pieces to it that'll hopefully just make it all nice and easy first so we'll have to have and and I'm at the damage to give so we'll have a public int damage to give and the other thing we'll need is as you know that's all we need for the moment and so we just leave the starting alone and leave our update alone and we're going to go void um trigger enter 2d collider loop Collider 2d utter and put our squiggly little brackets in so now all this is going to be is if uh tur that tag is equal to enemy then utter - getcomponent enemy enemy health manager so find the enemy health manager that's attached to our enemy objects and with just using that script we just go give damage and we're putting the damage to give save - pop back in here I will make sure on our head Stamper script that we add - script we just create so once I finish compile and there we go so that was a our heart enemy on contact and will say tree because we wanted these enemies that have tree health so we just want to be able to stomp on them and get rid of them straightaway so if we hit play now we should be able to boom there we go enemy is destroyed boom this guy boom perfect that's what we want but if we just replay again and we're not really getting any kind of good feedback when we do that we're kind of oh we're just kind of smashing down through them it'd be a bit bit nicer if we could kind of bounce on their heads a little bit whoa there we go I need us track it after all together hey there we go stomp it on him stomp it on spikes and yes so we want to be able to have a little bit of a little bit of a effect to happen when we bounce on our enemies not when we hit our enemies to make it look like we're bouncing on it basically so what we're going to do here is simply we have this box that's always going to attach to the player so we can have a set amount that we're able to buy the player by that's what we want to do so we'll go public airflows bounce bounce on enemy and to be able to move the player we need to be accessing higher rigidbody so we go private rigidbody 2d my rigid body 2d now the thing is if we go back in here so this script is attached to our head Stamper and we don't want to get the rigid body I'm the head Stamper because we don't have a rigid body on that that's not doing anything we have a rigid body on the player which is which isn't the same object that the the script is attached to so if you go back in here what we want to do is find the rigid body on the under parent object so we'll go here my rigidbody 2d is equal to rather than just go and get components rigidbody 2d what we'll do here is we've got transform - - transform a parent so what that'll do it'll find the parent object and then then we're going to go get component so we know we know where our parent object is so it's going to go okay parent objects on you we want to get that component and find the rigidbody 2d close brackets and add some skirt some normal brackets so now now we have the rigid body of the player rather than of our little and stamp on heads object so now we know we have that rigid body it's very easy for us to just go my rigidbody 2d dot velocity is equal to new vector2 and and we're not going to change the x value at all because we want the player to be able to still move along if they're if there are bouncing on enemy or ending so my rigidbody 2d dot velocity dot X we don't want to change that but what we do want to change is our Y velocity to make it bounce in the air so here we're just going to say bounce on enemy and we close that and we'll hit that button there so now we'll save pop back into the game and once this compiles here down in the corner we will hit play and once all run here we go oh I forgot to jump there for a second but we'll oh why is he gonna slowly fall in there weird it's kind of getting stuck in here it always done that now also we didn't set a value for her bounce on enemy said I wouldn't have to do anything hit play here huh very strange I don't know what's going on here that shouldn't be happy at all Oh cuz I didn't put it within this that's our problem so we didn't make any squiggly brackets after our if statement so we need to put them in there and we'll just pop this over for the sake of tidiness yes that was a strange thing to do so it was I was always trying to give you the bounce value onto the rigid body of the player which isn't isn't really what we want to happen so now when we press play this time we found some type of our enemy oh it wasn't it wasn't a very big bounce there so let's try let's actually make it like 20 let's try that mirror come here at me play jump on your head so now oh that's way too much let's bring it back down to 10 go over here and bang that's just enough of a bounce I think but again you can fiddle with the bounce man see how it feels yourself and there was one little thing that I'd always hear when I killed myself a little weather oh look we're getting there knock back a mil now it does kind of it almost looks like he's jumping out of the respawn point which maybe you might want to keep that in I don't know and personally I'm not I'm not too enamored by it but we don't want him to get knocked back when he comes out of the portal so once he dies which is handled in our level manager script so we'll open in our level manager back in here which I apparently already have open and so in here we're going to on our player we're just going to say - the knock back in amounts are not knock back the knock back count is equal to zero so then it knows okay we're not adding any more knock backs to him so very simple we're just going to go player dot knock back count is equal to zero save that go back into the game and then when we hit play here should be all sorted and all run nicely and smoothly and we have the jump on our enemies and able to get killed there any errors current boom okay here we go so once were killed back alive and there's no not I'm trying to push this around all over the place so there you go that's our basics of jumping on our enemies and killing them and adding a little knock back effect to the player next time is we're going to take a look at adding a weapon to our player a handheld weapon for example a little sword to stab people or something like that and not just another way for us to take care and take out our enemy friends so yes thanks for watching I'll be back soon with more tutorial goodness and everybody stay having fun thanks for watching this episode and if you'd like to see just how some of the concepts we've covered can be brought together into fully fledged game then head to portal NASSCOM and check out the demo from my game that I've been working on for the last while and it uses a lot of the concepts we covered so far in the series as well as other things that we're going to cover in the future so if you want to get a feel for where we're going and what kind of things you can make but these kind of put the basics of unity and head over there try out the game for yourselves and let me know what you think of it of course and the best place to let me know is to head to Twitter at games plus James say hi I'm always there ready to say hello and do stupid things but also if you'd like to see some of the game being worked on and developed in in real time has a twitch TV slash games plus chance where I regularly stream development of the game you can get to see behind the scenes of hell things all work so head there say hello see y'all in the near future
Info
Channel: gamesplusjames
Views: 61,545
Rating: undefined out of 5
Keywords: gaming, let's play, play, games, entertainment, retro, fun, unity, unity3d, how to, unity 2d, unity 2d platformer tutorial, make platformer game, unity platformer design, Unity (Software), Tutorial (Media Genre), Platform Game (Video Game Genre), Free, Unity 5, Unity 5 Platformer, Unity 5 2D Platformer
Id: sdGeGQPPW7E
Channel Id: undefined
Length: 26min 18sec (1578 seconds)
Published: Fri Mar 06 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.