【问题标题】:How to make an enemy stop chasing the player temporarily如何让敌人暂时停止追击玩家
【发布时间】:2021-11-15 06:59:21
【问题描述】:

我正在开发一款 FPS 游戏,其中包含了敌人死亡和受伤的动画。

一般来说,敌人是在追着玩家(奔跑)。万一被击中,我想暂时停止追击玩家(停留在同一点的时间应该等于“伤害”动画的持续时间),因为目前,敌人作为受伤,身体仍然由于 NavMeshAgent 向玩家移动。

下面是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);
        }
    }

我将不胜感激。

【问题讨论】:

  • 以前从未使用过NavMeshAgent,但如果enabled 的行为不符合预期,这可能是由于NavMeshAgent 在新路径重新获得后立即重新激活......我认为我的第二个“ workarround" 将简单地用您当前的位置覆盖destination,并且仅当您想再次重新激活代理时将其设置为player.transform.position

标签: c# unity3d


【解决方案1】:

这是我推荐的一种方法:

    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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多