[Unity 2D/3D Tutorial] Quick, Basic Camera Fade In and Fade Out
Hello. Today I have a quick tutorial for you on a very basic camera fader that can be used to fade scenes in and out. You can set any fade colour – let’s say you’re a fan of deep purple. You can also set the fade speed to give you something slow and gradual, or something quick and snappy. Finally, you can set the fade curve to any curve you want to give you any combination of fading in out, for example a slight flickering effect. The script is available on GitHub with a few of my other scripts.
Scripts:
*----*----* My Playlists *----*----*
Unity Journey Of Discovery (Game Dev and Tutorials):
The Main Course (Maths and Science Concepts Explained Visually:
Eco-Pool Natural Swimming Pool (Build and Progress):
4K Treats (Ultra HD content):
Finger Snacks (Shorter, Unclassified Videos):
*----*----*----*----*----*----*----*
The basic idea is that we create a one-pixel texture, give it a color, set its transparency, and then drag it across the entire screen in front of everything else, updating the transparency over time.
You’ll usually want to call the fading from somewhere in your game, but for demonstration purposes I’m calling it when a specific key is pressed. This, as well as the following public variables, can be set in the editor, as we’ll see later. Next, we select the fading speed and color. We create a default fading curve to allow us to customize the fading sequence, and finally set whether we want the scene to start faded out already.
As our private variables we have a float alpha controlling the current transparency, the texture we want to display over the scene, an integer for the direction of the fade, namely 1 for fading in and -1 for fading out, and finally the time progressed through the fade.
In our start method we set alpha according to whether we want to start faded out or not, and then create a 2D texture consisting of a single pixel. We set the color of that pixel to the supplied fade color, with the current alpha as opacity or transparency. We have to apply this change to the texture.
In our update method we initialize the fading out or fading in only if a fade is not currently in progress, and if the specified key was pressed in that frame. If the scene is faded out completely, we cap alpha at 1 and set time and direction so it will start decreasing alpha. If the scene is faded in completely, we floor alpha at 0 and set the time and direction such that it will start increasing alpha.
All that remains is to actually draw the texture, which we only do if it isn’t completely transparent, and then we update our variable values if the direction is not equal to zero, so if we’re currently in a fading phase. We do this in the OnGUI method, which renders and handles graphical user interface events.
We add deltaTime to our time variable, but we scale it with the specified speed and multiply it with direction to let it read from the fade curve in the correct direction. Then we set alpha to the point on the fade curve for the current time value, and we adjust the color of the one-pixel texture by changing the alpha. Again, we need to apply the change to the texture. Finally, we stop the fading whenever the scene is either fully faded in or fully faded out.
This script is then attached to our camera, and we can run our scene with the default input values. Every time we pressed the space bar, the scene fades in our out, depending on its current state. You can now play around with these input values to give you different effects. And there you have it – a very basic camera fader. As I’ve said at the start, the script is available in GitHub, with the link in the description. Go forth and fade!
1 view
65
15
2 hours ago 00:06:37 1
TOP 20 Visual Assets to INSPIRE YOU! (MAY 2024)
8 hours ago 00:10:36 5
Создание игр на Unity 2024 | Урок 1 - Установка
3 days ago 00:37:28 1
CUBES ONLY | 3D LOWPOLY Cube Worlds | The Final Boss | Blender Speed Modeling | Environment Design
3 days ago 00:00:35 1
Made a prototype for an AR Paper-plane game. Would you play something like this? #ar #arfoundation
4 days ago 00:05:45 2
Improve your AR development with the AR Magic Bar (Unity AR foundation & Lightship asset)
1 week ago 00:35:05 8
3D LOWPOLY Cube Worlds | Sheep Pasture Hut | Blender Speed Modeling | 3d Environment Design
1 week ago 00:15:27 1
How to build AR Apps fast (Unity + Lightship Samples)
1 week ago 15:33:59 1
Unity - Лучший курс | С Нуля до Junior за ОДНО видео
2 weeks ago 00:32:24 1
MagicaVoxel с нуля. Туториал для новичков by Artalasky
2 weeks ago 00:12:58 1
Как сделать 2D платформер на Unity | #1
2 weeks ago 00:08:41 1
Procedural Walking Animation in Minecraft
3 weeks ago 00:02:54 5
INTERGALAXY II | STELL STUDIO
3 weeks ago 00:08:56 1
Как сделать 2D платформер на Unity | #8
4 weeks ago 00:16:20 1
ADOBE ТЕБЕ НЕ НУЖЕН И ВОТ ПОЧЕМУ
4 weeks ago 00:48:45 1
Как сделать ЛЮБУЮ СТРЕЛЬБУ в UNITY? На примере РЕАЛЬНЫХ игр
4 weeks ago 00:07:04 6
UI & Background BLUR in Unity URP (FREE 2022 Tutorial)
4 weeks ago 00:07:09 1
Как сделать 2D платформер на Unity | #4
1 month ago 00:12:03 1
How I created an AR Strategy Game prototype (Unity + Lightship)
1 month ago 00:09:54 1
Сделал ИГРУ за 69 дней и выложил на Яндекс.Игры!
1 month ago 00:31:46 1
Unity 3D | Creating A Genshin Impact Style Anime Environment (Not Genshin Impact)
1 month ago 00:08:27 1
How to Make A Simple Tooltip in Unity Tutorial
1 month ago 00:35:16 1
12 часов в день ради собственной игры [Devlog]
1 month ago 00:02:02 1
Как сделать полоску здоровья в Unity | Health bar in unity | Tutorial
2 months ago 00:01:03 4
Gennady - новая игра, хардкорный 2D-платформер студии FEED 64