Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tmartinez88 · Mar 07, 2016 at 11:13 AM · colliderscripting beginnerlight

Fade Light in OnTriggerEnter and Fade Light out OnTriggerExit

Hey all,

Trying to get a light to fade in and out when the player enters and exits a boxcollider (is Trigger is selected). I took some stuff from here https://unity3d.com/learn/tutorials/projects/stealth/alarm-lights but am getting a lot of flickering when I attach this script to the box collider gameObject. I can tell something is happening when I enter and exit the collider but looks terribly messed up. I can get the light to turn on and off with no problem but when I introduce the lerping and if statements I get thrown off. Any ideas? I imagine I'm doing something really dumb here but i'm but a lowly n00b

 using UnityEngine;
 using System.Collections;
 
 public class fadelight : MonoBehaviour {
 
     public Light light;
     public float fadeSpeed = 2f;
     public float highIntensity = 6f;
     public float lowIntensity = 0.0f;
     public float changeMargin = 0.2f;
     public bool lightison = false;
     private float targetIntensity;
 
     void Awake(){
         light.enabled = false;
     }
 
     void OnTriggerEnter() {
         lightison = true;
     
     }
 
     void OnTriggerExit() {
 
         lightison = false;
     }
 
     void Update() {
         if (lightison == true) {
             light.enabled = true;
             light.intensity = Mathf.Lerp (lowIntensity, highIntensity, fadeSpeed * Time.deltaTime);
         } else {
             light.intensity = Mathf.Lerp (highIntensity, lowIntensity, fadeSpeed *Time.deltaTime);
         
         }
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Umresh · Mar 07, 2016 at 12:04 PM

Its because of the Lerp you used the intensity of the light is not reached the destination intensity(low/final). Intensity of light keeps varying. Clamp the value using condition to avoid flickering.

Comment
Add comment · 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
0

Answer by tmartinez88 · Mar 09, 2016 at 06:59 AM

So @Umresh you're saying that as long as I'm onTriggerEnter then it's consistently going between low and high or vice versa and that's why it's flickering? will look at clamp haha thanks

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 tmartinez88 · Mar 09, 2016 at 07:37 AM 0
Share

Hey all so if you want to fade a light in or out based on a collider trigger event HERE is the script haha. This is for everybody who thought they couldnt succeeed!! ;|) enjoy!!!! $$anonymous$$ake sure your collider has "is trigger" is checked yuppp

 using UnityEngine;
 using System.Collections;
 
 public class fadelight : $$anonymous$$onoBehaviour {
     
     public Light light;
     public float fadeSpeed = 2f;
     public float highIntensity = 6f;
     public float lowIntensity = 0.0f;
     public float change$$anonymous$$argin = 0.2f;
     private bool lightison = false;
 
     private float targetIntensity;
     
     void Awake(){
         light.intensity = 0f;
         targetIntensity = highIntensity;
     }
     
     void OnTriggerEnter() {
         lightison = true;
         
     }
     
     void OnTriggerExit() {
         lightison = false;
     }
     
     void Update() {
         if (lightison) {
             light.intensity = $$anonymous$$athf.Lerp (light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
             CheckTargetIntensity ();
         } else {
             light.intensity = $$anonymous$$athf.Lerp (light.intensity, 0f, fadeSpeed * Time.deltaTime);
         }
     }
     void CheckTargetIntensity() {
         if($$anonymous$$athf.Abs (targetIntensity - light.intensity) < change$$anonymous$$argin)
         {
                 if(targetIntensity == highIntensity){
                 targetIntensity = lowIntensity;
             }
         else {
                 targetIntensity = highIntensity;
             }
     }
 
 }
 
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help! My Temporary Character is an Accidental Yogi! 0 Answers

Changing light color between players when they collide (BOLT) 0 Answers

Player boundary restriction 1 Answer

Can't do copy of destroyed bullet 1 Answer

OnTriggerExit does not work 0 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