Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 derrtyones · Aug 11, 2013 at 08:35 PM · javascriptpoint lightlightning

Light intensity based on player distance

My code below is a bit messed up due to trying one and another but I can't get my idea to work properly.

I have a basic point light in my scene and my script attached to it. The target is the player. The var distance calculates the distance between the light and the player. What I want is that the light is off if the player is > 30 away. If the player gets close, the light must shine more brighter and if I walk further away it must dim.

How could I achieve this?

 var Target : Transform;
 var distance : float;
 var maxIntensity : float = 3.0;
 
 function Update() {
     
     distance = Mathf.Round(Vector3.Distance(gameObject.transform.position, Target.transform.position));
     
     if(distance>30){
         light.intensity = 0.0;
         print ("light off");
     }
     
     if(distance<30){
         light.intensity += Time.deltaTime * distance;
         if(light.intensity == 0.0){
             light.intensity = 0.1;
         }
     }
     
     if (Random.value > 0.9 && light.intensity > 0.1) {
         StartCoroutine("FlickerLight");
     }
     
 }
 
 function FlickerLight() {
     while(light.intensity > 0.0) {
         light.intensity -= Time.deltaTime * distance;
         yield;
     }
  
     while(light.intensity < 1.0) {
         light.intensity += Time.deltaTime * distance;
         yield;
     }
  
     light.intensity = 1.0;
 }
Comment
Add comment · Show 1
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 Joyrider · Aug 11, 2013 at 08:46 PM 0
Share

Tell me if I'm wrong, but reading your code, even if I am at 29 meters, the light will get brighter and brighter, right?

1 Reply

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

Answer by robertbu · Aug 11, 2013 at 08:44 PM

For your specific question, not dealing with the flickering lights, you can do:

 if (distance > 30) {
     light.intensity = 0.0;
 }
 else {
     light.intensity = (1.0 - distance/30.0) * maxIntensity;
 } 

Where 'maxIntensity' is a variable you define and initialize or you can replace it with a hard-coded value. According to the reference, Light.intensity (and therefore 'maxIntensity) must be set in range of 0 to 8.

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 derrtyones · Aug 11, 2013 at 08:47 PM 0
Share

Almost perfect! Your code works, but it kind of stutters if you get what I mean. How can we make the intensity of the light change more fluently?

avatar image Joyrider · Aug 11, 2013 at 08:49 PM 0
Share

Indeed, should be pretty straightforward. You can make things a little more complicated but using the value from robertbu's code as a targetvalue, and add/remove value from your intensity each frame.

Which would look more like what your code was originally meant to do, or that would be my guess anyway.

avatar image robertbu · Aug 11, 2013 at 08:54 PM 1
Share

You can use $$anonymous$$athf.Lerp(). $$anonymous$$erge this into your code:

 var intensity : float;
 var speed : float = 3.0;
 var maxIntensity = 8.0;
 
 function Update() {
 
     if (distance > 30.0) {
         intensity = 0.0;
     }
     else {
         intensity = (1.0 - distance/30.0) * maxIntensity;
     } 
     
     light.intensity = $$anonymous$$athf.Lerp(light.intensity, intensity, speed * Time.deltaTime);
 }

'speed' will deter$$anonymous$$e how quickly the light will adjust. This code will produce an easing of the light to its final intensity. For a non-eased look, use $$anonymous$$athf.$$anonymous$$oveTowards() ins$$anonymous$$d of $$anonymous$$athf.Lerp(). You will have to use a different range of values for 'speed' if you change to $$anonymous$$oveTowards().

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a point light which flashes when a key is pressed? 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Random Array of sounds for Random Game Object 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Animation for weapon plays on game start instead of when i attack 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