- Home /
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 :)
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?
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 ;)
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 :)
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?
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.
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 ;)
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");
}
}
}
Your answer
Follow this Question
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