Our team is making a beat 'em up, and I’m trying to make our character flicker once his health hits a certain point. We’ve got the flicker part working correctly, but we can’t get the timing to work properly. We want it to change color only every second (at least until we can actually get it to work, we’ll likely speed it up). This is what we’ve got so far. It makes the character flicker, but he does it constantly, rather than just changing once every second. We tried tweaking it around, but we can’t get it to flicker correctly. I don’t suppose anybody else has done this or knows how to fix it?
public class scriptChangeColor : MonoBehaviour {
SpriteRenderer render;
scriptPlayerStatus status;
// Use this for initialization
void Start ()
{
render = GetComponent <SpriteRenderer>();
status = GetComponent<scriptPlayerStatus> ();
StartCoroutine (Flicker ());
}
// Update is called once per frame
void Update ()
{
//print (Time.time);
StartCoroutine (Flicker ());
}
IEnumerator Flicker()
{
bool changed = false;
//while (status.health <= 20)
//{
if (status.health <= 20)
{
changed = true;
print("true");
yield return new WaitForSeconds (1);
changed = false;
print("false");
yield return new WaitForSeconds (1);
}
if (changed == false)
{
render.color = new Color (0f, 0f, 0f, 1f);
//print ("1");
//print ("2");
}
if (changed == true)
{
render.color = new Color (255f, 255f, 255f, 1f);
//print ("3");
//print ("4");
}
//}
}
}
You’re problem was that you were starting the coroutine ever single frame, but your code also was very confusing.
Here’s a much simpler implementation.
public class scriptChangeColor : MonoBehaviour {
float flickerTime=1f;
SpriteRenderer render;
scriptPlayerStatus status;
// Use this for initialization
void Start ()
{
render = GetComponent <SpriteRenderer>();
status = GetComponent<scriptPlayerStatus> ();
}
IEnumerator Flicker () // Call this when the character is below 20 health
{
while (status.health <= 20)
{
render.color = new Color (255f, 255f, 255f, 1f);
yield return new WaitForSeconds (flickerTime);
render.color = new Color (0f, 0f, 0f, 1f);
yield return new WaitForSeconds (flickerTime);
}
// We are now over 20 health
render.color = new Color (255f, 255f, 255f, 1f);
}
}
You can add this code if you want this script to handle when the flickering occurs
// In Start
StartCoroutine (FlickerManager ());
// Anywhere in the script
IEnumerator FlickerManager () {
while (true) {
if (status.health <= 20)
{
yield return StartCoroutine (Flicker ());
}
yield return null;
}
}
Oh my god, thank you. We had spent a couple hours sitting there and tweaking things around trying to figure out how exactly to make it work correctly, but kept running into the same issue. We just got it too that we made it so that the color it flickers is a public variable so we could change it, and also got it so that we can change the flicker speed for different health thresholds. We wanted to do that originally, but we couldn’t get past the flickering not working correctly. Thank you so much.
Also yeah, sorry about the confusing bit. We sat here for a couple of hours trying out different ways of doing it and didn’t wanna delete what we had that we knew worked, so that’s most of the commented out stuff from testing it and stuff.