Skip to content

Instantly share code, notes, and snippets.

@iamgabrielma
Created May 29, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamgabrielma/12efa6cd48e8af90527d07d6da4056ed to your computer and use it in GitHub Desktop.
Save iamgabrielma/12efa6cd48e8af90527d07d6da4056ed to your computer and use it in GitHub Desktop.
Modifies the alpha value of a GameObject following a pulse rhythm, creating the effect of appear/disappear gradually.
IEnumerator MapColorPulse()
{
bool isLimitReached = false;
map = GameObject.Find("Map");
SpriteRenderer mapRendererComponent = map.GetComponent<SpriteRenderer>();
initialMapAlphaValue = mapRendererComponent.color.a; // 1
//Debug.Log("Initial alpha: " + initialMapAlphaValue); // 1
// Test: Using Find() + new Color for setting up the alpha value:
while (true)
{
//Debug.Log("Initial alpha: " + initialMapAlphaValue);
if (initialMapAlphaValue > 0.1 && isLimitReached == false) // decrease alpha
{
initialMapAlphaValue = initialMapAlphaValue - 0.1f;
//Debug.Log("Is Zero reached?" + isZeroReached);
if (initialMapAlphaValue < 0.1)
{
isLimitReached = true;
}
}
else if (initialMapAlphaValue < 1 && isLimitReached == true) // increase alpha
{
initialMapAlphaValue = initialMapAlphaValue + 0.1f;
isLimitReached = true;
//Debug.Log("Is Zero reached?" + isZeroReached);
}
else if (initialMapAlphaValue >= 1) // back to decrease as initialvalue will be higher than zero but zeroreached will be false.
{
isLimitReached = false;
//Debug.Log("Is Zero reached?" + isZeroReached);
}
float newMapAlphaValue = initialMapAlphaValue;
mapRendererComponent.color = new Color(1, 1, 1, newMapAlphaValue);
//Debug.Log("New alpha : " + newMapAlphaValue); // Bug: Siempre 0.9
yield return new WaitForSeconds(1f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment