- Home /
Make character flash when taking damage [SOLVED]
EDIT: I figured it out. This also works for all particle systems. Any children in the gameobject (ie: CharacterController) you put the script on:
 public float FlashingTime = .6f;
     public float TimeInterval = .1f;
 
     void OnEnable ()
     {
         StartCoroutine(Flash(FlashingTime, TimeInterval));
     }
 
     IEnumerator Flash(float time, float intervalTime)
     {
         //this counts up time until the float set in FlashingTime
         float elapsedTime = 0f;
         //This repeats our coroutine until the FlashingTime is elapsed
         while(elapsedTime < time )
         {
         //This gets an array with all the renderers in our gameobject's children
             Renderer[] RendererArray = GetComponentsInChildren<Renderer>();
             //this turns off all the Renderers
             foreach(Renderer r in RendererArray)
              r.enabled = false;
              //then add time to elapsedtime
             elapsedTime += Time.deltaTime;
             //then wait for the Timeinterval set
             yield return new WaitForSeconds(intervalTime);
             //then turn them all back on
             foreach(Renderer r in RendererArray)
              r.enabled = true;
             elapsedTime += Time.deltaTime;
             //then wait for another interval of time
             yield return new WaitForSeconds(intervalTime);
         }
     }
Hey guys, I am trying to make my character flash/blink when he respawns/takes damage.
You see this in a lot of Mario games, where you blink between opaque and invisible and receive temporary invincibility after getting hit.
The problem is, a lot of tutorials out there are for 2D characters, and I have both meshes and particle systems on my character i need to blink in and out of transparency. Does anyone know how to achieve this?
EDIT: I got the meshes working, but can't figure out how to turn on and off the particle system renderer along with it... Here's my script:  public void Awake() { StartCoroutine(Flash(.5f, 0.1f)); } IEnumerator Flash(float time, float intervalTime) { float elapsedTime = 0f; while(elapsedTime < time ) { gameObject.GetComponentInChildren<Renderer>().enabled = false; gameObject.GetComponentInChildren<ParticleSystemRenderer>().enabled = false; elapsedTime += Time.deltaTime; yield return new WaitForSeconds(intervalTime); gameObject.GetComponentInChildren<Renderer>().enabled = true; gameObject.GetComponentInChildren<ParticleSystemRenderer>().enabled = true; elapsedTime += Time.deltaTime; yield return new WaitForSeconds(intervalTime); } }
I realise this is an old thread, and I'm not sure of the performance implications of enabling and disabling renderers at runtime, but you can simplify the enabling and disabling to r.enabled = !r.enabled, essentially shortening the loop by half. 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                