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 asimov · Apr 17, 2012 at 11:45 AM · shaderscollision detectiontransparencytriggersfade

Fading objects to transparent - Apply shader with a script?

Hi,

I have a cube which has its box collider set to 'is Trigger' and a Rigidbody component (to make the Trigger work) which moves forward towards some stationary objects which also have box collider components.

I am going to have a camera moving towards these objects and want the objects to fade to transparent when they approach. For this, I have created a trigger that will cause any objects that collide with it to gradually become transparent. So far, the trigger detects the objects fine and on doing so creates a temporary renderer object to reference each object's renderer. I then create a Color object with transparency to apply to their renderer.

Now, the objects change colour just fine but their alpha does not change. I have a suspicion that I have to use a shader for this, but am not sure how to apply one if this is the case.

Also, if anyone can suggest a more elegant method for fading objects approaching the camera then that would be great too. I would like to try to avoid using iTween as am trying to learn the ropes.

Thanks in advance.

  void OnTriggerEnter(Collider theCollision)
  {
      if (theCollision.gameObject.tag == "Obstacle")
      {
          Debug.Log("Trigger collided with an Obstacle");
          Color transparencyColor = new Color(1, 1, 1, 0.01f);
          Renderer tempRenderer;
          tempRenderer = theCollision.renderer;
          Debug.Log("tempRenderer.name = " + tempRenderer.name);
          tempRenderer.material.color = transparencyColor;
      }
      if (theCollision.gameObject.tag == "Player")
      {
          Debug.Log("Trigger collided with the Player");
      }
  }
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
Best Answer

Answer by kevinseligmann · Apr 17, 2012 at 08:13 PM

Try changing

 tempRenderer.material.color = transparencyColor;

to

 tempRenderer.material.color.a = 0.01f;

Does it 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 Lo0NuhtiK · Apr 17, 2012 at 08:17 PM 0
Share

if not, try

tempRenderer.materials[0].color = whatever ;

renderer materials is an array

Edited : (I didn't read your question until after I read the posted answer and commented) ... some shaders don't have alpha or allow it's alpha to be accessed through '.color.a' etc ; if you're getting colors to change, but not alpha values, try a different shader and see if that's the problem.

avatar image syclamoth · Apr 18, 2012 at 02:28 AM 0
Share

Yeah you need to use a transparent shader. Either switch materials at the beginning of the fade, or render transparent the whole time. I don't recommend using a transparent shader the whole time, though, because you can sometimes get weird and frustrating z-sorting issues.

avatar image
0

Answer by asimov · Apr 17, 2012 at 11:39 PM

Hi Kevin,

Thanks for your suggestion, they both work which is great.

The main problem appeared to be that I needed to have a Transparent Shader type for the material applied to the Obstacles in order to change the Alpha value. I was using the default Diffuse Shader.

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 kevinseligmann · Apr 18, 2012 at 02:22 AM 0
Share

Yes, I forgot to tell you that! It happened to me a few weeks ago, and I also realized that I need to use a shader with transparency, otherwise it doesn't work.

Glad to help!

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

Standard shader's Fade rendering mode won't allow full opacity with a textured material 0 Answers

How to add a FADE effect only on a side of a model? 0 Answers

How to a 3d character transparent? 2 Answers

Can I draw Opaque geometry on top of Sprites, cheaply? 0 Answers

How to change ZWrite in transparency shaders 0 Answers


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