• 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 primus88 · Mar 15, 2014 at 09:39 PM · lightingculling

How to disable point lights not in the camera frustum

Hello,

I feel a bit crazy asking this since I can't find this question anywhere.

I have a load of lights in my scene. Can I do something so that lights are disabled when not in camera view or behind walls?

Thank you

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 highpockets · Mar 15, 2014 at 10:27 PM 1
Share

I don't know if there is a typical way of doing this, but one thing that comes to $$anonymous$$d is putting trigger colliders on lights or groups of lights that are in the same area and expand the collider to cover the area that you see fit. When the player enters the area (OnTriggerEnter) turn the light(s) on ( gameObject.SetActive(true);). When the player exits the area affected by the light(s) (OnTriggerExit) turn the lights off (gameObject.SetActive(false);)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by raycosantana · Mar 15, 2014 at 11:20 PM

Maybe its not exactly what you want but in a old project I made this script to disable lights when they where to a certain distance from the player.

 using UnityEngine;
 using System.Collections;
 
 public class LightScript: MonoBehaviour {
 
     public float availableDistance;
     private float Distance;
     private Light Lightcomponent;
     private GameObject Player;
     ///------------------------------
     void Start(){
     Lightcomponent = gameObject.GetComponent<Light>();
     Player = GameObject.FindGameObjectWithTag("Player");
         }
      
     // Update is called once per frame
     void Update () {
     
      
     Distance = Vector3.Distance(Player.transform.position, transform.position);
 
 
         if (Distance < availableDistance){
         Lightcomponent.enabled = true;    
         }
         if (Distance > availableDistance){
         Lightcomponent.enabled = false;
                 }
     }
     
 }
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 primus88 · Mar 15, 2014 at 11:41 PM 0
Share

raycosantana and agohighpockets, I want to thank you both a lot! Both ideas were awesome but I went with ray's idea. I tested both and couldn't see any difference, but since ray's didn't used triggers I decided it's safer to use it.

Lots of thanks again guys. I wasn't expecting such quick and good support!

avatar image DigitalCandy primus88 · Apr 09, 2017 at 05:34 PM 0
Share

If you going to be using this technique, consider only checking the distance every few frames. Do you really need to know if every light is within range 50 times a second? Currently, if you had 30 lights in your scene, and your game is updating at the usual 50 frames a second, you're doing 50 frames x 30 lights = 1500 distance checks every second. Adding a timer that executed your update code, say, once every a second, you're down to only 30 checks a second, with absolutely no noticeable difference to your game.

avatar image toddisarockstar · Apr 09, 2017 at 06:30 PM 0
Share

digitalcandy is exactly right. no need to call this everyframe. especially using unitys vector3 distance. it uses the pothagarim approach that uses a bunch of sqaure roots and stuff to do an exact calulation on all the coordinates. Anytime i can, Especially when called often i replace it with a code that returns distance with a box shape which is much more proficient. also many times you really only need 2d distance ins$$anonymous$$d of 3d. detecting if the player is within a box shape of the light might be better if you are dealing with square rooms anyways.

     float dist(Vector3 v1,Vector3 v2){
         float f;
         float f2;
         f=v1.x-v2.x;
         if(f<0){f=f*-1;}
         f2=v1.z-v2.z;
         if(f2<0){f2=f2*-1;}
 
         if(f>f2){f2=f;}
         //within  this size  box!!
                return f2;
         }






  

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

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

How to create a Minecraft-like fog when stuff around player is not being drawn? 1 Answer

Lights on at night lights off during the day 1 Answer

Toggle built in lighting in script 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