TO SQUASH: Fixed shark death.

This commit is contained in:
2026-08-19 18:11:10 -04:00
parent 165f61afbb
commit 71bb746e8a
+45 -11
View File
@@ -153,18 +153,29 @@ public class Shark : MonoBehaviour
animator.SetBool("canMove", true); animator.SetBool("canMove", true);
} }
// Do stuff only if the spawn animation has finished. if (killed)
if (spawnFinished)
{ {
if (killed && !isDead) if (!isDead)
{ {
isDead = true; isDead = true;
animator.SetTrigger("killed");
if (animator != null)
{
// Keep death state stable even if Any State transitions exist for movement.
animator.SetBool("canMove", false);
animator.Play("SharkDeath", 0, 0.0f);
}
audioSource.PlayOneShot(deathSound); audioSource.PlayOneShot(deathSound);
player.score += sharkKillPoints; player.score += sharkKillPoints;
StartCoroutine(DestroyShark()); StartCoroutine(DestroyShark());
} }
else
return;
}
// Do stuff only if the spawn animation has finished.
if (spawnFinished)
{ {
UpdateMovement(); UpdateMovement();
animator.SetInteger("angle", angle); animator.SetInteger("angle", angle);
@@ -189,7 +200,6 @@ public class Shark : MonoBehaviour
} }
} }
} }
}
private void UpdateMovement() private void UpdateMovement()
{ {
@@ -281,11 +291,22 @@ public class Shark : MonoBehaviour
private IEnumerator DestroyShark() private IEnumerator DestroyShark()
{ {
// Wait two loops of the death animation before destroying the shark object. // Wait until the death state has looped twice, with a timeout fallback so this cannot hang forever.
yield return new WaitUntil( float elapsed = 0.0f;
() => animator.GetCurrentAnimatorStateInfo(0).IsName("SharkDeath") && const float maxWaitSeconds = 5.0f;
animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 2.0f
); while (elapsed < maxWaitSeconds)
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("SharkDeath") && stateInfo.normalizedTime >= 2.0f)
{
break;
}
elapsed += Time.deltaTime;
yield return null;
}
Destroy(gameObject); Destroy(gameObject);
} }
@@ -326,6 +347,19 @@ public class Shark : MonoBehaviour
public void KillShark() public void KillShark()
{ {
if (killed)
{
return;
}
killed = true; killed = true;
if (animator != null)
{
// Stop movement-related Any State transitions, then force the death animation.
animator.SetBool("canMove", false);
animator.ResetTrigger("killed");
animator.Play("SharkDeath", 0, 0.0f);
}
} }
} }