How do I make a MoveTo AI stop when a certain distance from player?

Hi guys,

I’m trying to figure out how to have a simple ai, following the player character via a MoveTo node, stop following when they are a certain distance away from the player character. Any help would be hugely appreciated!

Thanks a lot,
Joseph

I assume you are using the MoveTo node in a Behavoir tree, right?

Does setting the acceptance radius to the desired distance not work? Some people have experienced a problem with MoveTo never finishing which could be resolved by setting the EnableBTAITasks option in Project Settings / AI System, see here for the discussion.

Thanks for the reply! I’m not sure about behaviour tree, but this is the current set up I have inside my AI blueprint that I’m using on the pawn that is following:

I also have a function within it that allows me to stop and start it following:

However, I want it to stop following if, say, I stopped it following, went a certain distance away, and then tried to get it to start again. If I’m too far away, it shouldn’t be able to follow. Isn’t the acceptance radius used for how close the AI controlled pawn can get to the player character?

Thanks again for your help!

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.

Thanks so much for the detailed reply - I hadn’t heard of BT’s before so this was incredibly helpful! Unfortunately I haven’t been able to open the file you sent as, when I try to launch it, Unreal tells me this: “Failed to load map!
…/…/…/…/…/…/Users/user/Desktop/BPMovement/Content/TopDownBP/Maps/TopDownExampleMap.umap appears to be an asset file.”

Instead, I spent several hours trying, failing and finally succeeding in replicating the Quick Start one, which works quite well actually! My only problem is that I need to implement the input which stops and starts the movement completely - My main character is a duck, and the ai follower is a set of ducklings that the player can command to follow, or stop, by pressing a button. I had this working before but I’m not sure how to do it with BT, so if you could help that would be great, but I really appreciate everything so far and understand if you don’t have time!

Thank you again!!

That is not very hard to do. One way (perhaps not the best solution, but a very simple one) is to add a new Boolean variable to the blackboard and to not execute the movement branch of the BT if this variable is false, like so:

(The decorator checking the FollowingIsActive? variable is the new thing.)

To toggle this variable between true and false in the AIs, you could define a key binding in the player controller of the player (i.e., the TopDownController not the BP_FollowerController that we defined for the AI). When that key is pressed it needs to find all ducklings (or BP_AICharacter instances in my case) and toggle the FollowingisActive? variable in their blackboard.

Since there is a bit of additional setup involved (like adding the correct default values to the ...Key variables) I’ve updated the project [in my Dropbox][3] and also fixed the redirects so that it should now open correctly (I’ve checked on another computer I have, so hopefully it will work for you as well.)

Thanks a lot man - unfortunately I still can’t open the project, and it might be because I’m not updated to the latest update of Unreal (I’m on 4.13.1), which I’m afraid to do until I hand this project in as my dissertation in a couple of weeks.

Because of this, I’m not entirely sure how to make each of the things on the BT you’ve shown me, such as the BTService_GetTargetData. I’m going to try and play around with it but any help on the specifics of these would be brilliant!

Sorry for the hassle!

Hi there,

I finally managed to open the project on a different computer and tried to replicate it in mine. However, after having recreated every part (with the variables) I am getting no movement from the ducklings (with navmesh) and am getting one recurring error:

Error Blueprint Runtime Error: Accessed None trying to read property CallFunc_K2_GetPawn_ReturnValue from function: ‘ExecuteUbergraph_BP_FollowerController’ from node: Set Value as Vector in graph: EventGraph in object: BP_FollowerController with description: Accessed None trying to read property CallFunc_K2_GetPawn_ReturnValue

Any help with this would be hugely appreciated!
Thanks so much again, Joseph

This is very simple. In the MoveTo node, set the “Acceptable radius” to whatever you want the distance to be, and it will finish the movement when it enters this area.