Hello Unity fans. Today we are going to implement a way to light and extinguish fires based on when specific events occur. We will also add a flickering effect to the light. We won’t be focusing on the game objects or models themselves, but on the code required to enable the light with a fade in effect, enable particle system emissions at the correct time and eventually fade out the light and disable the particle systems emission again. All of this will be controlled by specified events, with all the fires reacting with a random delay when one of the events are triggered. Or in this case I guess we can say when the event fires. There’s a link to the scripts in the description of this video. Let’s get going.
Scripts: https://github.com/ReCogMission/First...
Camp fire by Jalen Jacobson (CC Attribution): https://sketchfab.com/3d-models/camp-...
Code Monkey: • Trigger an Action after some Time (Un...
---*----* My Playlists *----*---
Unity Journey Of Discovery (Tutorials): • Unity Journey of Discovery: Hello Wor...
The Main Course (Maths and Science Concepts Explained Visually: • Sun, Earth and Moon: Introduction and...
Eco-Pool Natural Swimming Pool (Build and Progress): • Green/Eco/Natural Pool, Part 1: Plann...
4K Treats (Ultra HD content): • Marine Life Photos Part 1 4K UHD
Finger Snacks (Shorter, Unclassified Videos): • Ribbon Eel (Rhinomuraena Quaesita) Sw...
*----*----*----*----*----*----*----*
In a big scene we may have many fires, torches or other light sources that may have to be lit and extinguished at specific times. We can rather have one main time tracker script that keeps track of the in-game time. That script can then fire off events at specified times. Each fire or light can subscribe to these events, then just wait for the event to fire, upon which it runs the required method.
In order to implement this, we start off with a static class that keeps track of time and implements various possible events, which can be subscribed to from different scripts in our project. We are going to define only two static events in this example, namely when the fires or lights should be lit, and when they should be put out.
Next, we have some private working variables...
And that’s all we need to keep track of the time of day, and fire off our two events at the appropriate times. Now we need some subscribers to the events. We’re going to use a campfire model by Jalen Jacobson which you can download under Creative Commons with Attribution from Sketchfab. Added to this will be a particle system creating our flames, which I’m unfortunately not allowed to share. Finally, we have a normal point light game object to provide the flickering light.
...
However, each fire needs to be aligned to its light, otherwise the light may appear before the flames, or vice versa. So, for the fire, we will add the option of having its event call delegated to its associated light, which we’ll get to in a moment.
So, let’s add a My Fire Light script to the point light of our camp fire. In addition to subscribing to the event and possibly calling the fire’s events as well, we want to add a flickering to the light. The parameters it takes are firstly the speed at which to scroll through the random Perlin noise generator for position, and for speed. Then, the base intensity for the light, followed by the scale to apply to the noise for the position and for the intensity. Finally, we have the fade in and fade out time as well as the ranges of a random delay in seconds.
...
Finally, we want to add a flickering effect to our fire’s light. To enable us to do this, we firstly sample from a Perlin noise for the x, y and z coordinates. There are many possible ways to accomplish this, but I’ve just multiplied the accumulated game time with the position scroll speed, then added constants so that the samples are taken at slightly different locations. We subtract a half to centre the samples around 0, then return this delta position after scaling it as required.
Similarly, we create a new intensity by multiplying the Perlin noise sample by the intensity scale and adding the base intensity onto it.
...
And now our fires are starting up with random delays, but aligned to their lights, whenever their events are triggered. We could now add these as standalone objects to our scene, or combine them into other, larger objects, and they will all adhere to the same principles, all regulated by the one time tick system.