• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Donilias · Nov 27, 2011 at 12:23 AM · pointlight

pointlight on/off like in horror games

Hey guy's. I was looking for a JS were the pointlight goes on/off like in horror movies/games.

By example; 2 seconds light and then it's off for a half second and then half second on, and then 1 second off and then again 2 seconds on.

2 seconds: on -> 0.5 second: off -> 0.5 second: on -> 1 second: off -> Then again and over again.

I was looking but i couldn't find anything.

I thought something like this;

     function Update() {
     light.range += 5.0 * 0.0;
     light.range +=-5.0 * 2.0;
     light.range += 5.0 * 2.5;
     light.range +=-5.0 * 3.5;
 }

But i know it's wrong :(

Someone help me please.

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Nov 27, 2011 at 01:33 AM

Why not do it with a coroutine?

var minOnTime : float = 1; var maxOnTime : float = 2.5; var minOffTime : float = 0.2; var maxOffTime : float = 0.6;

// set this in the inspector- drag the actual mesh onto it! var lampRenderer : MeshRenderer;

private var lightIntensity; private var glowyTexture : Texture2D; private var blackTexture : Texture2D;

function Start() { lightIntensity = light.intensity; blackTexture = new Texture2D(2, 2); var cols : Color[] = new Color[4]; for( var i = 0; i < cols.Length; ++i ) { cols[i] = Color.clear; } blackTexture.SetPixels(cols); blackTexture.Apply();

 glowyTexture = lampRenderer.material.GetTexture("_Illum") as Texture2D;
 StartCoroutine(FlickerLoop());

}

function FlickerLoop() { while(true) { yield WaitForSeconds(Random.Range(minOnTime, maxOnTime)); light.intensity = 0; lampRenderer.material.SetTexture("_Illum", blackTexture); yield WaitForSeconds(Random.Range(minOffTime, maxOffTime)); light.intensity = lightIntensity; lampRenderer.material.SetTexture("_Illum", glowyTexture); } }

This will make the light randomly flicker by the four values you can change in the inspector. You can also adapt this script to make the light jump around a bit, or make it change colour!

(I'm not too hot with JS, since I'm usually a C# man, so tell me if this has any errors since it's not tested!)

EDIT: There was one other issue- it should have been Color.clear, not Color.black where I set the 'black' texture! Sorry again (forgot how illum textures worked)

Comment
Add comment · Show 22 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Donilias · Nov 27, 2011 at 01:45 AM 0
Share

were should i draw this JS? Because i drawed this in the pointlight and nothing happend.

avatar image syclamoth · Nov 27, 2011 at 01:47 AM 0
Share

Oh whoops! I forgot one important line! Fixing it now. It was the one in Start() under the lightIntensity thing. Sorry!

avatar image Donilias · Nov 27, 2011 at 01:55 AM 0
Share

It's working and i think this looks perfect. But only one question. I have a lamp with this pointlight in it. So the lamp-texture is self-illumn. Can i remove the self-illumn when the light turns off, and the texture receives his self-illumn back when the light turns on. And so over again?

avatar image syclamoth · Nov 27, 2011 at 02:11 AM 0
Share

Oh! Yes. But since the builtin self-illu$$anonymous$$ating shader does not have a slider for 'illu$$anonymous$$ation', you'll have to provide a 'black' texture to put in the illu$$anonymous$$ation slot when it's not turned on. So, at the top of the script, put

 var blackTexture : Texture2D;
 var glowyTexture : Texture2D;

Then, having assigned them in the inspector the normal way, you can use

 renderer.material.SetTexture("_Illum", blackTexture);

and

 renderer.material.SetTexture("_Illum", glowyTexture);

next to the lines where you turn the light on and off.

(sorry this took so long, I had to do some research!)

avatar image Donilias · Nov 27, 2011 at 02:16 AM 0
Share

And what if i don't use textures, but only colors? like white:color.white and black:color.black

Show more comments
avatar image
0

Answer by Kilometers · Nov 27, 2011 at 12:53 AM

One of your problems is that you would be making all of these changes in a single frame. That is what the Update() function amounts to each time it is called: a single frame. There are different approaches to spacing out a timed event, but one way might be this:

         float lastTime = 0; // Keeps track of the last time the light was changed
         bool lastState = false; // Keeps track of the last state the light was set to, on (true) or off (false)
 

 void Update () {
     if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
     {
         if (lastState == false) // If the light was off...
         {
             light.range = 5; // Turn it on.
             lastState = true;
         }
         else // If the light was on...
         {
             light.range = 0; //Turn it off.
             lastState = false;
         }

         lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
     }
 }

Note the Random.Range() function. It will generate a value between 0.5 and 2, which correspond to .5 seconds and 2 seconds. It serves as the interval for the flicker. This ought to be an easier way to get a random stutter to the light.

EDIT: I just realized that you were looking for javascript. Sorry about that. Still the concept applies, and aside from the way the variables are declared, I don't think too much of the code is different.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Donilias · Nov 27, 2011 at 01:14 AM 0
Share

I don't have any knowledge with C#. So i don't know what to do.

avatar image syclamoth · Nov 27, 2011 at 01:35 AM 1
Share

I don't think you should be calling 'Random.Range' every frame like that. Because the value gets reset every time, it's much more likely to fall on the '0.5' side than the '2' side, since it checks every time.

avatar image Kilometers · Nov 27, 2011 at 01:55 AM 0
Share

Ah I didn't know that. Thanks!

avatar image
0

Answer by Geko_X · Nov 27, 2011 at 01:30 AM

I converted the code for you:

     private var lastTime : float = 0; // Keeps track of the last time the light was changed
     private var lastState : boolean = false; // Keeps track of the last state the light was set to, on (true) or off (false)
     
     
     function Update () {
         if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
         {
             if (lastState == false) // If the light was off...
             {
                 light.range = 5; // Turn it on.
                 lastState = true;
             }
             else // If the light was on...
             {
                 light.range = 0; //Turn it off.
                 lastState = false;
             }
     
             lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
             }
      }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Donilias · Nov 27, 2011 at 01:46 AM 0
Share

This is working Thnx. But i'm still waiting for the best choice. Yours or syclamoth :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

flashlight on/off 1 Answer

Light and lag problem 0 Answers

how to make sun? 1 Answer

When 2dsprite is flipped (rotation.y = 180) the 2d light does not work 1 Answer

Multiple point lights but only 1 is being as intense as it should be 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges