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
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): public class LightToggler : MonoBehaviour { // making these public will show them in the inspector (set the values there) public float delay; // amount of time before toggling public float minIntensity; // the minimum intensity of the light public float maxIntensity; // the maximum intensity of the light public bool startAtMin; // variable to hold a reference to the Light component on this gameObject private Light myLight; // variable to hold the amount of time that has passed private float timeElapsed; // this function is called once by Unity the moment the game starts private void Awake() { // get a reference to the Light component myLight = GetComponent<Light>(); // if the GetComponent was successful, the variable will no longer be empty (null) if(myLight != null) { // if startAtMin is true, set intensity to the min to start, otherwise set to max myLight.intensity = startAtMin ? minIntensity : maxIntensity; } } // this function is called every frame by Unity private void Update() { // if we have a reference to the Light component if(myLight != null) { // add the amount of time that has passed since last frame timeElapsed += Time.deltaTime; // if the amount of time passed is greater than or equal to the delay if(timeElapsed >= delay) { // reset the time elapsed timeElapsed = 0; // toggle the light ToggleLight(); } } } // function to toggle between two intensities public void ToggleLight() { // if the variable is not empty if(myLight != null) { // if the intensity is currently the minimum, switch to max if(myLight.intensity == minIntensity) { myLight.intensity = maxIntensity; } // if the intensity is currently the max, switch to min else if(myLight.intensity == maxIntensity) { myLight.intensity = minIntensity; } } } }
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.
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
This will work in Unity 2D right? I tried setting my player sprite to Diffuse and it disappeared I was looking to create something that would illuminate the room like one of these If you see what I mean?
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.
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... Can't find New Material.
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.
Hmm... I haven't done anything with lights before so what do you mean? I appreciate your help by the way thanks.
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 I'll play around wityh a few settings and see what happens
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?
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.
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.
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.
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): public class Rotator : MonoBehaviour { // making variables public will show them in the inspector public float speed; // speed of rotation // this function is called every frame by Unity private void Update() { // rotate this object about the Y axis by speed (multiplied by deltaTime to account for frame-rate changes transform.Rotate(Vector3.up, speed * Time.deltaTime, Space.Self); // try changing Vector3.up to Vector3.right or Vector3.forward // if you prefer a different rotation axis // also try changing Space.Self to Space.World // Self rotates around the object's axis (however it's currently oriented) // World rotates around the world's axis (never changes orientation) } } The lighting effect in the video you linked is a much more complicated effect that I'm afraid I can't help you with.
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; }
@MarkMorris @mysubcoder You guys realize this was over 4 years ago, right? Pretty sure he solved it by now...
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.