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
0
Question by teopotter88 · Dec 17, 2016 at 10:05 AM · screendamageflash

Screen flashes red on damage?

So I am making a 2D game and I want the screen to flash red when the player gets damaged. How can I do that in C#. I don't know where to start so any help would be great. Thanks :)

Comment
Add comment · Show 4
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 Zaclos · Dec 17, 2016 at 10:21 AM 0
Share

Hi, you can use UI to do that. For exemple : you can create a panel inside your main canvas and put the image color of the panel in red with 50% opacity. In your script you can active this gameObject (with SetActive method) when the player gets damaged for x sec.

I hope my answer will help you ;)

avatar image teopotter88 · Dec 17, 2016 at 10:46 AM 0
Share

So do I just create an empty game object and attach this script or do I use this method on the UI panel?

avatar image Zaclos teopotter88 · Dec 17, 2016 at 11:00 AM 0
Share

I think the easier way is to create a script to manage the red panel gameObject.

First you should create a new script; in my exemple I will call it DamagedEffect.

In your script create a public function who will change state of the panel gameObject (SetActive method) https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Now you can create the panel in your scene : GameObject > UI > Panel Attache this gameObject to the DamagedEffect script.

Now you just need to call the function of DamagedEffect script in your main script ;)

avatar image teopotter88 Zaclos · Dec 17, 2016 at 02:59 PM 0
Share

Thank you so much. What I did was ins$$anonymous$$d of opening an extra script, I implemented the panel code into the main health code. Thank you so much for your help. If you want me to accept it as an answer, please post it as a reply :)

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by teopotter88 · Dec 17, 2016 at 10:24 AM

So do I just create an empty game object and attach this script or do I use this method on the UI panel?

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 markrashera · Dec 18, 2016 at 11:21 AM

         if (getDamage)
         {
             Color Opaque = new Color(1, 1, 1, 1);
             bloodImage.color = Color.Lerp(bloodImage.color, Opaque, 20 * Time.deltaTime);
             if (bloodImage.color.a >= 0.8) //Almost Opaque, close enough
             {
                 getDamage = false;
             }
         }
         if (!getDamage)
         {
             Color Transparent = new Color(1, 1, 1, 0);
             bloodImage.color = Color.Lerp(bloodImage.color, Transparent, 20 * Time.deltaTime);
         }

just set getDamage to true when you get hit.

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 Zaclos · Dec 17, 2016 at 11:14 PM

I think the easier way is to create a script to manage the red panel gameObject.

First you should create a new script; in my exemple I will call it DamagedEffect.

In your script create a public function who will change state of the panel gameObject (SetActive method) https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Now you can create the panel in your scene : GameObject > UI > Panel Attache this gameObject to the DamagedEffect script.

Now you just need to call the function of DamagedEffect script in your main script ;)

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:12 PM

I know this is an old question, but for people out there still looking for a possible solution using Coroutines and LeanTween:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TestCameraFlashDamage : MonoBehaviour {
 
     public KeyCode activationKey;
     public Color targetColor;
     public float duration;
     private GameObject flashView;
 
     void Start () {
 
         // Create Flash Panel
         flashView = new GameObject ();
         flashView.name = "FlashCanvas";
         flashView.AddComponent<Canvas> ();
         Canvas myCanvas = flashView.GetComponent<Canvas> ();
         myCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
         flashView.AddComponent<CanvasScaler> ();
         flashView.AddComponent<GraphicRaycaster> ();
         flashView.transform.SetParent (transform);
         GameObject panel = new GameObject ("Panel");
         panel.AddComponent<CanvasRenderer> ();
         panel.AddComponent<Image> ();
         RectTransform rt = panel.GetComponent<RectTransform> ();
         rt.sizeDelta = new Vector2 (2000, 2000);
         panel.transform.SetParent (myCanvas.transform, false);
         flashView.SetActive (false);
     }
 
     IEnumerator PlayAnimation () {
         Debug.Log ("Show Flash Damage Animation");
         
         if (!flashView.activeSelf) {
             flashView.SetActive (true);
         }
         Image img = flashView.GetComponentInChildren<Image>();
         LeanTween.value (flashView, 1.0f, 0.0f, duration)
             .setOnUpdate ((float val) => {
                 if (img != null) {
                     img.color = new Color (targetColor.r, targetColor.g, targetColor.b, val);
                 }
             })
             .setOnComplete (() => {
                 flashView.SetActive (false);
             });
         return null;
     }
 
     void Update () {
 
         if (Input.GetKeyUp (activationKey)) {
             StartCoroutine ("PlayAnimation");
         }
     }
 }
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

62 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

Related Questions

Screen flashes red when taking damage 3 Answers

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

blink the screen 1 Answer

Locking cursor Issue - Flash 1 Answer

Screen Flash When Hit -- Error Help From Pervious Question's Answer 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