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 dot · Jun 02, 2011 at 06:36 PM · raycastcoloranimate

[well commented code example] problem animating color.alpha of objects above the character

@@^&#*$&(()!! All day work, I can't figure out what the hell...

We have a character. and there will be objects above it. All I want is when objects are above the character, their Color.a goes down. (up to, say .2, and, besides, if they don't have Transparent/Diffuse shader yet, they are given one) and when those objects are no longer above the character their color.a goes up to 1.

And I can't figure that out!!!

Here's my code, which I added to the character:

 //Timer: so that the rays aren't cast every frame, but - for example- every half a second would suffice
 //trying to reduce performance costs this way
 
 var checkDelay = 0.5;
 private var nextCheck = 0.5;
 function Timer(){
     if(Time.time >= nextCheck)
     {
         nextCheck = Time.time + checkDelay;
         return true;
     }
     else
         return false;        
 }
 
 
 var selected = new ArrayList(); //will store renderers which are selected this frame
 var prevSel = new ArrayList(); //will store renderers which were selected last frame
 var toFadeIn= new ArrayList(); //will store renderers which need to be faded back into full visibility
 
 function Update () {
     if(Timer()){
         AllR3();
     }
     FadeIn(toFadeIn);
     FadeOut(selected);
 }
 
 function AllR3(){
     var hits:RaycastHit[];
     hits = Physics.RaycastAll(transform.position,Vector3.up);
     
 //deals with what happened last frame
 //renderers hit on the last frame will be saved in prevSel (previous selection)
 //and "selected" cleared to
 
     prevSel = selected;
     selected.Clear();
 
 //collects all the renderers in a list
 //and changes shaders to transparent when necessary
     for(var i=0; i<hits.Length; i++){
         var rend = hits[i].collider.renderer;
         if(rend){
             rend.material.shader = Shader.Find("Transparent/Diffuse");
             selected.Add(rend);
         }
     }
     
 //using renderers selected on previous frame (prevSel)
 //and those just selected,
 //to find those rendenderes who were selected last frame, but arent any more...
 
     for( i =0; i<prevSel.Count;i++){
             if(selected.Contains(prevSel[i])){
                 prevSel.RemoveAt(i);
                 i--;
             }
             else
                 toFadeIn.Add(prevSel[i]);
         }
     //..and makes a "toFadeIn" array that holds all of them
     
 
 }
 
 //functions that do the Color.a animation - fading in and fading out
 
 var targetFadeOut = .3; //how much transparent we want the renderes to get when character is under them
 var fadeDuration= .3; //how much time it will take to change the color.a
 
 function FadeIn(obj){
     if(obj.Count ==0) //if there aren't any elements to animate - don't continue with the script
         return;
     for(var i = 0; i< obj.Count; i++){
         if(obj[i].material.color.a == 1.0){
             //finish
             //~ obj[i].material.shader = Shader.Find("Diffuse"); //alternatively - go back to diffuse
             obj = obj.RemoveAt(i); //the only way to get out of "toFadeIn" array is to actually get color.a to 1.0, this will remove the renderer from array as there's nothing else we want to do with that renderer
             i--;//fix cause removing an element will put the next element in its place, which still needs to be analyzed
         }
         else
             obj[i].material.color.a = Mathf.Lerp(obj[i].material.color.a,1.0,fadeDuration * Time.deltaTime); //if target is not achieved - animate towards it
     }
 }
 function FadeOut(obj){
     if(obj.Count == 0)
         return;
     for(var i=0; i< obj.Count; i++)
         if(obj[i].material.color.a == targetFadeOut){
             //finish
 //still will need till it's no longer selected, moves to toFadeIn, fades in and only then disappears from our arrays
         }
         else{
             obj[i].material.color.a = Mathf.Lerp(obj[i].material.color.a,targetFadeOut, fadeDuration * Time.deltaTime);
             print("fade out inside else");
             }
 }

Could you give me a hand?

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to find out if raycast dont hit anything 1 Answer

Help - Selection System 1 Answer

Change Color of Object when NOT looking at it 1 Answer

Referencing color change when object is 'deselected' 2 Answers

Get pixel from Rotated Texture2D 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