IEnumerator KillCharacter() { characterMovement.Kill(); characterMovement.GetCapsuleCollider().enabled = false; // Disabling the collider so when the enemy dies the player can path throgh thier dead bodys and not to have a glitch. animator.SetTrigger(DEATH_TRIGGER); var playerComponent = GetComponent <PlayerControl>(); if (deathSounds.Length != 0) { audioSource.clip = deathSounds[UnityEngine.Random.Range(0, deathSounds.Length)]; audioSource.Play(); } characterMovement.GetNavMeshAgent().speed = 0; //yield return new WaitForSecondsRealtime(audioSource.clip.length); yield return(null); if (playerComponent && playerComponent.isActiveAndEnabled) // relying on Lazy Evaluation (Google if you need help) { FindObjectOfType <SavingWrapper>().DeleteSave(); SceneManager.LoadSceneAsync(0); //SceneManager.LoadScene(0); // TODO check for scene Error } else // Assume is enemy for now, reconsider other NPCs Later { characterMovement.GetNavMeshAgent().speed = 0; //DestroyObject(gameObject, deathVanishSeconds + audioSource.clip.length); //Destroy(gameObject, deathVanishSeconds + audioSource.clip.length); } }
void Update() { distanceToPlayer = Vector3.Distance(player.transform.position, transform.position); WeaponSystem weaponSystem = GetComponent <WeaponSystem>(); // No performance issue currentWeaponRange = weaponSystem.GetCurrentWeapon().GetMaxAttackRange(); bool inWeaponRadius = distanceToPlayer <= currentWeaponRange; bool inChaseRadius = distanceToPlayer > currentWeaponRange && distanceToPlayer <= chaseRadius; bool outsideChaseRadius = distanceToPlayer > chaseRadius; if (inWeaponRadius) { StopAllCoroutines(); state = State.attacking; transform.LookAt(player.gameObject.transform); weaponSystem.AttackTarget(player.gameObject); character.GetNavMeshAgent().Move(Vector3.zero); character.GetNavMeshAgent().velocity = Vector3.zero; } if (outsideChaseRadius) { StopAllCoroutines(); //weaponSystem.StopAttacking(); StartCoroutine(Patrol()); } if (inChaseRadius) { StopAllCoroutines(); weaponSystem.StopAttacking(); StartCoroutine(ChasePlayer()); } }
IEnumerator KillCharacter() { characterMovement.Kill(); animator.SetTrigger(DEATH_TRIGGER); var playerComponent = GetComponent <PlayerControl>(); audioSource.clip = deathSounds[UnityEngine.Random.Range(0, deathSounds.Length)]; audioSource.Play(); characterMovement.GetNavMeshAgent().speed = 0; yield return(new WaitForSecondsRealtime(audioSource.clip.length)); if (playerComponent && playerComponent.isActiveAndEnabled) // relying on Lazy Evaluation (Google if you need help) { SceneManager.LoadScene(0); } else // Assume is enemy for now, reconsider other NPCs Later { characterMovement.GetNavMeshAgent().speed = 0; DestroyObject(gameObject, deathVanishSeconds + audioSource.clip.length); } }