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 :slight_smile:

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

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

4 Answers

4

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

Please just type that on the top address bar of the file explorer. it will go directly there. ProgramData is a hidden folder in windows.

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.

@Komayo Windows cant find C:\ProgramData\Unity2 (with and withought the space)

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) Unity - Scripting API: GameObject.SetActive

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 :wink:

You have a number 2 added to the end.. its just "C:\ProgramData\Unity"

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