Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
2
Question by Lanipoop · Oct 23, 2010 at 09:15 AM · screenalphadamageflashred

Screen flashes red when taking damage

How would I make the screen flash red whenever the player takes damage?

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

4 Replies

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

Answer by noradninja · Oct 26, 2010 at 12:02 AM

Fading a GUITexture is pretty easy:

function Fade (start : float, end : float, length : float, currentObject : GameObject) { //define Fade parmeters if (currentObject.guiTexture.color.a == start){

for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) { //for the length of time currentObject.guiTexture.color.a = Mathf.Lerp(start, end, i); //lerp the value of the transparency from the start value to the end value in equal increments yield; currentObject.guiTexture.color.a = end; // ensure the fade is completely finished (because lerp doesn't always end on an exact value) } //end for

} //end if

} //end Fade

function FlashWhenHit (){ Fade (0, 0.8, 0.5, GUITextureobjectname); yield WaitForSeconds (.01); Fade (0.8, 0, 0.5, GUITextureobjectname); }

Then, when wou want you screen to flash, just call FlashWhenHit:

if (Hit){
FlashWhenHit();
}

The above example (in FlashWhenHit) will fade your texture from 100% transparent (invisible) to 80% opaque (just slightly transparent) over 1/2 second. It checks to make sure the texture is 100% transparent before attempting the fade to eliminate visual errors, and at the end ensures it is at exactly 80% opacity. It will then wait 1/100th second, and fade the texture back out to transparent. You can, of course, adjust the starting and ending opacity by changing the start and end values in the function call, as well as how long the fade takes and what object if affects. The WaitForSeconds is in there so the texture will stay at its max opacity momentarily (to make it more visually obvious); the length of time is adjustable there too. Also, if you want the screen to flash a certain number of times, you could use a for loop with a counter that goes to 0 from, say, 3, to get the screen to flash 3 times, etc.

Hope this helps.

Comment
Add comment · Show 8 · 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 noradninja · Oct 26, 2010 at 12:06 AM 0
Share

I suppose you could use $$anonymous$$athf.PingPong to just ramp the opacity up then back down too, I just do it this way because I think it gives me more control over the ti$$anonymous$$g, and I use Fade() to do lots of stuff like fading in and out of black at the start of a level or when a player dies, fading the UI in or out when a player touches the screen or moves the mouse, etc.

avatar image Lanipoop · Oct 26, 2010 at 02:58 AM 0
Share

WOW that worked beautifully! I got a couple errors, but I was able to fix it :) Thank you so much!!!

avatar image Lanipoop · Oct 26, 2010 at 03:12 AM 0
Share

Actually I am having a problem with the 2nd fade in FlashWhenHit().. The transparency only fades back to 0 when I get hit continuously. But if I only get hit once, it'll only do the first fade in FlashWhenHit() so my screen will stay red. How do I fix this?

avatar image Lanipoop · Oct 26, 2010 at 03:14 AM 0
Share

Never $$anonymous$$d, I fixed it. Thanks again!!!!!!!!

avatar image noradninja · Oct 27, 2010 at 12:51 PM 0
Share

No problem. Incidentally, were your erros something in the code I gave you or was it your implementation? I like code review and if I made a mistake, I'm curious what it was :)

Show more comments
avatar image
2

Answer by noradninja · Oct 24, 2010 at 02:43 PM

A simple method:

Make fullscreen GUITexture, and set it to red. Set the opacity to 0%, and attach it to a separate camera on a layer above the main camera (so it is drawn on top of everything). Make sure you set the clear flag on this camera to Don't clear.

Set up your logic so whenever your player is hit, a variable is set to true momentarily.

Attach a script to the GUITexture that looks at the state of the hit variable, and if it is true, set the opacity of the GUITexture to 80% for a few frames and then back to 0% (this will give you the flash but also allow the player to still see the action on screen).

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 Lanipoop · Oct 25, 2010 at 12:05 AM 0
Share

Thanks! Would you be able to give me an example of some pseudo-code?

avatar image Lanipoop · Oct 25, 2010 at 12:38 AM 0
Share

I'm not sure how I would do the code for increasing the opacity for just a few frames and have it go back to 0.

avatar image
0
Wiki

Answer by Billu · Apr 01, 2015 at 04:39 PM

To show the red screen continuously instead of just instantly (for example when you are inside the fire zone and receives consistent burning damage), the fading of the opacity should be put in the update() function and changes in every frame. The following code may help in this situation.

 bool increment = true;
     float len = 0.5f;
     float opac = 0.4f;
     bool alarm;
     
     // Update is called once per frame
     void Update () {
 
         if (alarm)
         {        
             aColor = gameObject.guiTexture.color;
             if (increment == true) 
             {    
                 aColor.a += opac*Time.deltaTime/len;
 
             } else
             {
                 aColor.a -= opac*Time.deltaTime/len;
             }
 
             if (aColor.a > opac) 
             {
                 increment = false;
             } else if (aColor.a < 0f)
             {
                 increment = true;
             }
 
             gameObject.guiTexture.color = aColor;
         }
     }

When you want to start the consistent flashing screen, just set the variable 'alarm' to true. And whenever you want to stop this flashing, set the 'alarm' to be false, and also reset the opacity to 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 hotlabs · Oct 24, 2021 at 04:14 PM

Posted a possible solution here: https://answers.unity.com/questions/1286867/screen-flashes-red-on-damage.html?childToView=1867776#answer-1867776

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Red flash for each network player when he gets hit 1 Answer

Screen flashes red on damage? 4 Answers

Screen Flash When Hit -- Error Help From Pervious Question's Answer 1 Answer

Making screen flash texture when player is damaged 0 Answers

Fog working on my machine but not on others? /how to flash a colour on the screen 0 Answers


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