• 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 /
  • Help Room /
avatar image
2
Question by DiscoFever · Sep 30, 2015 at 02:21 PM · 2dspriterendererblinking

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 ?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
-2

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

Comment
Add comment · Show 1 · 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 DiscoFever · Sep 30, 2015 at 05:22 PM 0
Share

If wish it was that simple; nope.

avatar image
16

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

Comment
Add comment · Show 5 · 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 wmccullough · Nov 27, 2017 at 03:33 AM 1
Share

This is awesome, helped me to quickly implement a flash!

avatar image KUFgoddess wmccullough · Jan 16, 2018 at 11:46 AM 0
Share

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.

avatar image AlonsOh · Aug 04, 2018 at 02:32 AM 0
Share

Wow! Thanks a lot for this shader! I'm a total noob on the shaders subject, and this works wonders, thank you! :D

avatar image Verdemis · Sep 27, 2018 at 07:58 AM 0
Share

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.

avatar image CaptainAfrica · Mar 13, 2021 at 08:20 AM 0
Share

@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!

avatar image
0

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Making an GameObject a child or another GameObject makes sprites dissapear 0 Answers

Batching Sprites in 2D - "Objects have different MaterialPropertyBlock set" 1 Answer

New Unity3d 2d sprite Renderer Feature not available 1 Answer

Set SpriteRenderer of Child Objects on and off 0 Answers

Need to make a fake 2D shadow mask shader 0 Answers


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