• 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 EpicEra · Oct 06, 2013 at 01:39 PM · lerpontriggerenteralphaontriggerexitopacity

OnTriggerEnter change alpha to half over time.

Hello every one! I need help with changing my alpha on a material to 0.5 over time. This event will occur when a ball rolls into a specific zone and a windmill are suppose to be half transparent while the ball is in that zone. Im using toon shaders with outlines, but when collission happens the shader will first change to a shader with alpha values, at the moment the code works but with no lerp/change over time.

My code so far:

 var Windmill : GameObject; // Put Windmill mesh here in the inspector
 var WindmillBlades : GameObject; // Put Windmill blades here
 var WindmillMotor : GameObject; // Put Windmill motor here
 var ToonAlpha : Shader; //Add shader in inspector - the one with alpha
 var ToonNonAlpha : Shader; //Add shader in inspector - the one with no alpha and outlines
 
 
 function Start () {
 }        
         
 function OnTriggerEnter (c : Collider)
 {
     if (c.CompareTag("Player"))
     {
     Windmill.renderer.material.shader = ToonAlpha;
     WindmillMotor.renderer.material.shader = ToonAlpha;
     WindmillBlades.renderer.material.shader = ToonAlpha;
     
     Windmill.renderer.material.color.a = 0.5; // 50 % transparent, I want this to lerp.
     WindmillMotor.renderer.material.color.a = 0.5; // 50 % transparent, I want this to lerp.
     WindmillBlades.renderer.material.color.a = 0.5; // 50 % transparent, I want this to lerp.
     }
 }
 function OnTriggerExit (c : Collider)
 {
     if (c.CompareTag("Player"))
     {
     WindmillBlades.renderer.material.color.a = 1.0; // fully opaque , I want this to lerp.
     WindmillMotor.renderer.material.color.a = 1.0; // fully opaque, I want this to lerp.
     Windmill.renderer.material.color.a = 1.0; // fully opaque, I want this to lerp.
     
     WindmillBlades.renderer.material.shader = ToonNonAlpha;
     WindmillMotor.renderer.material.shader = ToonNonAlpha;
     Windmill.renderer.material.shader = ToonNonAlpha;
     }
 }
 
 function Update () {
 }

Sadly I have no Idea how to make it work, not so good with lerp and such things. I have tried some lerping but all in vain, and I dont know If I should have my "ontrigger" inside the update function for stuff to work with "Time.deltaTime".

Thank you for helping out :)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by EpicEra · Oct 06, 2013 at 04:56 PM

A big THANK YOU guys! Now I got a working code for half transparency, both ways :) When you leave the collider the opacity goes up as well and it looks nice.

 var Windmill : GameObject; // Put Windmill mesh here in the inspector
 var WindmillBlades : GameObject; // Put Windmill blades here
 var WindmillMotor : GameObject; // Put Windmill motor here
 var ToonAlpha : Shader; //Add shader in inspector - the one with alpha
 var ToonNonAlpha : Shader; //Add shader in inspector - the one with no alpha and outlines
 
 
 var makeTransparent = 0;
 var smooth = 0.01;
 
 
 function Start () {
 }        
         
 function OnTriggerEnter (c : Collider)
 {
     if (c.CompareTag("Player"))
     {    
     Windmill.renderer.material.shader = ToonAlpha;
     WindmillMotor.renderer.material.shader = ToonAlpha;
     WindmillBlades.renderer.material.shader = ToonAlpha;
 
     makeTransparent = 1;
     
     }
 }
 function OnTriggerExit (c : Collider)
 {
     if (c.CompareTag("Player"))
     {
     makeTransparent = 2;
     }
 }
 
 function Update () {
     if (makeTransparent == 1)
     {
     IntensityChanging();
     }
     else if (makeTransparent == 2)
     {
     IntensityChanging2();
     }
 }
 
 function IntensityChanging ()
 {
     Windmill.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 0.5, smooth*Time.deltaTime);
     WindmillMotor.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 0.5, smooth*Time.deltaTime);
     WindmillBlades.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 0.5, smooth*Time.deltaTime);
     
 }
 
 function IntensityChanging2 ()
 {
     Windmill.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 1.0, smooth*Time.deltaTime);
     WindmillMotor.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 1.0, smooth*Time.deltaTime);
     WindmillBlades.renderer.material.color.a = Mathf.Lerp(Windmill.renderer.material.color.a, 1.0, smooth*Time.deltaTime);
         if (Windmill.renderer.material.color.a >= 0.99)
         {
         WindmillBlades.renderer.material.shader = ToonNonAlpha;
         WindmillMotor.renderer.material.shader = ToonNonAlpha;
         Windmill.renderer.material.shader = ToonNonAlpha;
         makeTransparent = 0;
         }
     
 }
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 smallbit · Oct 06, 2013 at 03:22 PM

Try this tutorial, it deals with intensity but should give you some hints :)

http://unity3d.com/learn/tutorials/modules/beginner/scripting/lerp

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 gardian06 · Oct 06, 2013 at 02:17 PM

Time.DeltaTime is a publicly accessible static property. realistically you can get to it from anywhere, but there are very specific cases where you should never need to get it (off hand FixedUpdate as this is time independent therefore you shouldn't need to reference time).

though I am not directly clear on what you mean by OnTriggerXXX inside update (stab in dark) probably mean that the logic of the OnTriggerXXX in Update if this is true then yes.

if you attempted to do a lerp inside the OnTriggerXXX (except for stay) then you will not get a lerp to work; because OnTriggerEnter is called the frame immediately after the collision has occurred with a trigger, and OnTriggerExit is called the frame immediately after the collision has stopped with a given trigger. basically you would be doing on iteration of a change function (most change functions need to be called repeatedly either until they are complete, or some condition that they need to stop.)

try placing the change logic in the Update, or a co-routine, and just add control logic to the OnTriggerXXX

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 EpicEra · Oct 06, 2013 at 02:27 PM 0
Share

So I should lerp inside the update function insted, lets say by: On collission set "var hit" to on and then in the update function have an if statement that does the change and lerping wehen "var hit" is set to on?

Seems like a solid plan but how do i lerp it? Im having a hard time understanding theese 2 http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Lerp.html

and I bet there are other ways of doing it, any recommendations?

/cheers

avatar image gardian06 · Oct 06, 2013 at 03:42 PM 0
Share

Lerp might not be the absolute best option in all conditions for example the traditional version of Lerp (without an accumulator) is an example of the classical infinite half-steps problem, but with an accumulator it is a linear. though if you don't want to have to deal with having an accumulator sitting around you could just use $$anonymous$$athf.$$anonymous$$oveTowards ins$$anonymous$$d. I question the example given in $$anonymous$$athf.Lerp mainly because if Time.Time is greater then 1 then it will instantly finish.

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

17 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

Related Questions

changes to colour alpha affect RGB values, but not in the same way 1 Answer

Display gui box after trigger is activated? 2 Answers

How can I get trigger enter/exit every frame when I change Time.timeScale? 0 Answers

How to use OnTriggerEnter and OnTriggerExit in multiplayer game 1 Answer

Problem using trigger to set gameobjects active 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