[ISSUE] I need help. I can't figure out what's wrong with my patrol script!

So, I’ve been trying to get my monster AI script to switch between patrolling and chasing; however, they try to chase and they’re stuck on the same path. Here’s the script:

// How do monsters patrol certain areas?
using UnityEngine;

[RequireComponent(typeof(NavMeshAgent2D))]
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(MonsterAI))]
public class Patrol : MonoBehaviour {

    [Header("Setting the Path")]
    public Transform[] points;
    public Player[] players;
    private int destPoint = 0;
    private NavMeshAgent2D agent;

    [Header("Chasing, Patrolling, or Still?")]
    public string action = "Still";

    [Header("Who to Chase?")]
    public int target;

    void Start()
    {
        target = Random.Range(0, players.Length);
        agent = GetComponent<NavMeshAgent2D>();

        /* Disabling auto-braking allows for continuous movement
           between points (ie, the agent doesn't slow down as it
           approaches a destination point).*/
        agent.autoBraking = false;
        if (action == "Patrolling")
        {
            agent.enabled = true;
            GotoNextPoint();
        }
        else
        {
            agent.enabled = false;
            Chase();
        }
    }

    void Chase()
    {
        agent.destination = players[target].transform.position;
    }

    void GotoNextPoint() {
        // Returns if no points have been set up or we're not patrolling
        if (points.Length == 0 || action != "Patrolling")
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 0.5f && action == "Patrolling")
            GotoNextPoint();
    }
}

You’re not updating the chase destination at all? Is that what you mean?

I am actually updating it here:

void Start(){...
    ...if (action == "Patrolling")
        {
            agent.enabled = true;
            GotoNextPoint();
        }
        else
        {
            agent.enabled = false;
            Chase();
        }
    }
    void Chase()
    {
        agent.destination = players[target].transform.position;
    }

It’s still stuck though…

Okay, I guess what I meant was that you’re just setting that once. I thought maybe the player was moving and the AI wasn’t following. Are you saying that even if the player stays still, the AI won’t reach it?

I might be wrong, but you really don’t declare where the monster should go. Maybe google around how to make something similar. Good luck tho!

Yep.

I have attempted that, but no dice… :frowning: Thank you for the suggestion though!

Okay, well there’s something from the beginning I didn’t ask about…Is your state set to Chase from the beginning? There’s no code to change it mid-way, at the moment…

Good point. Thank you for pointing that out! :slight_smile:

First of all, this whole string for the enemy state if incredibly inefficient, use a bool like “chasePlayer” or an enum if you need more states.

why are you setting “agent.enabled = false;” just before telling the agent to give chase?

ignoring that, the code you have should make the agent get to the position that player X was at the time the code was executed.

and unless you change the AI state from outside the script the if statement in the update method will never run, the string is set to “Still”.

1 Like

Thank you. We’ve decided to switch to GameMaker to make it easier to build and prototype. When we switch back (if we do), then I’ll try it! :slight_smile: