diff --git a/Assets/Scripts/Game/Shark.cs b/Assets/Scripts/Game/Shark.cs index c7ed521..97fa306 100644 --- a/Assets/Scripts/Game/Shark.cs +++ b/Assets/Scripts/Game/Shark.cs @@ -153,40 +153,50 @@ public class Shark : MonoBehaviour animator.SetBool("canMove", true); } - // Do stuff only if the spawn animation has finished. - if (spawnFinished) + if (killed) { - if (killed && !isDead) + if (!isDead) { 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); player.score += sharkKillPoints; StartCoroutine(DestroyShark()); - } - else + } + + return; + } + + // Do stuff only if the spawn animation has finished. + if (spawnFinished) + { + UpdateMovement(); + animator.SetInteger("angle", angle); + + if (!isAngry && !isTired && Random.value < angryProbability) { - UpdateMovement(); - animator.SetInteger("angle", angle); + isAngry = true; + angryTimer = angryDuration; + audioSource.PlayOneShot(angrySound); + } - if (!isAngry && !isTired && Random.value < angryProbability) - { - isAngry = true; - angryTimer = angryDuration; - audioSource.PlayOneShot(angrySound); - } + if (isAngry && !isTired && angryTimer <= 0f) + { + isAngry = false; + isTired = true; + audioSource.PlayOneShot(tiredSound); + } - if (isAngry && !isTired && angryTimer <= 0f) - { - isAngry = false; - isTired = true; - audioSource.PlayOneShot(tiredSound); - } - - if (isAngry) - { - angryTimer -= Time.fixedDeltaTime; - } + if (isAngry) + { + angryTimer -= Time.fixedDeltaTime; } } } @@ -281,11 +291,22 @@ public class Shark : MonoBehaviour private IEnumerator DestroyShark() { - // Wait two loops of the death animation before destroying the shark object. - yield return new WaitUntil( - () => animator.GetCurrentAnimatorStateInfo(0).IsName("SharkDeath") && - animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 2.0f - ); + // Wait until the death state has looped twice, with a timeout fallback so this cannot hang forever. + float elapsed = 0.0f; + const float maxWaitSeconds = 5.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); } @@ -326,6 +347,19 @@ public class Shark : MonoBehaviour public void KillShark() { + if (killed) + { + return; + } + 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); + } } }