Objective: Part 07 of setting up floating combat text in Unity will look at building functionality for a Critical Hit modifier.
Currently the project is only set up for what we call ‘Normal Hits’, where by when the player shoots they will only deal damage between a minimum and maximum amount and there is no variance to accommodate otherwise. When we add modifiers (e.g. critical hits) to a game, we allow the player to deal higher damage, thus killing enemies faster and feeling more in control of chaotic situations. Without this RNG factor, games can get quite boring quite quickly as they become too predictable and repetitive.
I will also be adding more modifiers as I progress through this project.
Step 1 — Coding Logic
Let’s keep this super simple to start with and I will build onto more complex logic later.
Starting in my ‘WeaponShooting’ script which is responsible for the Raycast I shoot (and if I hit an enemy we do XYZ), let’s add a few lines:
First, let’s roll the dice and see how much damage we are going to do, making sure we store this variable otherwise when if call the same method on the next 2 lines we are going to get 2 different results.
Let’s pass this variable into both InstantiateDamagePopUp() and ReceiveDamage() which are responsible for different features.
- InstantiateDamagePopUp() — Decides which pop up to create.
- ReceiveDamage() — Utilises an Interface to pass through this value and damage the enemy.
We are only interested in the initial method for this feature which we can add some logic to:
Let’s simply check to see if the passed in ‘damage’ parameter value is greater than 70 and if it is, we are going to instantiate a ‘Critical Hit Prefab’ otherwise instantiate a ‘Normal Hit Prefab’.
Step 2 — Creating a Critical Hit Prefab
Instead of using the 1 prefab where we have to continually keep updating the values, size, scale, color of the text (depending on a critical hit or not) let’s create a second prefab that is responsible for the Critical Hits.
I dragged the existing prefab from the Inspector into the Hierarchy and duplicated it, so that I can then drag this back into the Prefabs folder and create an ‘Original Variant’ as opposed to duplicating it within the Prefabs folder which can cause issues.
Let’s now adjust the font settings, color, and the height/scale Animation Curves below.
Step 3— Adjusting the Font Settings & Color of the Text
Now we want to start the process of making the text visually stand out to let the player know they have made a Critical Hit and the first thing we can do is change the color of the text.
By clicking on the Text GameObject under your Critical Combat Text Prefab, let’s make the adjustments in the Inspector:
I made a change to the:
- Font Asset
- Font Style
- Font Size
- Vertex Color
Step 4— Increasing the Scale of the Text
To give this text a bit more of a POP effect, let’s make some adjustments to the Scaling Animation Curve:
Here I have increased the Value on the Y-Axis to 2.5 (from 1.75) to make the scale grow large and at the same time I have increased the Time on the X-Axis to 1.5 (from 1.0) allowing the Critical Hit to linger for longer on the screen.
Let’s take a look at the changes!
There is also an opportunity here to adjust the Height Animation Curve as well if you wanted to text to be instantiating a little higher.