Programming and Designing Melee Combat

Introduction:

In this next segment of the blog, I will go into detail about the combat system that exists within the game. In the last segment of the blog, I spoke about designing the main character and creating a 2D Player controller that functions correctly, the next logical step leaned towards sorting out a logical system in which the player can take and receive damage between non played enemies in the game, serving as the foundation to work on to slowly adding in more features in the future.    

Designing the required SpriteSheets: 

Before I could start anything involving code, I first needed at least one enemy sprite to test with, and I also needed some attack animations for the player to add to the animator and feed in some more parameters to get that added in with the player controller. So that's what I did.   




To make testing a bit quicker, I made a character set on the same one as the main character and changed the colors and hair, you could call it a reskin. However for testing purposes, it works fine and I can come back one thing's are established and functional and build upon the player models into other people and characters with different weapons and styles, etc. 


Setting up Player & Enemy Health and Damage Logic

Now that I have the appropriate sprite sheets, all was left was to cut them up into individual frames and feed them into the animator for each animation required, such as Idle, walk, an animation that indicates you've been hurt or have hurt, etc. However, this alone doesn't give me a result, just some animated pixel guy who doesn't do anything, so next, I needed to add some logical instructions to make him actually do something, like run and the player and hit him in the face with his weapon. So the first script I set up was an 'Attack' script that would store all of the player's data for attacks, so in essence, giving the enemy and the player a health amount, and a damage amount, that can take away from our health amount under a very specific condition. In this case, that condition needs to be called when the player does an attack.  


As a whole, this script is pretty simple, the max health int (100 value) to current health, so in the inspector, it will update in real-time the player's current health value. If the player is hit by an enemy, take away their damage int, from the player's current health. So in this case, if the player is hit, 20 damage from the enemy's attack script will be called and taken away from the player's current health value to where it's been called, (in this case when the enemy attack function collides with the player but we will get into that shortly) if the player's current health gets below 0 from 100, the die function will be called, turning off all attached scripts making player input unresponsive, locking the player from doing anything putting them into a game overstate, in which I am still to decide how that would work, for now, though I can just reset the same in the editor, the player, however, will not have this pleasure.


The enemy health script is fairly similar in a structure just flipped + & - ints on the damage and health functions so the enemy will do damage to the player only and not other enemies if there are more than one during combat. As well as this I have written a function that will flip the enemy on the X-axis 180 or -180 depending on if I want the enemy to run to the player from the front or behind, and I also included a line that transforms the enemy back 5 on the X-axis when they are hit creating some room between the player and enemy if they are swarmed as an example, it provides extra maneuverability in tight enemy situations. As well as this I also did a rehash of the player animation cycle and set up all the parameters within the functions that were needed animating the player. Finally, all that was left was to set up box colliders and set them as triggers for the functions and also, set up some sort of function that would allow the enemy to run to the player when in a certain distance of the enemy, this also gives the enemy a set target to focus on, much like how the enemy in streets of rage is consistently lock ongoing after the player. 


The green box inside the player acts as our hitbox, enemies also have their own hitboxes, if the enemy's attack point collides with the player's hitbox during their attack, it will take away some health. pretty cool :). The smaller white circle in front of the player is the player's attack point, it works the exact same as the enemy's, except it will take away from enemies only rather than anyone or the player themselves, the attack point will also flip axis corresponding with the player's input and direction, so the player can attack from the front of left.

Setting up Enemy targets and states

Now finally all that's left to do is finalize in some last animations and set some parameters for when they're called, such as a hurt animation, a simple flash of red over the target you hit, and have that animation called during the TakeDamage function, then return back to the attack or idle animation. While in the animator, I also set up a script on the attack animation, that in short, feeds in the player's current transform position, and sets the enemy's current transform position to travel at a set speed towards the player, when in a set range, plays the enemy attack animation and calls their attack function at the same time.    


Now finally all logic is set up on both the player and the enemy player, therefore I drew up some other characters and used the same process to make more enemies for the player to fight and put them all in the same scene to fight it out!


In the next segment of the blog, I will go into the making of UI elements such as dialogue boxes and a health bar inputting some data from the health script into a slider for a healthbar effect.

Comments