简体   繁体   English

如何让敌人暂时停止追击玩家

[英]How to make an enemy stop chasing the player temporarily

I am working on an FPS game in which I included the animations for enemy dying and getting hurt.我正在开发一款 FPS 游戏,其中包含敌人死亡和受伤的动画。

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.如果被击中,我想暂时停止追击玩家(停留在同一点的时间应该等于“受伤”动画的持续时间),因为目前敌人表现得像受到了伤害,身体还在由于 NavMeshAgent 向玩家移动。

Below is the Update() and TakeDamage() method:下面是 Update() 和 TakeDamage() 方法:

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.我将不胜感激任何帮助。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM