Makes the sprite alpha flicker.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SpriteAlphaFlicker : MonoBehaviour | |
{ | |
public float flickersPerSecond = 15f; | |
public float flickerRangeMin = -0.1f; | |
public float flickerRangeMax = 0.1f; | |
private SpriteRenderer spriteRenderer; | |
private float alpha; | |
private float time; | |
private void Start() | |
{ | |
spriteRenderer = GetComponent<SpriteRenderer>(); | |
alpha = spriteRenderer.color.a; | |
} | |
private void Update() | |
{ | |
if (GetMillisecs() > 1000f / flickersPerSecond) | |
{ | |
float newAlpha = alpha + Random.Range(flickerRangeMin, flickerRangeMax); | |
Color newColor = spriteRenderer.color; | |
newColor.a = newAlpha; | |
spriteRenderer.color = newColor; | |
ResetTime(); | |
} | |
} | |
private float GetMillisecs() | |
{ | |
return (Time.realtimeSinceStartup - time) * 1000; | |
} | |
public void ResetTime() | |
{ | |
time = Time.realtimeSinceStartup; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment