Home > Back-end >  How to make an enemy stop chasing the player temporarily
How to make an enemy stop chasing the player temporarily

Time:11-16

I am working on an FPS game in which I included the animations for enemy dying and getting hurt.

Generally, the enemy is chasing the player (running). In case of getting hit, I would like to temporarily stop chasing the player (the time of staying at the same point should be equal to the duration of "hurt" animation) as currently, the enemy acts as being hurt and the body is still moving towards the player due to NavMeshAgent.

Below is the Update() and TakeDamage() method:

void Update()
    {
        GetComponent<NavMeshAgent>().destination = player.transform.position;
        if(GetComponent<NavMeshAgent>().velocity.magnitude > 1)
        {
            enemyAnimator.SetBool("isRunning", true);
        }
        else
        {
            enemyAnimator.SetBool("isRunning", false);
        }
        if (enemyAnimator.GetBool("isHit"))
        {
            enemyAnimator.SetBool("isHit", false);
            GetComponent<NavMeshAgent>().enabled = false;
        }
    }

    public void TakeDamage(float damage)
    {
        health -= damage;
        enemyAnimator.SetBool("isHit", true);
        // after getting hit, the enemy should not move for the time of hit animation
        GetComponent<NavMeshAgent>().enabled = false;
        Debug.Log("Enemy health: "   health);
        if(health <= 0)
        {
            enemyAnimator.SetBool("isDying", true);
            // here also, some time should pass before the enemy object being destroyed
            // Destroy(gameObject);
        }
    }

I would appreciate any help.

CodePudding user response:

This is one way I'd recommend going about it:

    private bool isAlive = true;

    void Update()
    {
      if(isAlive)
      {
        GetComponent<NavMeshAgent>().destination = player.transform.position;

        if (GetComponent<NavMeshAgent>().velocity.magnitude > 1)
        {
            enemyAnimator.SetBool("isRunning", true);
        }
        else
        {
            enemyAnimator.SetBool("isRunning", false);
        }
        if (enemyAnimator.GetBool("isHit"))
        {
            enemyAnimator.SetBool("isHit", false);
        }
      }
    }

    public void TakeDamage(float damage)
    {
        health -= damage;
        if (health <= 0)
        {
            isAlive = false;
            GetComponent<NavMeshAgent>().isStopped = true;
            enemyAnimator.SetBool("isDying", true);
            Destroy(gameObject, YOUR DELAY HERE); //Destroy has a built in optional delay
            Debug.Log("Enemy killed");
            return;
        }
        enemyAnimator.SetBool("isHit", true);
        StartCoroutine(StopOnHit()); //Trigger the coroutine           
        Debug.Log("Enemy health: "   health);
    }

    private IEnumerator StopOnHit()
    {
        GetComponent<NavMeshAgent>().isStopped = true; //Stop the Agent 
        yield return new WaitForSeconds(YOUR DELAY HERE); //Wait a set amount of time before continuing the code 
        GetComponent<NavMeshAgent>().isStopped = false; //Reenable the Agent
    }
  • Related