Search Unity

Flashing lights...

Discussion in '2D' started by AEBlob, Jan 25, 2016.

  1. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Was looking to add an orange flashing light of some sort, as you get around industrial areas, but I have no idea how to do this. Can anyone help, or point me in the direction of a good lighting tutorial?

    Thanks :rolleyes:
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You'll want to make a game object with a light component. Then use a script to drive the behavior you're looking for.

    I wrote up this script with comments to get you started. This script will toggle a light between a min and max intensity after a delay. This script must be on a gameObject with a Light component to work.

    Code (CSharp):
    1. public class LightToggler : MonoBehaviour
    2. {
    3.     // making these public will show them in the inspector (set the values there)
    4.     public float delay; // amount of time before toggling
    5.     public float minIntensity; // the minimum intensity of the light
    6.     public float maxIntensity; // the maximum intensity of the light
    7.     public bool startAtMin;
    8.  
    9.     // variable to hold a reference to the Light component on this gameObject
    10.     private Light myLight;
    11.  
    12.     // variable to hold the amount of time that has passed
    13.     private float timeElapsed;
    14.  
    15.     // this function is called once by Unity the moment the game starts
    16.     private void Awake()
    17.     {
    18.         // get a reference to the Light component
    19.         myLight = GetComponent<Light>();
    20.  
    21.         // if the GetComponent was successful, the variable will no longer be empty (null)
    22.         if(myLight != null)
    23.         {
    24.             // if startAtMin is true, set intensity to the min to start, otherwise set to max
    25.             myLight.intensity = startAtMin ? minIntensity : maxIntensity;
    26.         }
    27.     }
    28.  
    29.     // this function is called every frame by Unity
    30.     private void Update()
    31.     {
    32.         // if we have a reference to the Light component
    33.         if(myLight != null)
    34.         {
    35.             // add the amount of time that has passed since last frame
    36.             timeElapsed += Time.deltaTime;
    37.  
    38.             // if the amount of time passed is greater than or equal to the delay
    39.             if(timeElapsed >= delay)
    40.             {
    41.                 // reset the time elapsed
    42.                 timeElapsed = 0;
    43.                 // toggle the light
    44.                 ToggleLight();
    45.             }
    46.         }
    47.     }
    48.  
    49.     // function to toggle between two intensities
    50.     public void ToggleLight()
    51.     {
    52.         // if the variable is not empty
    53.         if(myLight != null)
    54.         {
    55.             // if the intensity is currently the minimum, switch to max
    56.             if(myLight.intensity == minIntensity)
    57.             {
    58.                 myLight.intensity = maxIntensity;
    59.             }
    60.             // if the intensity is currently the max, switch to min
    61.             else if(myLight.intensity == maxIntensity)
    62.             {
    63.                 myLight.intensity = minIntensity;
    64.             }
    65.         }
    66.     }
    67. }
     
    Last edited: Jan 25, 2016
    Ongjming, wshigham and AEBlob like this.
  3. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Hey great, thanks Jeffory, I'll give it a try. :)
     
  4. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I think I'm missing something, I played around with various settings but I can't see hide nor hair of any light, only the gizmo.
    upload_2016-1-25_20-50-33.png
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Make sure that the light is near the object you're trying to be lit, but not directly ontop of it. If the point light is at Z = 0, and the sprite is at Z = 0, you won't see the sprite receiving light.

    Also make sure that the Sprite is using a Diffuse material. Default sprite material I believe doesn't receive light.

    You can create a Diffuse Sprite material easily if you're not using one. In your assets right click, create a new material, and in the inspector assign it the shader Sprite -> Diffuse. Then drag that material onto your sprite's component list, and see if it will receive light that way.

    If you're just after the 'glow' effect, try using the "Draw Halo" toggle. If you want light bloom, you'll need an Image Effect on your camera.

    Also make sure you set a Max value and Delay in the inspector, in your screenshot they are set to 0
     
    Last edited: Jan 25, 2016
  6. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    This will work in Unity 2D right? I tried setting my player sprite to Diffuse and it disappeared o_O I was looking to create something that would illuminate the room like one of these upload_2016-1-25_22-16-34.jpeg

    If you see what I mean? :)
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    It should not disappear, if anything it should turn very dark if it's not being lit by something. Make sure the tint isn't set to alpha 0 or something. Make sure you're using the Sprite Diffuse, and not something different.



    You should be able to achieve that effect using a rotating Spot Light.
     
    AEBlob likes this.
  8. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I seem to be missing something... both literally and figuratively. I would want to do this with all the objects in the room that I want illuminated? Also I don't seem to have the same menu options...
    upload_2016-1-27_21-15-3.png

    Can't find New Material. o_O
     
  9. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    When I double click on upload_2016-1-27_21-18-58.png I'm unable to change anything

    upload_2016-1-27_21-18-33.png
     
  10. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can't edit properties on the default material, but you can create your own new material.

    In your assets panel, right click and go to Create -> Material.

    You can alter your new material, and then apply it to your sprite by dragging the material into the component list in the inspector.

    Any sprite that you want to receive light must have a material that can receive light. The Sprite/Diffuse material can receive lighting. Also know that SpriteRenderers cannot cast shadows.
     
    Last edited: Jan 27, 2016
  11. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    OK so now I got him... turning dark ;) getting closer.
     
  12. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Yep, just make sure that you have lights in your scene or everything will be dark!
     
    AEBlob likes this.
  13. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Hmm... I haven't done anything with lights before so what do you mean? :)

    I appreciate your help by the way thanks.
     
  14. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    OK I think I'm getting there, the spot light seems to work, although it does change the characters color somewhat, but I'm assuming if I play with the color of the light I can mitigate that? Also the flashes are a bit intense I don't suffer from epilepsy, but I might after looking at this for a while :eek: I'll play around wityh a few settings and see what happens
     
  15. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I'm curious, what does the Area (baked only) type do, doesn't seem to do anything for me? Also is it possible to get shadows?
     
  16. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    No problemo, happy to help.

    When you apply that new material to the sprite, it will turn dark because it now requires lighting. If all your sprites have that material, your scene will be full of dark sprites unless you add lights to brighten everything up.

    Usually when a 2D game has lots of sprites that receive light, there will be many lights in the scene. Sometimes they will use a directional light to simulate sunlight, point and spot lights to represent lamps and other light sources. So have fun and populate your scene with lights to achieve the visual that you're looking for.

    Check out the lighting overview in the docs here: http://docs.unity3d.com/Manual/Lighting.html

    Area lights (baked only) means that the engine will pre-process the lighting, and permanently apply it to the sprites. This means that if the scene changes while the game is running, the lighting from that light will not update. Area lights are useful for backgrounds and objects which are always lit the same way.

    The color of the sprites should be their normal colors if you use a white colored light. They will be darker or lighter though depending on how intense the light is. If you want an orange light like that construction light then try a slightly orange tint on the light color.

    So play around with the intensity, range, and position of the light to find a good level of brightness for your scene.

    SpriteRenderers cannot cast shadows. One way to achieve 2D objects that cast shadows would be to use Quads (a 3D primitive shape) with a material set to Cutout.
     
    Last edited: Jan 27, 2016
  17. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    OK I'll try and set the scene I was aiming for, I'm making a platformer where the character goes through a series of rooms, and one of the rooms he enters is an industrial area with orange rotating lights. I set all the objects in said room with the right material (again thanks for that you explained it well and made it easy) and added multiple lights, and it looks pretty good now.

    However, they're more like flashing spotlights, and I was looking to make a light that will rotate around an animated light (like the one I posted a pic of before) which will be attached to the wall, so more like a spotlight that shines up/along the wall circling the central pivot of the light animation, if that makes sense.

    Think I'm making it complicated, but when I get an idea I like to continue until I achieve it. :)

    Also, and perhaps more importantly, I notice now when my character moves out of this room to one of the other ones he is very dark again, I assume I'll have to somehow turn the diffuse material on/off when he enters and leaves the room.
     
  18. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I suppose something a little like this in a way, not sure how he did it, unfortunately he doesn't explain his process. Obviously not exactly like it as my light will be static, but the way the light shines across the surface rather than onto it, and the way it interacts with the objects around it.

     
  19. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well for your player, you can create a light that you attach to your player (child object), so the light could follow him around or be turned on and off at the correct times.

    For the rotating light, instead of the script i gave you before, try something like this:

    Code (CSharp):
    1. public class Rotator : MonoBehaviour {
    2.     // making variables public will show them in the inspector
    3.     public float speed; // speed of rotation
    4.  
    5.     // this function is called every frame by Unity
    6.     private void Update() {
    7.         // rotate this object about the Y axis by speed (multiplied by deltaTime to account for frame-rate changes
    8.         transform.Rotate(Vector3.up, speed * Time.deltaTime, Space.Self);
    9.  
    10.         // try changing Vector3.up to Vector3.right or Vector3.forward
    11.         // if you prefer a different rotation axis
    12.  
    13.         // also try changing Space.Self to Space.World
    14.         // Self rotates around the object's axis (however it's currently oriented)
    15.         // World rotates around the world's axis (never changes orientation)
    16.     }
    17. }
    The lighting effect in the video you linked is a much more complicated effect that I'm afraid I can't help you with.
     
    Last edited: Jan 27, 2016
    Ongjming and AEBlob like this.
  20. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    OK great thanks. I'll give it a try tomorrow and let you know how I go. Thanks again Jeffery.
     
  21. markmozza

    markmozza

    Joined:
    Oct 16, 2015
    Posts:
    86
    Light Switch for Multiple Lights.
     
  22. mysubcoder

    mysubcoder

    Joined:
    Jul 21, 2020
    Posts:
    1
    It works fine. Just use approximate comparison instead of equality to match fractional values.
    if (Mathf.Approximately(myLight.intensity, minIntensity))
    {
    myLight.intensity = maxIntensity;
    }
    // if the intensity is currently the max, switch to min
    else if (Mathf.Approximately(myLight.intensity, maxIntensity))
    {
    myLight.intensity = minIntensity;
    }

     
    Ongjming likes this.
  23. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    @MarkMorris @mysubcoder
    You guys realize this was over 4 years ago, right? Pretty sure he solved it by now...
     
  24. Ongjming

    Ongjming

    Joined:
    Jan 16, 2021
    Posts:
    2
    Thanks, this helped a lot. This came up when I searched, had similar question as OP and glad I found the responses and updated changes after a few years.
     
    thegrosvenor likes this.