Sprite Flash in Unity

Share on facebook
Share on google
Share on twitter
Share on linkedin

Flashing effect is commonly used in Shoot ‘Em Up and Beat ‘Em Up games. It gives clue to players that those enemies or objects can be damaged and destroyed. I used this technique in the development of Burst Fighter. (The spaceships image above is from Burst Fighter)

The implementation in Unity is actually quite simple. We just need to change the default sprite shader with our custom flash shader that we will be creating right now. I won’t explain the basic of shader programming. You can find it on the internet easily. I personally recommend reading this article by Alan Zucconi before diving into shader programming.

The shader will look like this:

Shader "Sprites/Sprite Flash"
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
		_FlashColor ("Flash Color", Color) = (1,1,1,1)
		_FlashAmount ("Flash Amount", Range (0,1)) = 0


		Cull Off
		Lighting Off
		ZWrite Off
		Blend One OneMinusSrcAlpha

			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile _ PIXELSNAP_ON
			#include "UnityCG.cginc"
			struct appdata_t
				float4 vertex   : POSITION;
				float4 color    : COLOR;
				float2 texcoord : TEXCOORD0;

			struct v2f
				float4 vertex   : SV_POSITION;
				fixed4 color    : COLOR;
				float2 texcoord  : TEXCOORD0;
			fixed4 _Color;
			fixed4 _FlashColor;

			v2f vert(appdata_t IN)
				v2f OUT;
				OUT.vertex = UnityObjectToClipPos(IN.vertex);
				OUT.texcoord = IN.texcoord;
				OUT.color = IN.color * _Color;
				#ifdef PIXELSNAP_ON
				OUT.vertex = UnityPixelSnap (OUT.vertex);

				return OUT;

			sampler2D _MainTex;
			sampler2D _AlphaTex;
			float _AlphaSplitEnabled;
			float _FlashAmount;

			fixed4 SampleSpriteTexture (float2 uv)
				fixed4 color = tex2D (_MainTex, uv);

				if (_AlphaSplitEnabled)
					color.a = tex2D (_AlphaTex, uv).r;

				return color;

			fixed4 frag(v2f IN) : SV_Target
				fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
				c.rgb = lerp(c.rgb, _FlashColor.rgb, _FlashAmount);
				c.rgb *= c.a;
				return c;

Look closer to this line.

c.rgb = lerp(c.rgb, _FlashColor.rgb, _FlashAmount);

The idea is we want to lerp our existing sprite color to _FlashColor based on _FlashAmount. Now, create a new material with this shader and attach it to your sprite. Next step is creating a C# script to interact with _FlashAmount value.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteFlash : MonoBehaviour {

	public float flashSpeed;

	Material mat;

	bool isFlashing = false;

	private void Awake() {
		mat = GetComponent().material;

	private void Update() {

	private void Flash(){

	IEnumerator DoFlash(){
		isFlashing = false;
		yield return new WaitForEndOfFrame();
		isFlashing = true;
		float flash = 1f;
		while (isFlashing && flash >=0)
			flash -= Time.deltaTime * flashSpeed;
			mat.SetFloat("_FlashAmount", flash);
			yield return null;
		isFlashing = false;

That’s it! Now start play mode and press Space bar to flash the sprite. You can download the Unity project here: Unity Sprite Flash

this article is contributed by: claygamestudio.com

More to explorer

Post Global Game Jam 2020

Halo ! apa kabar semuanya ! Apa kalian masih MANTAP ? ? admin bakalan nge review untuk event Global Game Jam Ngalam

Global Game Jam 2020

Global Game Jam 2020 di Malang berjalan dengan luar biasa! Terima kasih ya yang sudah ikutan. Semangatnya sungguh tiada duanya! Bahkan ada

Leave a Comment

Your email address will not be published. Required fields are marked *