I might have gone a little overboard with my solution

Instead of plainly destroying the slime’s gameobject, I implemented a DeathSequence Coroutine that I call in DetectDeath():

        private void DetectDeath()
        {
            if (currentHealth < 0)
            {
                StartCoroutine(nameof(DeathSequence));
            }
        }

        IEnumerator DeathSequence()
        {
            SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
            WaitForEndOfFrame wf = new WaitForEndOfFrame();

            GetComponent<Collider2D>().enabled = false;

            for (int i = 0; i < 60; i++)
            {
                spriteRenderer.enabled = !spriteRenderer.enabled;
                yield return wf;
            }
            Destroy(gameObject);
        }

It’s simple enough but does give some nice little feedback…

Nice. I love coroutines

Isn’t this a little late? I would expect the slime to be dead on 0 health, so <= 0 perhaps?

I think this is assuming 60fps. On high-end computers this is going to be blindingly fast because it’s not running at 60fps. Perhaps you’d want to rather try flashing it 60 times per second by dividing 1 second into 60 parts with 1f / 60f and then waiting for that amount (0.0167f) of seconds

float frameTime = 1f  / 60f; // or float frameTime = 0.0167f;
WaitForSeconds wf = new WaitForSeconds(frameTime);
for (int i = 0; i < 60; i++)
{
    spriteRenderer.enabled = !spriteRenderer.enabled;
    yield return wf;
}

Well, since there’s nothing showing the amount of HP for the slimes (yet), there’s not much of a difference for the player’s experience… :grinning_face_with_smiling_eyes:

It doesn’t have to do so much with how powerful your machine is but rather the capabilities of the GPU and especially the display…

It’s still a very good point, I haven’t checked how it behaves if I switched my display to a higher refresh rate…
Actually the lesson I’m about to start is on adding some death VFX, so it’s about to get replaced now, anyway.

Privacy & Terms