Event Listeners
4_ScriptableEvent
Last updated
4_ScriptableEvent
Last updated
Health.cs is where those events are fired, but let’s find out who is listening to these events. Let’s check the prefab_player and examine the DamageVFX.
When the player is damaged, this event is fired and it will trigger the particle system dynamic method “Emit” that takes an int as a parameter. With this simple trick, we can visually show a relation between the amount of damage and the intensity of the VFX without code. A damage of 10 will emit 10 particles, and a damage of 50 , maybe a critical hit, will emit 50 particles.
In a bigger game, you would probably have a different VFX for different amounts of damage, but in Hyper Casual games, you can get away with this. The Heal VFX in the prefab_player does the opposite but works the same way.
You may have noticed that when the player gets damaged, there is a red vignette effect appearing then fading out.
UICanvas/Damage Vignette
As we can see, here we listen to the OnPlayerDamaged event. When it is fired, we activate the Fade out.
Note : we ignore the int parameter as we don’t need it and call a regular method instead of a dynamic method through the unity event.
The example_OnPlayerDeath event is listed by the Game Over screen.
You will notice that the Game Over Screen is visible in the editor and gets disabled when the game starts. Indeed, it needs to be enabled to register to the event at the beginning of the game.
This is what the boolean variable called “Disable after subscribing” is for. The purpose of this variable is to disable the GameObject after subscribing to the event in Awake().
You will notice also that we added a small delay before invoking the response. This is useful to add small delays without needing to write code to do it.