No announcement yet.

How do I make the AI stop chasing the player?

  • Filter
  • Time
  • Show
Clear All
new posts

    How do I make the AI stop chasing the player?

    Right now I have my AI set up where when they enter aggro mode based
    on pawn sensing or player attacking from behind. Once in aggro mode
    they follow the player and then attack until either the AI or player dies.

    I attached screenshots of my controller and behaviour tree.

    I want to have the AI stop chasing the player and return to
    his patrol if the distance between the AI and the player is
    greater than 1000cm. But I want the AI to still be able to
    go back into Aggro mode if the player comes within sight
    or attacks again.

    I was thinking maybe a timer to be running while the
    AI is in pursuit of the player to keep checking the distance.
    And then another decorator, isPlayerStillInSight to determine
    what to do.

    But I would love to hear suggestions.

    Does anyone know, What is the best way to set this up?

    Click image for larger version

Name:	Screen Shot 2016-02-06 at 1.04.23 AM.png
Views:	1
Size:	773.8 KB
ID:	1171627

    Click image for larger version

Name:	Screen Shot 2016-02-06 at 1.10.57 AM.png
Views:	1
Size:	663.2 KB
ID:	1171628
    About Me

    Here's one way to approach it, psuedo-BehaviorTree of the patrol state, then goes into a get closer state (if within FOV but not engagement distance), and then into the attack state if within FOV + engagement distance.

    Click image for larger version

Name:	Patrol.jpg
Views:	1
Size:	224.5 KB
ID:	1099960



      I have 3 questions.
      1. There is some green text on the selector "Within FOV + DistCheck". Is that a decorator, a function call, something else, or just a comment/tag?
      2. You have decorators on each sequence "state ==" for various states. Are these decorators based on the values of Bools being set as keys or are they variables of a different type?
      3. In your example I can't see how the AI will get out of the state "detected". How will the decorator variables be reset if player moves out of range?
      Last edited by Colorado; 02-06-2016, 10:28 AM.
      About Me


        1. It's a service, it would just constantly get forward angles on the AI and seeing if it Spotted the Player within FOV with a clear line of sight via a trace, store that origin in the location vector BlackBoard key and tell the AI to go there. If the Player is within engagement distance then Player is Detected so run closer to him and with a delay for aiming attack him. Otherwise go back to Patrol if none of the above.

        You'll have to mix in the distance to stop chasing (or even spot the Player at all) how you see fit within those conditions for updating which state to change to.

        2. "state" would ideally just be an enum BlackBoard key with Patrol, Spotted, Detected entries. The service above would be constantly updating the "state" variable, and the decorators on each sequence checks it.

        3. If the Player runs around a corner the AI will go to the last Spotted origin (of the Player), then look around for a few seconds and determine whether to go back to Patrol or continue chasing in Spotted or attack if Detected.
        Last edited by SparkyMcSparks; 02-06-2016, 12:09 PM.



          I have been working on this all day. I am attaching what I have at the bottom
          of the post.

          It works but I don't really like it. And it doesn't really do exactly what I want.

          He chases the player on site, and then gets close to him and attacks
          after his attack he pauses to give the player a little time to run away
          if the player gets far enough he stops attacking.

          I like your solution much better because with my solution, he needs to
          reach the player at least once and attack and the player needs to
          run away exactly after the attack. It is not as good as your solution.
          Your solution achieves what I want, if you run far enough he stops
          chasing. That is what I want. I tried to do it with Function calls
          after wasting about 10 hours I see why I need that Service.

          I never set up a service on the behavior tree
          and I don't know how to set up an enum variable

          Can you post the code for the service and setting the enum?
          or point me to somewhere I can learn how to do that?


          Click image for larger version

Name:	Screen Shot 2016-02-07 at 1.40.51 AM.png
Views:	1
Size:	880.5 KB
ID:	1099989
          Click image for larger version

Name:	Screen Shot 2016-02-07 at 1.44.59 AM.png
Views:	1
Size:	699.0 KB
ID:	1099990
          Click image for larger version

Name:	HighresScreenshot00006.png
Views:	1
Size:	478.3 KB
ID:	1099991
          Last edited by Colorado; 02-06-2016, 08:09 PM.
          About Me


            Was going to do a video but it's 2:30AM so I'm going to knock out in a second, hopefully these screenshots help give you some direction though!

            Make an Enumeration, I called EBotState
            Click image for larger version

Name:	Enum.png
Views:	1
Size:	150.9 KB
ID:	1100003

            Open EBotState, click New to add new entries Patrol, Spotted, Detected
            Click image for larger version

Name:	EnumEntries.png
Views:	1
Size:	32.4 KB
ID:	1100004

            On your AI character add an enum variable BotState and point the Variable Type to EBotState you made earlier.
            Click image for larger version

Name:	EnumCharacter.png
Views:	1
Size:	27.7 KB
ID:	1100007

            Then switch over to your BlackBoard create a new enum variable, I called it BotState too. Point the Enum Type to EBotState you made earlier
            Click image for larger version

Name:	EnumBlackBoardKey.png
Views:	1
Size:	65.5 KB
ID:	1100005

            The top left is how to set the BotState on the AI character, and below that is how to use a switch if you want to do some script stuff based on what the current value of the BotState variable is.

            To the right is how to set the BlackBoard BotState variable, which is what you'll use on the Decorators to check which sequence to go down. Basically the Key Name should equal the name of the Blackboard key we want to change, which is BotState
            Click image for larger version

Name:	EnumSetSwitch.png
Views:	1
Size:	205.8 KB
ID:	1100012

            And this is how you check the BlackBoard BotState on the Decorator
            Click image for larger version

Name:	EnumDecorator.png
Views:	1
Size:	36.6 KB
ID:	1100006
            Last edited by SparkyMcSparks; 02-07-2016, 07:32 AM.


              Thank you SO MUCH!!!!!!
              I am going to try it tonight.
              About Me


                Thank you SparkyMcSparks for your help,

                After spending my entire weekend on something I thought would take an hour.
                I got depressed about this and avoided working on it for a day or two.
                Anyway, I rolled up my sleeves and tried again today and was finally able to get it to work exactly like I wanted.
                thanks mostly to your help.

                Now the enemy will start to go after the player based on PawnSensing
                he will continue to purse the player as long as the player has not moved out of range
                if he moves out of range he goes back to patrol.

                I followed your guidance. The service and the Enum were great suggestions.

                The only thing I deviated on was sending the blackboard a 'Player spotted location' and having AI
                chase the location you were at. I had done a BT that did that in a different project and I didn't like
                it at all. They were chasing shadows and easy to avoid. My goal was to have him go right after the
                player unless the player really runs away very far. (and to do this without giving every bot a leash or
                a predefined zone)

                The service calls the function that checks the distance, sets isInRange, and then calls a function setBotState
                (which I posted a screenshot of)
                setBotState will do nothing if he is Patrolling, but if he is not patrolling, then it checks if he is still in range.

                Here is what my final implementation looked like

                Click image for larger version

Name:	Screen Shot 2016-02-10 at 2.24.40 AM.png
Views:	1
Size:	879.8 KB
ID:	1100177
                Click image for larger version

Name:	Screen Shot 2016-02-10 at 2.31.05 AM.png
Views:	1
Size:	713.4 KB
ID:	1100178
                Last edited by Colorado; 02-09-2016, 08:48 PM.
                About Me