I don’t understand the exact behavior you want, but I think I get the gist of it. However, it seems to me that while it’s not too difficult to achieve what you want to do using plain Blueprints, it is easier to do (and in particular maintain) if you implement it using Behavior Trees. Essentially BTs allow you to structure actions that are spread out over time and might have to be interrupted in a way that’s easier to visualize than relying on plain code. Fortunately the [Behavior Tree Quick Start][1] in the UE documentation does something similar to what you want to do. Since the service in the Quick Start is more complex than it needs to be, and you don’t really need to implement your own Task for moving to a certain location, I’ve redone the project with a simplified service and BT and put it in [my Dropbox][2] so that you can look at how everything is put together.
The behavior is controlled by the following tree:
A BT is (in the simplest case) structured using selectors and sequences. When a selector is activated it tries to execute its children from left to right and stops the execution once the first child completes successfully. A sequence tries to execute all of its children and aborts the execution if one of its children fails. There is much more detailed information in the Quick Start and the other documentation linked above.
The green box (BTService_GetTargetData) is a so-called “Service” that checks periodically whether a target is in range and writes the data into the Blackboard (which you can think of as the “working memory” of the tree). A service is essentially a normal Blueprint that runs periodically and updates the data the BT needs to function.
The left sequence first determines whether the AI should follow the target. This is done with decorators, which are the dark blue boxes in the tree. The decorators can make use of the data the service has put into the blackboard. To illustrate two different approaches of using decorators, I’ve used one decorator that checks a Boolean variable in the blackboard to determine whether the AI pawn is close enough to the player character to start following. The second test directly compares the target location with the AI pawn’s location and aborts if they are already very close together. If both these test succeed the AI character runs towards the location where the player was seen, waits for one second and returns to its original location. If one of these tests fails the AI pawn simply returns home and waits for one second. You can, of course, arrange for pretty much any desired combination of actions to happen in this way.
