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 /
avatar image
0
Question by dacoetzee · Apr 30, 2016 at 11:41 AM · c#colliderlightingtrigger

Triggering candle lights on with Colliders

I have created numerous point lights that will randomly change in intensity like a flickering candle. All of these lights working at the same time however really slows down the game engine. So how can I trigger these lights using my FPSController to turn on when colliding. I have created a sphere collider for each individual point light and a sphere collider for my FPScontroller. Could someone help me with scripting this in C#

This is my script so far that allows the lights to flicker. I understand that i should use OnTriggerEnter and OnTriggerExit i just don't know how to script this all. Any help would greatly be appreciated, I'm a beginner using scripts and unity :)

using System; using UnityEngine; using Random = UnityEngine.Random;

[RequireComponent(typeof (Light))] public class TorchLight : MonoBehaviour {

  public float MinLightIntensity = 0.6F;
  public float MaxLightIntensity = 1.0F;
 
  public float AccelerateTime = 0.15f;
 
  private float _targetIntensity = 1.0f;
  private float _lastIntensity = 1.0f;
 
  private float _timePassed = 0.0f;
 
  private Light _lt;
  private const double Tolerance = 0.0001;
 
  private void Start() {
      _lt = GetComponent<Light>();
      _lastIntensity = _lt.intensity;
      FixedUpdate();
  }
 
  private void FixedUpdate() {
      _timePassed += Time.deltaTime;
      _lt.intensity = Mathf.Lerp(_lastIntensity, _targetIntensity, _timePassed/AccelerateTime);
 
      if (Math.Abs(_lt.intensity - _targetIntensity) < Tolerance) {
          _lastIntensity = _lt.intensity;
          _targetIntensity = Random.Range(MinLightIntensity, MaxLightIntensity);
          _timePassed = 0.0f;
      }
  }

}

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

1 Reply

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

Answer by Quertie · Apr 30, 2016 at 06:03 PM

If I am understanding this right, you want the light to flicker only if the player is within a certain area around it. Since the flickering of the light is coded in the FixedUpdate() function, you need some way to know if the player is inbound this area. All you really need is a bool ;)

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 
 [RequireComponent(typeof (Light))] public class TorchLight : MonoBehaviour {
   public float MinLightIntensity = 0.6F;
   public float MaxLightIntensity = 1.0F;
  
   public float AccelerateTime = 0.15f;
   private float _targetIntensity = 1.0f;
   private float _lastIntensity = 1.0f;
  
   private float _timePassed = 0.0f;
   private Light _lt;
   private const double Tolerance = 0.0001;
 
   public bool inBound; // Add a bool to know if player is inbound or not
  
   private void Start() {
       _lt = GetComponent<Light>();
       _lastIntensity = _lt.intensity;
       FixedUpdate();
   }
   private void OnTriggerEnter(Collider col) // When player enters area, set inBound to true
   {
     if(col.gameObject.Tag == "Player")
     {
       inBound = true;
      }
   }
   private void OnTriggerExit(Collider col) // When player leaves area, set inBound to false
   {
     if(col.gameObject.Tag == "Player")
     {
       inBound = false;
      }
   }
   private void FixedUpdate() {
       if(inBound) // logic only applies if player is inbound
       {
         _timePassed += Time.deltaTime;
         _lt.intensity = Mathf.Lerp(_lastIntensity, _targetIntensity, _timePassed/AccelerateTime);
  
         if (Math.Abs(_lt.intensity - _targetIntensity) < Tolerance) {
             _lastIntensity = _lt.intensity;
             _targetIntensity = Random.Range(MinLightIntensity, MaxLightIntensity);
             _timePassed = 0.0f;
         }
       }
   }
 } 
 

Make sure you have a Collider with isTrigger enabled on your light, and that your character has a "Player" tag, and this should work :)

Comment
Add comment · Show 2 · 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 dacoetzee · May 01, 2016 at 08:41 AM 0
Share

Thanks this worked great!!!

avatar image Quertie · May 01, 2016 at 11:01 AM 0
Share

You're welcome! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

If I deactivate the collider and then reactivate it, the trigger no longer works? 1 Answer

Can requirecomponent specify IsTrigger on a collider? 1 Answer

On Method for a collider on another object. 1 Answer

I want to write a code where i can detect collision with trigger collider in FixedUpate function(C# source) 1 Answer

Can't click gameobject when over another trigger? 1 Answer


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