Throughout week 3 and the beginning of week 4, my time was spent finalising the Procedural Dungeon Generation and creating enemies to populate the depression realm. During this time our mechanics designer implemented an 8-directional attack, but it was up to me to implement health and damage to the player.

Player Health Script

This is a simple starter health script for the player, starting with public floats as numbers that correlate to the player’s max and current health count. With its current health being set to its max health in the Start() method.
I wrote the TakeDamage(int damage) method to take in an int that will correlate to how much damage the player will take [shown as currentHealth -= damage which decreases the players current health by the int amount taken in by the TakeDamage method]. I did this as in the future there may be certain enemies or obstacles that deal more than 1 damage to the player, This is a much more robust method than the TakeDamage method having no input and dealing 1 damage to the player every time the method is called.
Making the player deal damage
In order to make the player deal damage, I created a similar TakeDamage() method in the enemy scripts, this method will exist in all enemies.
The roomManager.EnemyKilled(gameObject) part of the script is explained in the Dungeon section of this website.


Now that the enemies were able to take damage via the TakeDamage() method, I had to implement a way for that method to be called. This was done via the Weapon script I wrote above, this script would detect if the player’s melee’s BoxCollider2D component entered the enemies’ BoxCollider2D component (It check this by checking the other collider’s tag, all enemies in the game are tagged with “Enemy”). If the melee’s trigger detected an enemy, it would then call the TakeDamage() method on that specific enemy’s script. As the damage int was set to 1 (public float damage = 1; Unity is able to convert floats without decimals to ints automatically), the enemy hit would then take 1 point of damage.
Player Dash
Once our Mechancis Designer, Ayse, had programmed a dash into the player movement, I needed to alter it slightly so that it would prevent the player from taking damage during this dash.
To do this, I changed the player’s PlayerHealth script and added an if statement to the TakeDamage() method.
What this did was make the TakeDamage() method check if the player was dashing during a possible instance of damage, if the player was dashing, then no HP is deducted from the players currentHealth int.

This video shows that in the console, the player’s health decreases while colliding with the enemy’s projectile. However, if it collides with it while dashing, the player’s health doesn’t decrease.