c# - Fading in/out GameObject -




i'm new coding, i'm still trying develop logic of thinking me create solutions i'm wanting games. currently, i'm in unity trying create 2d gameobject that's wall hiding secret door. want gameobject fade out (about 90%) when player gameobject triggers it, revealing space behind , hidden door.

so far, i've managed figure out how render "secret wall" go inactive on trigger, disappears, doesn't produce visual i'm going for. said, i'm still working on developing coder's way of thinking, while i've done lot of research solve problem, many of results don't readily understand.

here's code:

using system.collections; using system.collections.generic; using unityengine;  public class secretdoor1 : monobehaviour {      void ontriggerenter2d (collider2d secretdoortrig) {          if (secretdoortrig.gameobject.tag == "player") {              getcomponent<spriterenderer> ().enabled = false;          }          else {              getcomponent<spriterenderer> ().enabled = true;          }     }      void ontriggerexit2d (collider2d secretdoortrig) {           if (secretdoortrig.gameobject.tag == "player") {               getcomponent<spriterenderer> ().enabled = true;          }          else {              getcomponent<spriterenderer> ().enabled = false;          }     } } 

fading sprite the-same moving gameobject on time except modify alpha instead of it's position.

the 3 important stuff fading object time.deltatime, mathf.lerp/color.lerp , coroutine. need understand how these work together.

start coroutine, use time.deltatime increment variable. variable used use determine how function has ran. in for/while loop, use variable incremented every-frame , duration want fade happen generate alpha of mathf.lerp function. create new color alpha , and assign sprite.

this done every frame until variable incremented time.deltatime reaches duration want fade happen within.

here simple spriterenderer fade function:

public spriterenderer spritetofade;  ienumerator fadeout(spriterenderer myrenderer, float duration) {     float counter = 0;     //get current color     color spritecolor = myrenderer.material.color;      while (counter < duration)     {         counter += time.deltatime;         //fade 1 0         float alpha = mathf.lerp(1, 0, counter / duration);         debug.log(alpha);          //change alpha         myrenderer.color = new color(spritecolor.r, spritecolor.g, spritecolor.b, alpha);         //wait frame         yield return null;     } } 

if want fade in, change mathf.lerp(1, 0, counter / duration); mathf.lerp(0, 1, counter / duration); make alpha go 0 1 over-time instead of 1 0.


from example above, writing fade-out , fade-in functions requires way tell function change alpha 1 0 or 0 1. can make function use boolean or enum variable determine type of fade perform. of-course, can separate fade-in/fade-out functions it's have in 1 function.

here extended version of function supports fade-in , fade-out. supports gameobjects meshrenderer(3d), spriterenderer(2d), image, rawimage....you can extend support more components that's missing.

ienumerator fadeinandout(gameobject objecttofade, bool fadein, float duration) {     float counter = 0f;      //set values depending on if fadein or fadeout     float a, b;     if (fadein)     {         = 0;         b = 1;     }     else     {         = 1;         b = 0;     }      int mode = 0;     color currentcolor = color.clear;      spriterenderer tempsprenderer = objecttofade.getcomponent<spriterenderer>();     image tempimage = objecttofade.getcomponent<image>();     rawimage temprawimage = objecttofade.getcomponent<rawimage>();     meshrenderer temprenderer = objecttofade.getcomponent<meshrenderer>();      //check if sprite     if (tempsprenderer != null)     {         currentcolor = tempsprenderer.color;         mode = 0;     }     //check if image     else if (tempimage != null)     {         currentcolor = tempimage.color;         mode = 1;     }     //check if rawimage     else if (temprawimage != null)     {         currentcolor = temprawimage.color;         mode = 2;     }     //check if 3d object     else if (temprenderer != null)     {         currentcolor = temprenderer.material.color;         mode = 3;          //enable fade mode on material if not done         temprenderer.material.setfloat("_mode", 2);         temprenderer.material.setint("_srcblend", (int)unityengine.rendering.blendmode.srcalpha);         temprenderer.material.setint("_dstblend", (int)unityengine.rendering.blendmode.oneminussrcalpha);         temprenderer.material.setint("_zwrite", 0);         temprenderer.material.disablekeyword("_alphatest_on");         temprenderer.material.enablekeyword("_alphablend_on");         temprenderer.material.disablekeyword("_alphapremultiply_on");         temprenderer.material.renderqueue = 3000;     }     else     {         yield break;     }      while (counter < duration)     {         counter += time.deltatime;         float alpha = mathf.lerp(a, b, counter / duration);          switch (mode)         {             case 0:                 tempsprenderer.color = new color(currentcolor.r, currentcolor.g, currentcolor.b, alpha);                 break;             case 1:                 tempimage.color = new color(currentcolor.r, currentcolor.g, currentcolor.b, alpha);                 break;             case 2:                 temprawimage.color = new color(currentcolor.r, currentcolor.g, currentcolor.b, alpha);                 break;             case 3:                 temprenderer.material.color = new color(currentcolor.r, currentcolor.g, currentcolor.b, alpha);                 break;         }         yield return null;     } } 

usage:

gameobject fade:

public gameobject spriterend; 

fade-out in 3 seconds

startcoroutine(fadeinandout(spriterend, false, 3f)); 

fade-in in 3 seconds

startcoroutine(fadeinandout(spriterend, true, 3f)); 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -