- Home /
Police flashing light script
Hey guys,
I have found scripts that allow you to make lights blink/flash in time etc... But I was wondering, is there a good way to make Police lights (the ones that would be on top of the car) or would i just have to use lots of different scripts on each light and change the flashing order manually
Thanks
-Grady
Answer by syclamoth · Sep 28, 2011 at 11:58 PM
You can assign a cubemap light cookie which is half black and half white around the central axis- this way you can get the sweeping lamp effect happening! Unfortunately it's a bit difficult to get the lamp itself to 'flash' nicely, but if you have a 'flare' object attached to the light, it is possible to fade it in and out using Mathf.Sin and some constants. (Sin is actually more appropriate than it usually is, here- see testure's comment about rotating lamps!)
Actually, the simplest cubemap would be totally black on two sides, and then totally white on the other two, and then for the top and bottom, just two intersecting diagonal lines and a checkerboard-pattern!
For the flashing, I would have a lens flare Object- then put a script like this
public LensFlare flare; public float rotSpeed; public float rotOffset; public float maxIntensity;
void Update() { float newIntensity = Mathf.Sin((Time.time rotSpeed) + (rotOffset Mathf.Deg2Rad)) * maxIntensity; flare.brightness = Mathf.Clamp(newIntensity, 0, maxIntensity); }
Given the correct bits and bobs assigned in the editor, this should allow you to make a nice-looking police flare!
This script will give you everything you need to change the timing of each individual lamp- by changing the offset value, you can make different lamps flash at different times.
If you want to, you can use the same basic code on a light as well- but, for lights I would recommend using the black-and-white cubemap instead (because it is more accurate, and would look better anyway).
EDIT: Whoops! I forgot what the brightness bit was called for lensflares! Fixed it now.
for some reason I keep getting this error:
Assets/flash.cs(1,18): error CS0116: A namespace can only contain types and namespace declarations
And the other one is, when i have a game object selected (preferably the parent for the flashing lights) i go Component > Rendering > Lens Flare. Does that mean that I attach that script above to the same game object that the lens flare is attached to?
Obviously, you need to put all of this inside a class! When you create a new script from the editor in the project window, don't delete the bit at the top about public class Whatever : $$anonymous$$onoBehaviour- it's kind of vital. You don't have to attach it to the same object, since the flare is assigned in the editor (you have to drag and drop it on to your script component). Flares are not one of the usual components, and so do not have an automatic lookup like transforms and rigidbodies do.
I used this script, with the beginning bits still in it like you said. (flareFlash.cs)
using UnityEngine; using System.Collections;
public class flareFlash : $$anonymous$$onoBehaviour {
public LensFlare flare;
public float rotSpeed; public float rotOffset; public float maxIntensity;
void Update() { float newIntensity = $$anonymous$$athf.Sin((Time.time rotSpeed) + (rotOffset $$anonymous$$athf.Deg2Rad)) * maxIntensity; flare.intensity = $$anonymous$$athf.Clamp(newIntensity, 0, maxIntensity); }
}
And now I get this error:
Assets/flareFlash.cs(16,11): error CS1061: Type UnityEngine.LensFlare' does not contain a definition for
intensity' and no extension method intensity' of type
UnityEngine.LensFlare' could be found (are you missing a using directive or an assembly reference?)
I seem to have a really bad thing with gettings errors :/
LensFlare doesn't contain a definition for 'intensity', just like the error says...
Try 'brightness' ins$$anonymous$$d:
http://unity3d.com/support/documentation/ScriptReference/LensFlare.html
ahhhh thankyou testure!!!!! the script works now! thanks for your help both of you, but just curios, would it still be ok to use a couple of point lights to sort of show the source of where the supposed light is co$$anonymous$$g from, i.e. drawing the halo on the point light....?
Answer by testure · Sep 28, 2011 at 11:25 PM
The easiest way to do it would be to parent your point lights around an empty game object and then just use the curve editor to make them rotate. No scripting required.. unless you want them to speed up/slow down or turn off/on, etc.
also, you probably need to specify what type of effect you're looking for.. you're talking about flashing, but police lights don't necessarily "flash". The typical police light is just a rotating light inside a glass lens: http://www.youtube.com/watch?v=IdaNooVo3f8
yeah, your right about the rotating bit :)
I was thinking maybe a bit like, if you've ever played grand theft auto iv, the police cruiser lights in that, they way they sort of flash in that. Here is a video: http://www.youtube.com/watch?v=PxO$$anonymous$$s91atyg (just to note: that video is in slow motion at the beginning, so the police lights actually flash normal speed throughout the video. if you skip through to 1:50 you will see what i mean....!!)
Your answer
Follow this Question
Related Questions
How to make flashing/blinking/flickering lights? 1 Answer
Police Flashing Lights? 4 Answers
Light effects on collision 1 Answer
Make character flash when taking damage [SOLVED] 0 Answers
indestructible gameobject 0 Answers