Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by thestrandedmoose · Dec 30, 2017 at 01:43 AM · animationdamagehitblinkingflashing

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); } }

Comment
Add comment · Show 2
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 MichaelABC · Mar 07 at 03:08 PM 0
Share

I don't want to ever read code like above here :(

avatar image flymania117 · May 11 at 09:40 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

195 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make flashing/blinking/flickering lights? 1 Answer

Can I make animations snap to a frame? 1 Answer

Raycast with SendMessage can send Parameter, but how to send a variable ? 2 Answers

Headbob when being hit 1 Answer

Hit Marker 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