Respawn point, multiple calls. Help.?

Just a simple re spawn for testing death animations but I noticed when the Death Sequence is called the re spawn goes fine, but then the player is re spawned, another time, then one more. 3 in total then stops, the first is fluid and then it puts him back to the re spawn point again with like… a flicker twice then stops.

Ill insert the relevant code.

   public void TakeDamage(int amount)
   { if(canGetHit == true)
       {
           Debug.Log("GotHit");
           isAttacked = true;
           currentHealth -= amount;
           ani.SetTrigger("isHit");
           StartCoroutine(hitKnockback());
          
       }
      
    
       if(currentHealth <= 0)
       {
           StartCoroutine("DeathSequence");
       }
     


   }
private IEnumerator DeathSequence()
{
    ani.SetBool("IsDead", true);
    canGetHit = false; 
    canMove = false;
    yield return new WaitForSeconds(2f);
   
    transform.position = respawnPoint.transform.position;
   
    Debug.Log("RESPAWN");
   
    currentHealth = maxHealth;
    ani.SetBool("IsDead", false);
    canGetHit = true;
    canMove = true;

}

In your TakeDamage function, the check for “currentHealth <= 0” will happen even if canGetHit is equal to false. This means that while the player is stuck in the death animation for 2 seconds, it can still get hit and start another copy of the DeathSequence coroutine. I recommend changing the TakeDamage function to return immediately if canGetHit is false.

public void TakeDamage(int amount)
{
    if (!canGetHit)
        return;

    Debug.Log("GotHit");
    isAttacked = true;
    currentHealth -= amount;
    ani.SetTrigger("isHit");
    StartCoroutine(hitKnockback());

    if (currentHealth <= 0)
    {
        StartCoroutine("DeathSequence");
    }
}
1 Like

Gotcha.! thank you.

works perfect, thank you again.