Can See Target
The Can See Target branch contains all of the logic pertaining to attacking if a target is within sight. This branch contains many tasks and has been divided into multiple braches and two subtrees. Here’s what the branch looks like with just a reference to those subtrees:
A Lower Priority conditional abort is used so the Is Target In Sight task will be reevaluated if any branch to the right of the Can See Target branch is active. When Is Target In Sight returns Success it means that there is a target within the agent’s field of view. The Start Stop Ability will then run to ensure the character is not running. A Selector is placed so the left branch can attack and the right branch will be run after the attacking is finished. Let’s look at the left branch first. The Parallel task is used to run both the left and right branches at the same time. This allows the agent to possibly change targets while still attacking the original target. The left branch looks like the following:
At the top of this branch is a Repeater. The Repeater will allow the child tasks to be reevaluated each tick. The child Sequence task then contains two children: Is Target In Sight and Return Success. The Is Target In Sight is the same as the one at the top of the Can See Target branch except this one will store the target in the Possible Target variable instead of the Target variable. This allows the existing target to be kept if for some reason the agent doesn’t want to switch to the new target that just came within sight.
The branch below the Return Success looks similar to a setup before where we first compare the Possible Target variable to the current Target variable and if it’s different then there’s a chance that the agent should switch based on the Random Probability. If the agent should switch then the current target is added to the Ignore Targets set and the Set Shared GameObject task will set the Possible Target to the Target.
As the agent is continuously checking for new targets they are also attacking with the branch on the right:
Without the Attack subtree pulled in this is a small branch which first determines the best weapon for the current game state. This task is only run once and allows the agent to have a weapon ready to go when the Attack branch runs. The Start Equip Unequip task will equip the Item Set returned by the Determine Weapon task. The rightmost task is a Behavior Tree Reference task which loads the Attack branch.
The Attack branch is explained on this page but a quick summary is that it will allow the agent to actually fire at the target. As soon as the agent is done attacking (because the target died or is no longer in sight), the following branch will run:
The Reset task will run and reset all of the variables so the lower branches start fresh. If the target is still alive then Is Alive will return success and the next three tasks will run. The target will still be alive if Is Target In Sight returns failure meaning the target is no longer in sight (such as if the target ran away and took a corner). The agent will then get the last position of the target and set the Search variable to true so the agent will search at that position. Finally, the Target variable is reset since it’s not needed anymore.