I would like to implement a “follow target” behavior that has some restrictions. Here is what it would do.
The scene contains a moving object designated as Target and another object which is the Follower. The Follower is stationary by default. When the Target moves within 4 units from the Follower, it “detects” the Target and starts following it using NavMesh, but only for 5 seconds. After that, the Follower stops and is ready to resume following the Target if it comes back within the 4 unit radius.
I have tried some simple BTs, but I think my approach is fundamentally wrong. What would be the proper way of achieving the described behavior?
Considering I understood correctly, here is a Behaviour Tree that will do what you are after 🙂
The decorator used here above the Seek action, is the “Timeout Decorator” and is set to timeout after 5 seconds, and as a result interrupt the Seek action after that amount of time has come to pass.
Thanks! This works as I described in my question. The problem is, I made a mistake by forgetting that after 5 seconds the follower will be within 4 units, so it will resume the Seek action right away 🙂 I guess we need an additional timeout between the end of Seek action and detecting the Target. How would you do that?
If you simply want to introduce a delay after the Seek action has timed out, you can use the Wait action.
For this tree to work correctly though, we also need to add the “Optional” decorator above the Timeout, because the Timeout decorator will return Failure when the Timeout has come to pass, but for the Sequencer to continue to the next action, we need to return Success to it.
As such, we use the “Optional” decorator here, so that we disregard whether the Timeout returns Failure (which is by default) or Success and enforce the Sequencer to continue in either case.
Please let me know if that works for you, or if you need any further clarification or questions.
Thanks 🙂
Is it possible to simultaneously log the distance between the Target and the Follower? I can’t figure out how to do that without an additional BT, but that’s probably wrong 🙂
You are very welcome and sorry for the late reply.
Whenever you want to do something “simultaneously” in a BT, the solution is to use the Parallel Composite node 🙂
The Parallel node is basically executing all it’s child nodes simultaneously and in the same frame (but in order from left to right).
So if you for example have a Parallel node with two child nodes that log something to the console, both log action will execute in the same frame.
By default, child nodes of a Parallel that are finished (returned Success or Failure) are not restarted until all other child nodes are finished as well, but if you set the Parallel node to be “Dynamic” in it’s inspector, then it’s child nodes will restart as soon as they are finished regardless of the other child nodes status, which might be useful depending on what you are after.
Please let me know if you need more help with the Parallel node (or anything else for that matter 😉 )
Thanks!