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 PuttPutt · Dec 19, 2011 at 12:05 PM · cameralights

fade in/out a light when the camera is looking at it + in range + no objects are in the way.

Hey guys,

I am making a game with a big city and many dynamic lights.

To save performance I am making a script to disable and enable lights when you get in close proximity + no object is in the way.

I woud like it so that the script also only executes when the actual light is seen by the camera

So far I have this:

 var fadeLights = 0.00;
    
 function Update () {
 distancevar = Vector3.Distance(transform.position, target.position);
 var hit : RaycastHit;
     if(Physics.Linecast(transform.position, target.position, hit)){
         if(hit.collider.gameObject.tag == "PLAYER" && distancevar < range && Tod.lightsOnOff == 1){
             this.light.enabled = true;
             if(this.light.intensity < 1){
             this.light.intensity += 10*Time.deltaTime;
                 }    
             }
         else{
         if(this.light.intensity <= 0){
         this.light.enabled = false;
                 }
         if(this.light.intensity > 0){
         this.light.intensity -= 10*Time.deltaTime;
                 }
             }    
                     
         }
 }


Do you guys have an idea what I need to add to the first if statement to achieve this?

PS, I am quite new at unity so please if you can explain it fully.

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 ks13 · Dec 19, 2011 at 04:16 PM

Hi, from what i understand, you put this script on every light right? And you got everything but the angle measurement?
If that's it then use Vector3.Angle(player.forward, (player.position - transform.position)) to check if the angle if inferior to FoV of the camera.

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 Julien-Lynge · Dec 19, 2011 at 04:25 PM 0
Share

You can also use the built in camera functions to see if the light is on-screen. Take a look at http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToViewportPoint.html - to check if the point is on screen, check that the x&y are within (0,1) and the z > 0 on the transformed Vector3.

avatar image
0

Answer by PuttPutt · Dec 19, 2011 at 10:53 PM

Ok I found a "not so elegant" solution!

Seeing how the lights disable when an object is in the way, I made a 3 small planes around the camera(right,left, back) So when you aren't looking at a light, it gets disabled.

I couldn't get the vector3 to work.

Thanks anyways!

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

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

6 People are following this question.

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

Related Questions

Know if gameObject is being seen 2 Answers

Camera Icon 1 Answer

How to make camera position relative to a specific target. 1 Answer

v2019.4 Gizmos NOT VISIBLE 0 Answers

Third person camera and Object 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