Go toFill Areaand drag the bar until it fits into the background. In my case, my ending values arePos X: 75,Width: 160, and the rest is left to its default value.

Here’s what it looks like now:

However now we have another problem, if we were to set the value of the slider to 1, here’s what we would get:

Notice how our fill bar is on the right side of the actual bar itself.

Unfortunately, there doesn’t seem to be an answer that resolved the problem for me. This might be more of a Unity problem than anything else.

However, that doesn’t mean we’ll just give up.

We can simply fix it the same way you might never have noticed it in the Survival Shooter tutorial: we’ll expand the background of the slider so that the bar would naturally look like it’s correct.

To do that, we go toBackgroundand then changeLeft,Top, Right, and Bottomto -3

After that we should have something like this:

Much better, right? Nothing looks out of place!

Now that we have the health bar looking nicer, it’s time to move it to the bottom left corner. SelectingHealth Bar, go to theRect Transformcomponent, click on the square, to open up the UI alignments and hitShift+Ctrland left click thebottom leftoption to move everything to the bottom left corner.

Right now, everything is exactly at the bottom left corner and doesn’t look nice. Let’s add some adding.

ClickHealth Barand setPox Xto be15andPos Yto be10.

When you’re done, your game should look like this now (don’t forget to set your slider value to be 100)!

Creating our Player Health System

Creating the Player’s health

Now that we have an UI health bar, it’s time to write some code to create the player’s health.

First, click on ourPlayergame object and create a new script calledPlayerHealth.This script will control the player health whenever they get damaged and then show these changes in our health bar.

We create a public Slider that will be ourHealthBar. It’s important to note that we need to importUIotherwise the compiler would complain to us about ourSliderobject.

Next, we create a public Health to represent our max health and _currentHealth to represent how much health our player has.

In Start() we instantiate our_currentHealthto be our max health.

Finally, we create publicTakeDamage(), public meaning that another script can use this component and call the function. Inside this function, we get our damage amount and we update our_currentHealthand we change the value of our slider to reflect health loss.

Before we proceed on, make sure to drag ourHealthBargame object to ourPlayerHealthscript component.

Like so:

Writing the Damage Dealing Code

Now that we have that setup, the next thing we need to do is create the code that callsTakeDamage().

Luckily for us, we already have a script that deals with most of this:EnemyAttack.

All we need to do is grab an instance of our newPlayerHealthscript and then run the take damage code.

In our script, we already have access to the player and detection for when we attack, so all we need to do is grab ourPlayerHealthscript and trigger our TakeDamage function when we callAttack()inEnemyAttack.

As you might recall from previous tutorials, the way that this code works is that:

We have a trigger collider that detects when our knight should start attacking

Then we have a mesh collider on our player that will detect when we’re touching the player

Finally, in our animation, we set a point in time to callAttack()and when that happens, if the knight is still in contact with our player, we’ll take damage.

With this, we have everything we need to have a complete game, or you might think

If you were to play the game right now, you would have encountered a new problem that hasn’t been realized until now:

If we were to play the game while looking at our console. Whenever we bump into the knight, we would hitOnColliderEnter()get hit, and then for some reason runOnColliderExit()even if we don’t move the player.

As a result, if the player never moves, they’ll only get damaged once. How’s that for being boring?

We’ll solve this tomorrow, because it’s getting late today.

Conclusion

Today in day 19, we created a health system that allows our player to receive damage from our already existing enemy attack code.

We’re almost done, but we’ve encountered a problem where the enemy stops damaging us even when it looks like they successfully attacked us.

I have an idea of what the problem is, but we’ll talk more about that tomorrow!