Creating Floating Combat Text For My FPS in Unity — Part 02
Objective: Part 02 of setting up floating combat text in Unity which looks at bringing randomness to the instantiation position and changing the color of the text.
Giving the Instantiation Position More Randomness
Back in our ‘FloatingCombatTextPopUp’ script, we have been instantiating it at the exact same location directly by passing into the parameter — Vector3.zero or, (0, 0, 0). This is where we need to provide an alternative parameter.
I am going to create a utility method that creates and returns a random Vector3 to us so we can replace Vector3.zero.
Now, we don’t want the depth (Z-Axis) to be affected because we only want to move the text slightly to the left/right (X-Axis) and up/down (Y-Axis) of the GameObject itself.
We also need to make sure the _min and _max values are floats otherwise we will be stuck with rigid positions again (e.g. -1, -1, 1, 0 or 1,1), extremely limiting the potential positions.
Now that we have our randomness, let’s look at changing the color.
Changing the Color of the Text
We have 2 options here, we can either set the color in the ‘Damage Text’ GameObject through the Inspector, or we can set this through code.
Traditionally in video games, yellow, white and orange seem to be the most dominant color choices as they provide the best contrast on the screen and visually it is much easier to read, especially if the numbers are moving very quickly.
Inspector:
Code:
Let’s create a new script ‘FloatingCombatTextColor’ and add it to the prefab:
This is a super simple approach that is just showing you how to change the color of the text to blue after 3 seconds:
If you wanted to be a little funky with your color selection, we could also pass in a random color range: