微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

敌方 AI 与 NavMesh 无法正常工作

如何解决敌方 AI 与 NavMesh 无法正常工作

我一直在尝试创建一个让敌人在某些点之间巡逻的 AI,当在一定范围内发现玩家时,敌人会停止巡逻并跟随玩家。 问题是,如果我只有敌人跟随玩家,那么与巡逻完全相同,但两者一起似乎不起作用。敌人的行为很奇怪。

我的代码


    public void Start()
    {
        _navMeshAgent = this.GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();

        if (_navMeshAgent == null)
        {
            Debug.LogError("not attached to " + gameObject.name);
        }

        else
        {
            if (_patrolPoints != null && _patrolPoints.Count >= 2)
            {
                _currentPatrolIndex = 0;
                anim.SetBool("idle",true);
                SetDestination();
            }
            else
            {
                Debug.Log("Insufficient patrol points.");
            }

        }
    }


    void Update()
    {



        if (player == null)
        {

            player = GameObject.Find("Player Character").GetComponent<Transform>();

        }

        if (Vector3.distance(player.transform.position,this.transform.position) < 10)
        {
          
            chacePlayer();
        }
        else
        {
            patrol();
        }
       

        bool patrol()
        {
            Debug.Log("patrolling");
            //Check if we're close to the destination.
            if (_travelling && _navMeshAgent.remainingdistance <= 1.0f)
            {
                _travelling = false;
                anim.SetBool("idle",false);
                anim.SetBool("move",true);



                //If we're going to wait,then wait dumbass!
                if (_patrolWaiting)
                {
                    _waiting = true;
                    _waitTimer = 0f;
                    anim.SetBool("idle",true);
                    anim.SetBool("move",false);

                }
                else
                {

                    ChangePatrolPoint();
                    SetDestination();
                }
            }

            //Instead if we're waiting...
            if (_waiting)
            {
                _waitTimer += Time.deltaTime;
                if (_waitTimer >= _totalWaitingTime)
                {
                    _waiting = false;
                    anim.SetBool("move",true);
                    anim.SetBool("idle",false);



                    ChangePatrolPoint();
                    SetDestination();
                }
            }
            return true;

        }

        
    }


  

    private void SetDestination()
    {
        if (_patrolPoints != null)
        {
            Vector3 targetVector = _patrolPoints[_currentPatrolIndex].transform.position;

            _navMeshAgent.SetDestination(targetVector);
            _travelling = true;

            ////anim.SetBool("idle",false);
            ////anim.SetBool("move",true);


        }
    }

    //Selects a new patrol point in the available list,but
    //also with a small probability allows for us to move forward or backwards.

    private void ChangePatrolPoint()
    {
        //Unity generate random number between 0 and 1
        if (UnityEngine.Random.Range(0f,1f) <= _switchProbability)
        {
            //decides if go forward or backwards: whatever the value,make the oposite
            _patrolForward = !_patrolForward;
        }

        if (_patrolForward)
        {
            //if the patrolpoint exceedes patrolpoints.count,go backs to zero
            _currentPatrolIndex = (_currentPatrolIndex + 1) % _patrolPoints.Count;
        }
        else
        {
            if (--_currentPatrolIndex < 0)
            {
                _currentPatrolIndex = _patrolPoints.Count - 1;
            }
        }
    }


    void chacePlayer()
    {
    



            Vector3 direction = player.transform.position - this.transform.position;

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,Quaternion.LookRotation(direction),0.1f);
        direction.y = 0;
            if (direction.magnitude > 2)
            {
                this.transform.Translate(0,0.05f);
            Debug.Log("chacing");


            }


        
    }

一开始我以为是因为代码同时运行,所以我放了一个Debug看看是不是这样,但是每次敌人跟着玩家时巡逻队的调试都会停止。 有人可以帮我吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。