HOW TO MAKE 2D MELEE COMBAT - EASY UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we won and welcome back to black thorn fraud my name is Noah and in today's unity tutorial we will take a look how to create a simple 2d melee combat system by the end of the video your player character will be able to use his weapon the slash to pieces your games various Falls notes that you'll be able to use the techniques showcased in this video to bring to light all kinds of weapons from a long-range Spears to shore swords you'll have the possibility to treat the weapons area of effect rages and shape the amount of damage each attack deals as well as how fast the player can swing his weapon just before starting I wanted to give a big thanks to wisp for supporting me and my contents by a patreon it's just so appreciated and helpful with that said I'll jump straight into unity so as you can see I have a simple scene setup with a Fleur character that for now can move left and right and an enemy that moves left I want to give my a player the possibility to hurt this animated with his swords so I'll make a new c-sharp script called player tank drag and drop it onto my player and open up no I don't want my player spam clicking space to attack or rather you have to wait a few milliseconds between each attack so I'll make a private four variable called time between attack and a public one called start time between attack in my update function I'll make an if statesman's checking whether a time between attack is less or equal to zero and if it is well then I can attack I also make sure to set my timer to attack value back to its default starting value if however time between attack is not less or equal to zero they'll gradually decrease that value using minus equals time Delta time with that done I'll cut out the attack so I'll make another if statement and here you check whether the player hit space obviously you can use whatever key or button you wants for your attack once the player hits space to attack I'm going to check for all the enemies inside a certain radius and deal them damage I start by making a circular shaped area of effect has seen in my video on 2d destructible terrain will use the or lap circle all function so I'll start by making a 2d Collider array called enemies to damage and set it equal to physics 2d dot overlap circle this function will basically cast an invisible circle at a certain position and with a certain radius and all enemies found inside it will be dealt damage that's a plan anyway so for the circles position I'll make a public transform variable called attack pose and for the circles range I'll make a new for variable called attack range in my parentheses I'll then use those two newly created variables for the circles position and range for third parameter off this function we can plug in a layer mask which is very useful I'll allow the circle to ignore certain colliders found inside its radius such as the environment or friend NPCs and of course focus only on colliders with the corresponding layer mask so I'll make a public layer mask variable called what is enemy and place that right there now we can live through all enemies found in the circle and deal each one a certain amount of damage so let's make a for loop double pressing tab to automatically generate the syntax for that loop and run this loop a number of times equal to the amount of enemies found in the circle and now let's deal them damage I'll make a public int variable called damage and then grab the enemy scripts components of my enemy character and reduces health with damage you could of course make a public function called take damage inside of your enemies scripts it takes in an int variable called damage and reduces their health there without forgetting of course to call the take damage function inside of our loop and passing in damage between the parentheses before heading back into unity to make sure that all this is working I'll make a function called on draw gizmos selectors now make sure you type this out exactly as I have done here or the function will never get calls basically unit C will render out in the scene view a cool gizmo of a certain color and shape when we select our player character which is great to help ourselves and visualize the range and shape our players attack but I'll just make the gizmo Raines and since our attack has a circular area of effect I'll make sure to draw a wireframe sphere with the attack post position and the attack range from Regis ok heading back into unity I'll type out a value for my start time between attack I'll just type in zero point three meaning that the player can attack every zero point three seconds I'll then creates a new empty game object called attack pose parented to my player and place it in front of him of course don't forget to drag and drop it inside of the corresponding empty slot in the inspector next up I'll type out the attack range and by doing so you'll see that cool red gizmo in the scene view appear clearly indicating to us the area of effect of a character's attack allow make a new layer called enemy and say my enemy character to that layer of course make sure your enemy has some 2d Collider attached to it all the attacks invisible circle won't detect anything lastly crank up the damage value and now I can hit play and my enemy will take damage except this is far from visible to the player so let me head over to my enemies script and first things first destroy him when he reaches zero health I also make him spawn some cool blood particles whenever he is hit using the instantiates method of course take your time on this try making the attacks impacts the most juicy and satisfying as possible I'll get my screen shaking a little whenever the player attacks for example as well as make him play an attack animation you could also get the enemy playing a little her sound whenever he takes damage I'll drag and drop my particle effects prefab inside of the empty slot in the inspector and hit play and things already feel a lot better the enemy doesn't take any damage when out of my circle when I hit space to a tank and my enemies within reach BAM blood spawns and my enemy takes a hit you'll also notice that my enemy when hit enough is destroyed perfect but still things aren't completely right I feel as if the enemy should get knocked back a little one hits or just pause dazed by the attack so I'll make a private floor variable in my enemies script called days time another public one this time called start this time if days time is less or equal to 0 then the enemy's speed is at its normal value of 5 if not I'll set speed to zero so that the enemy can move and gradually decrease days time and so the enemy takes damage I'll say days time equal to start days time this way the enemy pauses a little with a speed of zero and so back into you to see I'll get the enemy days to probable zero point six seconds and testing results in game things feel a lot better the attack has more of an effect on the enemy who no longer feels like some unstoppable beast of course you could not bag the enemy slightly itself just making pause that's completely up to you now of course you may want your weapons area of effects to be more square or rectangular you can do so by simply changing the overlap circle all the function into the overlap box all function again you'll need to send the box's position and itself a radius define the box's scale for the x and y coordinates I'll remove the attack wages variable and instead make a float variable called attack range X and another unnamed attack range y then type new vector2 in here and plug in those two new variables notice that you can also set the box's angle which can be pretty useful I'll just leave that as zero for this simple demo it's also a good idea to change the gizmo visual into a wire cube instead of a sphere in unity I'll now play around with mat tanks X&Y range and you'll see the gizmo update itself inside the scene view and there we go you're now armed with a couple cool tools to make a fun and melly combat game ax I hope you enjoyed the video and found it helpful you'll be wonderful if you could hit the like and subscribe buttons as well as considered supporting me financially back patreon like these top supporters with that said have a great day thanks so much for watching stay tuned choose [Applause] you [Music] [Applause]
Info
Channel: Blackthornprod
Views: 399,985
Rating: undefined out of 5
Keywords: blackthornprod, blackthorn, btp, noa calice, game dev, unity, how to, make, create, melee, melee combat, tutorial, indie game dev, beginner, C#, c#, programming, enemies, player character, coding, scripting, melee combat system, melee attacking, hitboxes, attack shape, radius, OverlapCircleAll, collisions, attack animation, HOW TO MAKE 2D MELEE COMBAT - EASY UNITY TUTORIAL, art, 2D, 2d, weapon, sword, attack, combat, how to make melee combat
Id: 1QfxdUpVh5I
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Thu Jul 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.