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 Netram · May 29, 2015 at 05:21 AM · scripting problemrenderertransparent

Transparent object (scripting)

Hi all! I have a roof in my game and I want it to turn transparent when the player character walks under it. I have moved the collider of the roof down under it and knows that part works so therefore I need help with the transparent part. I have read several post with the solution: this.GetComponent().material.color.a = 0.05f; but have realized that this wont work due to the fact that the color.a is a Read Only variable. And because of this I wrote my code like this:

 private Color colorOrig;
 private Color colorTrans;
 private Renderer r;
 
     void Start(){
         r = this.GetComponent<Renderer>();
         colorOrig = r.material.color;
         colorTrans = new Color (colorOrig.r, colorOrig.g, colorOrig.b, 0.5f);
     }
 
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Player") {
             r.material.color = colorTrans;
         }
     }
 
     void OnTriggerExit(Collider other) {
         if (other.tag == "Player")
             r.material.color = colorOrig;}

But it doesn't work. Any suggestions?

/Netråm

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
1

Answer by karma0413 · May 29, 2015 at 09:17 AM

I actually just completed this specific function for my game.. It is true that it is a read-only variable.. all sub-variables within a vector are read only. To change it you must set it as it's whole variable... (and not simply modify only the alpha) ....

Accessing the alpha variable is as follows: (One of the many ways)

 Color theColorToAdjust = renderer.material.color;
 theColorToAdjust.a = 0f; // Completely transparent
 theColorToAdjust.a = 1f; // completely opaque



*** ONCE you modify the sub-variable alpha, then you can SET IT by passing through the entire variable COLOR instead of only it's ALPHA. renderer.material.color = theColorToAdjust; //Which contains your new alpha setting

 // If the color you want to adjust is on another different gameobject then you would....
 Color theColorToAdjust = GameObject.FindGameObjectWithTag ("Player").GetComponent <Renderer>().material.color;
 theColorToAdjust.a = 0f;


Since I have completed this entire thing, here is how I went about it.... 1. First I determine if something is standing in between my character and the camera. ( Is something like a roof in the way?). To do this; I made all of my roofs to be on a layer that I called "FadeLayer". And then I simply did a physics.Raycast from the camera to my character and if my raycast touches anything that is on the FadeLayer, like a roof, then it will proceed to make it transparent....

 Vector3 direction = GameObject.FindGameObjectWithTag ("Player").transform.position - transform.position;
         float distanceRay = Vector3.Distance (transform.position, GameObject.FindGameObjectWithTag ("Player").transform.position);
         RaycastHit hitInfo;
         Physics.Raycast (transform.position, direction, out hitInfo, distanceRay);
         if (hitInfo.collider != null) 
         {
 
             if (hitInfo.collider.gameObject.layer == 8) 
             {
                 hitInfo.collider.gameObject.GetComponent <FadeFlag> ().GoFade ();
             }
         }



On every Roof or on every object that is supposed to "Go transparent", I have this script attached to it... Please notice the last command in the code above is telling the FadeFlag scipts to "GoFade" which my function for initiating the transparency function.

  • And so as long as the rooftop continues to receive the message to GoFade...Then it will continue to try to be transparent until it reaches full transparency.... However, as soon as it is no longer getting a signal then ONLY AFTER 5 seconds, it will go back to being opaque.

Here is the FadeFlag script:

 public bool receivedSignal = false;
 public bool amITransparent = false;
 public float signalTimeStamp;
 public float returnDelay = 5f;
 public float fadeIn = 0.05f;
 public float fadeOut = 0.15f;

 void FixedUpdate () 
     {
         Color thisColor = renderer.material.color;
         if (thisColor.a < 1f) 
         {
             amITransparent = true;
         } else 
             {
                 amITransparent = false;
             }
 
         if (Time.realtimeSinceStartup >= signalTimeStamp + returnDelay && amITransparent == true) 
         {
             // Begin making it opaque again because we haven't received a signal in over 5 seconds
             receivedSignal = false;
             thisColor.a = thisColor.a + fadeIn;
             if (thisColor.a >= 1f){thisColor.a = 1f;}
             renderer.material.color = thisColor;
 
         }
         if (receivedSignal == true) 
         {
             // then continue to fade to 100% transparency
             thisColor.a = thisColor.a - fadeOut;
             if (thisColor.a <0f){thisColor.a = 0f;}
             renderer.material.color = thisColor;
 
         }
     }
 
     public void GoFade ()
     {
         receivedSignal = true;
         signalTimeStamp = Time.realtimeSinceStartup;
 
     }
 }



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 revolute · May 29, 2015 at 06:49 AM

Your shader should be support color blending.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make an object transparent when below 1.0A, and completely opaque at 1.0A? 3 Answers

Get the renderer of multiple GameObjects at the same time? 1 Answer

Changing all materials of selection from code 0 Answers

How can I make a transparent ground, that hides other objects beneath it? 1 Answer

How Do I Not Render Objects That My Player Doesn't See 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