Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Amaresh · May 11, 2014 at 10:08 PM · texturematerialcoloralpha

How to Make a Character Flicker?

Hey Guys! I've made my character a bit transparent (alpha = 0.3f) and red on collision, and it comes back to normal after one second. Before coming back to normal alpha, I want it to flicker between alpha value of 0.3f and 1.0f. You might have seen this effect on many games when the character gets hit by something.

Can someone help me with this? Please Help, ThankYou.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ninjapretzel · May 12, 2014 at 01:31 AM

There would be multiple steps involved. My solution would be to create a new behaviour to handle the flickering. It would track flicker 'ticks' and update the color that is on the material on the same object. Assuming you are using the default Transparent/Diffuse shader, and C#...

Using a timer pattern is pretty easy, though not the most 'effecient'. It's really not to big of a deal for most games.

Pastebin for easier copy: http://pastebin.com/uB3NGfks

Edit: with the way this is designed, you will just need to set the animating property when you want to start/stop the effect.

If you're using the same timer pattern (it would be good practice to use it a bunch, since it's a pretty common pattern, and pretty useful)


 //When taking damage, wherever that is
 flickerTimeout = 3; //flicker for 3 seconds when hit
 
 
 //In Update() somewhere
 flickerTimeout -= Time.deltaTime
 GetComponent<Flicker>().animate = flickerTimeout > 0;

 

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Flicker : MonoBehaviour {
     
     public bool animate = true;
     
     public float tickTime = .1f;     //Time in seconds per 'tick' (.1 = .1sec full alpha, .1sec reduced alpha, .1sec full, etc)
     public float alphaScale = .3f;    //How transparent are the 'faded' ticks?
     
     float timeout = 0;                //timer to keep track of current time this tick.
     float fullAlpha = 1;             //Keep track of full alpha. We will grab this info from the material on start.
     bool full = true;                //Flag to keep track of if we should use the full or reduced alpha
     
     void Start() {
         fullAlpha = renderer.material.color.a;
         timeout = 0;
         full = true;
     }
     
     void Update() {
         Color c = renderer.material.color;
         c.a = fullAlpha;
         
         //Is the effect animating?
         if (animate) { 
             //Accumulate time into the timer
             timeout += Time.deltaTime;
             
             //Process all ticks that would have happened over the deltaTime 
             //(incase of a delay, we don't want it to instantly flip for a few frames)
             while (timeout > tickTime) {
                 timeout -= tickTime;
                 full = !full;
                 //Subtract the time for that tick, and flip the fade flag.
             }
             
             //If we are not full this frame, set the reduced alpha value.
             if (!full) { 
                 c.a *= alphaScale;
             }
             
         }
         
         //This will apply the full alpha if we are not animating,
         //And then partial alpha half the time if we are.
         renderer.material.color = c;
     }
     
 }
 
 
 
Comment
Add comment · Show 4 · 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 Amaresh · May 12, 2014 at 09:21 AM 0
Share

@ninjapretzel its not flickering. ins$$anonymous$$d it is staying at full alpha.

avatar image Amaresh · May 12, 2014 at 10:37 AM 0
Share

Is there a way to use PingPong?

avatar image ninjapretzel · May 12, 2014 at 03:13 PM 0
Share

did you attach the script to the object that also has the mesh renderer attached to it?

Also, are you using a transparent shader? (not a cutout, Transparent/Diffuse or some other)

ping pong could work, but you would also get a smooth transition from high to low, ins$$anonymous$$d of instant. It would create a different effect.

 Color c = renderer.material.color;
 c.a = .3f + $$anonymous$$athf.PingPong(Time.time * 7f, .7f); 
 renderer.material.color = c;
avatar image Amaresh · May 12, 2014 at 03:39 PM 0
Share

@ninjapretzel yes! I've attached the script to the object having mesh renderer. But I'm using this shader.

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.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Blending a Color with a Material Texture 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Bullet Texture , Material and Glow Effect : Unity 1 Answer

Find Rotation of GameObject : Unity 1 Answer


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