Search Unity

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

Discussion in 'Scripting' started by maddieScarbrough, Nov 21, 2017.

  1. maddieScarbrough

    maddieScarbrough

    Joined:
    Sep 7, 2016
    Posts:
    21
    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:
    Code (CSharp):
    1. // How do monsters patrol certain areas?
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(NavMeshAgent2D))]
    5. [RequireComponent(typeof(Rigidbody2D))]
    6. [RequireComponent(typeof(MonsterAI))]
    7. public class Patrol : MonoBehaviour {
    8.  
    9.     [Header("Setting the Path")]
    10.     public Transform[] points;
    11.     public Player[] players;
    12.     private int destPoint = 0;
    13.     private NavMeshAgent2D agent;
    14.  
    15.     [Header("Chasing, Patrolling, or Still?")]
    16.     public string action = "Still";
    17.  
    18.     [Header("Who to Chase?")]
    19.     public int target;
    20.  
    21.     void Start()
    22.     {
    23.         target = Random.Range(0, players.Length);
    24.         agent = GetComponent<NavMeshAgent2D>();
    25.  
    26.         /* Disabling auto-braking allows for continuous movement
    27.            between points (ie, the agent doesn't slow down as it
    28.            approaches a destination point).*/
    29.         agent.autoBraking = false;
    30.         if (action == "Patrolling")
    31.         {
    32.             agent.enabled = true;
    33.             GotoNextPoint();
    34.         }
    35.         else
    36.         {
    37.             agent.enabled = false;
    38.             Chase();
    39.         }
    40.     }
    41.  
    42.     void Chase()
    43.     {
    44.         agent.destination = players[target].transform.position;
    45.     }
    46.  
    47.     void GotoNextPoint() {
    48.         // Returns if no points have been set up or we're not patrolling
    49.         if (points.Length == 0 || action != "Patrolling")
    50.             return;
    51.  
    52.         // Set the agent to go to the currently selected destination.
    53.         agent.destination = points[destPoint].position;
    54.  
    55.         // Choose the next point in the array as the destination,
    56.         // cycling to the start if necessary.
    57.         destPoint = (destPoint + 1) % points.Length;
    58.     }
    59.  
    60.  
    61.     void Update () {
    62.         // Choose the next destination point when the agent gets
    63.         // close to the current one.
    64.         if (!agent.pathPending && agent.remainingDistance < 0.5f && action == "Patrolling")
    65.             GotoNextPoint();
    66.     }
    67. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're not updating the chase destination at all? Is that what you mean?
     
  3. maddieScarbrough

    maddieScarbrough

    Joined:
    Sep 7, 2016
    Posts:
    21
    I am actually updating it here:
    Code (CSharp):
    1. void Start(){...
    2.     ...if (action == "Patrolling")
    3.         {
    4.             agent.enabled = true;
    5.             GotoNextPoint();
    6.         }
    7.         else
    8.         {
    9.             agent.enabled = false;
    10.             Chase();
    11.         }
    12.     }
    13.     void Chase()
    14.     {
    15.         agent.destination = players[target].transform.position;
    16.     }
    It's still stuck though...
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  5. AlfredEnglund

    AlfredEnglund

    Joined:
    Apr 17, 2017
    Posts:
    5
    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!
     
  6. maddieScarbrough

    maddieScarbrough

    Joined:
    Sep 7, 2016
    Posts:
    21
    Yep.
    I have attempted that, but no dice... :( Thank you for the suggestion though!
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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..
     
  8. maddieScarbrough

    maddieScarbrough

    Joined:
    Sep 7, 2016
    Posts:
    21
    Good point. Thank you for pointing that out! :)
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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".
     
    maddieScarbrough likes this.
  10. maddieScarbrough

    maddieScarbrough

    Joined:
    Sep 7, 2016
    Posts:
    21
    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! :)