Here my final solution (and it works ):
In character.cs:
void Update() {
if (navAgent.remainingDistance > navAgent.stoppingDistance && isAlive) {
Move(navAgent.desiredVelocity);
}
else {
Move(Vector3.zero);
if (!isAlive)
SetDestination(transform.position);
}
}
In HealthSystem:
IEnumerator KillCharacter() {
characterMovement.Kill();
var playerComponent = GetComponent<PlayerControl>();
audioSource.clip = deathSounds[UnityEngine.Random.Range(0, deathSounds.Length)];
audioSource.Play();
animator.SetTrigger(AnimationConstants.DEATH_TRIGGER);
if (playerComponent) {
//to prevent the player move when is dead and the user clicks on terrain
FindObjectOfType<CameraRaycaster>().enabled = false;
}
else {
//to prevent the enemy to follow the player while he is dead
GetComponent<EnemyAI>().enabled = false;
}
yield return new WaitForSecondsRealtime(audioSource.clip.length);
if (playerComponent && playerComponent.isActiveAndEnabled)
SceneManager.LoadScene(0);
else
Destroy(gameObject, deathVanishSeconds);
}
Do you like it @sampattuzzi?
Regards!