Make a sprite blink white when hit
Currently to make a sprite 'blink' i have a 2 frames animation; one color, one white. I was wondering if there was a better way of doing it, like having some kind of super code on the sprite renderer to apply; best would be programmatically.
Any ideas ?
Answer by GnomenxD · Feb 07, 2016 at 10:39 PM
Hey dude, if you're still looking for it here's a little help.
Make a new Shader with the following. Then make a material that you apply to the SpriteRenderer. Then you can use SpriteRenderer.material.SetFloat("_FlashAmount", amount from 0 to 1) to make it flash up. Then just set it back to 0 and it looks normals again. Hope you can use it, if you haven't already figured it out by yourself.
Shader "Sprites/Diffuse Flash"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0
_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert alpha vertex:vert
#pragma multi_compile DUMMY PIXELSNAP_ON
sampler2D _MainTex;
fixed4 _Color;
float _FlashAmount,_SelfIllum;
struct Input
{
float2 uv_MainTex;
fixed4 color;
};
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
v.normal = float3(0,0,-1);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount);
o.Emission = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount) * _SelfIllum;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Regards
This is awesome, helped me to quickly implement a flash!
Hello i was just wondering where do you put this part? SpriteRenderer.material.SetFloat("_FlashAmount", 0, 1);
does this go on an enemy script or in the shader and if the shader where if you would be so kind to let me know.
Wow! Thanks a lot for this shader! I'm a total noob on the shaders subject, and this works wonders, thank you! :D
That's really cool. But I have one question... is there a way to change the flash color on runtime. I don't have much practice with shaders until now. I tried it with
sRenderer.material.SetColor("_Color", Color.red);
or
sRenderer.color = Color.red
But it doesnt work.
@GnomenxD is there any way to make the flash amount smoothly transition? I made it .8 in my script but unity only allowed 1 or 0 as values.
EDIT: Figured out that you needed to write f after the numbers in order to make it partially shaded white to set it as a float rather than an integer. The Shader script still works fine!
Answer by meebou · Mar 29, 2019 at 08:12 AM
The line o.Emission = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount) * _SelfIllum;
speaks for itself :-) "float3 1,1,1 is the color in RGB" so if you want for example red you write
o.Emission = lerp(c.rgb,float3(1.0,0.0,0.0),_FlashAmount) * _SelfIllum;
ok this post is 3-4 years old :D
Answer by bubzy · Sep 30, 2015 at 05:12 PM
http://docs.unity3d.com/ScriptReference/SpriteRenderer-color.html
something like
SpriteRenderer tempSprite = gameObject.GetComponent<SpriteRenderer>();
tempSprite.color = Color.white;
just a guess. hope this helps :)