- Home /
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 :)
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;
}
}
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
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
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
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
![](https://koobas.hobune.stream/wayback/20220613115418im_/https://answers.unity.com/themes/thub/images/avi.jpg)