5
\$\begingroup\$

I am trying to implement a super simple day/night cycle. It works fine, at 9pm it gets dark, and at 6am it gets bright.

However, I am "creating" darkness by simply setting the alpha of a black Image that covers the entire screen, ie:

if(NightTime){
    Color tempColor = dayNightImg.color;
    tempColor.a = 0.5f;
    dayNightImg.color = tempColor;
    isDark = true;
}

So, in order to create the Illusion of light, I would have to be able to ignore part of this Dark Image, i.e "Light it up", for example in a circle around my character. Is there some way I can do this? Or Is my approach too simple for this effect?

Here is an example of what I want: enter image description here

\$\endgroup\$
  • \$\begingroup\$ Why not just use... umm... Lights? To make your scene Dark, adjust ambient light and just put a spotlight as a child of your player. \$\endgroup\$ – Aditya Yadav Jun 9 '17 at 12:56
  • \$\begingroup\$ @AdityaYadav Because I did not know how. But I assume u knew everything when u started out :) The link u posted seems a bit outdated, from 2010. Ill look into ambient lights. Remember, it doesnt take a lot of effort to be a nice person :) \$\endgroup\$ – Green_qaue Jun 9 '17 at 13:13
  • \$\begingroup\$ Well, I wasn't trying to be rude nor I claim to know everything even just (still a beginner :) ). The link is outdated but that's what showed up in google search. And you can access Ambient light from Window -> Lightning. Again, sorry if I sounded rude but I was trying to be. \$\endgroup\$ – Aditya Yadav Jun 9 '17 at 13:17
  • \$\begingroup\$ @AdityaYadav If you know how to use it to create the same type of darkness in 2d as a normal black image with opacity would, please post an Asnwer. Cant find any guides on how to use it for 2d \$\endgroup\$ – Green_qaue Jun 9 '17 at 13:26
  • \$\begingroup\$ @Adita Post that as an answer, since it is the most optimal way of creating this effect. \$\endgroup\$ – S. Tarık Çetin Jun 11 '17 at 11:17
5
\$\begingroup\$

There's a quick and dirty way to do this.

Just create, outside Unity, an image, completely black except for a circle in the center.

With Gimp, follow these steps:

  • Create a new image, larger than the maximum resolution you'll use in the game
  • Fill it with black
  • Add alpha channel to the layer
  • Create an elliptic selection in the center
  • Press delete to remove the circle
  • Export the image in png format

You'll have something like this: http://i.imgur.com/YBuV7so.png

Now, back in Unity:

  • Create a new 2D Sprite
  • Set this image as its Sprite
  • Set its Z position in order to put this in front of the camera, before any other sprite of your scene.

Et voilà, now you have a full, black mask with an "always on" center, and you can change the alpha channel of the sprite via script/Inspector in order to modify the opacity of the black mask.

Another way is to use an appropriate Shader, but since you're still a beginner, I would start with this simple solution. ;)

Edit:

Link to a simple example for multiple spotlights with intensity controlled by script: https://github.com/Galandil74/Unity-Spotlights-Example

I prefer to use Directional lights for the spots because of their uniformity, and the fact that their intensity is added to the ambient lighting of the scene. Everything is handled via script, ambient light, spotlights, number of lights, etc.

All sprites affected by the lights must have a Sprites/Diffuse shader, the Sprites/Default shader isn't affected by lights, but only by post-effect shaders.

Sample image with 5 lights: enter image description here

\$\endgroup\$
  • \$\begingroup\$ Thanks, Ive started to look at using simple lightning so I can have more than 1 lightsource. But this answers my question So I will accept it :) \$\endgroup\$ – Green_qaue Jun 9 '17 at 15:07
  • \$\begingroup\$ Exactly: when you start to use lights, it means you're starting to deal with shaders. ;) \$\endgroup\$ – Galandil Jun 9 '17 at 16:33
5
\$\begingroup\$

Extending my comment to an answer...

To achieve this kind of effect, follow the steps

  1. Change the ambient color from Window -> Lightning -> Settings to black.
  2. If you're using sprites then you need to change the materials of Sprites, which you want to be affected by Lights (Sprite-Default is not affected by lighting, I don't know why though), to an empty material
  3. Create a spotlight, and make it a child of player object and play around with it's settings to get your desired effect.
  4. Change the ambient color from code to achieve your day-night effect.

Note : I'm not very experienced with Lights, this is what I achieved. If there's any error or fault here, please do point it out.

Night mode. With ambient color black Day mode. With ambient color white

\$\endgroup\$
  • 1
    \$\begingroup\$ Yup, this is another way to handle with more control spotlights. I'll edit my answer to add a link to a simple scene example with more controls and a script to handle the lights, using Directional ones instead of Spot. \$\endgroup\$ – Galandil Jun 12 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.