I am creating a 2D Platformer game in which if the player falls off the base/ground then I want the player to respawn at the nearest checkpoint. However I want there to be a delay between the player falling off the ground and the respawn. This is the section of my code to achieve this goal:

private void OnTriggerEnter2D(Collider2D collision)
    if (collision.tag.Equals("Kill")) 
    else if(collision.tag.Equals("Checkpoint"))

        Animator flag = collision.GetComponent<Animator>();
        if (!flag.GetBool("hasCrossed"))
            flag.SetBool("hasCrossed", true);
            checkpoint = collision.gameObject.transform.position;

IEnumerator Respawn()
    yield return new WaitForSeconds(respawnDelay);
    transform.position = checkpoint;


This works fine for small respawn delays like 1 or 2 seconds. But on increasing the delay to 5 seconds, the player after appearing at the nearest checkpoint vanishes off in an instant and then after quite a few repetitions the player reappears.

This is for the 2 second time delay This is for the 2 sec time delay

This is for the 5 second time delay This is for the 5 sec time delay

How can I fix this?

  • 3
    \$\begingroup\$ Love the animated screenshots! \$\endgroup\$ – Konerak May 2 '18 at 12:24
  • 1
    \$\begingroup\$ also known as gifs \$\endgroup\$ – user1306322 May 2 '18 at 16:10

This doesn't seem to have anything to do with how you set up the coroutine. What is probably happening is that the player gets a high enough acceleration while falling to glitch past the ground and the fallout collider. When you move the position during the respawn, it will still be falling at the same rate. In your Respawn method, sometime after your wait line, set the velocity back to zero: GetComponent<Rigidbody2D>().velocity = Vector2.zero.

  • 2
    \$\begingroup\$ Gaming bugs are awesome xD \$\endgroup\$ – Tom May 2 '18 at 9:12
  • 11
    \$\begingroup\$ More generally you should reset as much state of your player character as you need to besides position. Like health, MP, ammo, etc. Don't forget to reset enemy enemies as well if it's that kind of game. \$\endgroup\$ – ratchet freak May 2 '18 at 10:40
  • \$\begingroup\$ @Joe but what about frenemies? \$\endgroup\$ – David Starkey May 2 '18 at 15:32
  • 8
    \$\begingroup\$ Related: Unless your game is physics-based, many platformers have a "max fall speed" to allow for more controlled falls (and avoid bugs like this). \$\endgroup\$ – defy May 2 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.