0

I been trying to create an AI for the enemy to patrol between some points and, when spot player in a certain range, the enemy stop the patrol and follows the player. The thing is, if I only have the enemy to follow the player, works perfectly the same happens with just the patrol, but both together doesn't seem to work. The enemy acts in a weird way.

My code:


    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, 0.05f);
            Debug.Log("chacing");


            }


        
    }

At first I thought it was because the code was running both together so I put a Debug to see if it was that, but the debug of the patrol stops every time the enemy is following the player. Can someone please help me?

fss25
  • 13
  • 4
  • Whenever I implement AI that changes what it is doing depending on the situation, I would use a [State Machine](https://forum.unity.com/threads/c-proper-state-machine.380612/). Try to refactor your code so the AI runs through states. The base state is clearly patrol, but then you exit the patrol state to chase state if the player is in view. You can also add more states as well which makes expanding your AI later easier. I also find using state machines makes it far easier to debug code as it is in subsections. – TEEBQNE Apr 22 '21 at 19:49
  • I would also add to the question how the AI is acting weird. Possibly attach a video link or a gif. It goes a long way when asking for help. – TEEBQNE Apr 22 '21 at 19:52

0 Answers0