Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Respawn point, multiple calls. Help.?

Discussion in 'Scripting' started by Geodesic_Void, May 15, 2024.

  1. Geodesic_Void

    Geodesic_Void

    Joined:
    Apr 4, 2024
    Posts:
    26
    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.
    Code (CSharp):
    1.    public void TakeDamage(int amount)
    2.    { if(canGetHit == true)
    3.        {
    4.            Debug.Log("GotHit");
    5.            isAttacked = true;
    6.            currentHealth -= amount;
    7.            ani.SetTrigger("isHit");
    8.            StartCoroutine(hitKnockback());
    9.          
    10.        }
    11.      
    12.    
    13.        if(currentHealth <= 0)
    14.        {
    15.            StartCoroutine("DeathSequence");
    16.        }
    17.      
    18.  
    19.  
    20.    }
    21. private IEnumerator DeathSequence()
    22. {
    23.     ani.SetBool("IsDead", true);
    24.     canGetHit = false;
    25.     canMove = false;
    26.     yield return new WaitForSeconds(2f);
    27.    
    28.     transform.position = respawnPoint.transform.position;
    29.    
    30.     Debug.Log("RESPAWN");
    31.    
    32.     currentHealth = maxHealth;
    33.     ani.SetBool("IsDead", false);
    34.     canGetHit = true;
    35.     canMove = true;
    36.  
    37. }
    38.  
     
  2. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    212
    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.

    Code (CSharp):
    1. public void TakeDamage(int amount)
    2. {
    3.     if (!canGetHit)
    4.         return;
    5.  
    6.     Debug.Log("GotHit");
    7.     isAttacked = true;
    8.     currentHealth -= amount;
    9.     ani.SetTrigger("isHit");
    10.     StartCoroutine(hitKnockback());
    11.  
    12.     if (currentHealth <= 0)
    13.     {
    14.         StartCoroutine("DeathSequence");
    15.     }
    16. }
     
    Ryiah likes this.
  3. Geodesic_Void

    Geodesic_Void

    Joined:
    Apr 4, 2024
    Posts:
    26
    Gotcha.! thank you.
     
  4. Geodesic_Void

    Geodesic_Void

    Joined:
    Apr 4, 2024
    Posts:
    26
    works perfect, thank you again.