We can use a Behavior Tree to construct an NPC with melee attack behavior. The Melee Attacker will have the following behavior:
Search for the best opponent to attack every 10 seconds. The best opponent to attack is going to be the closest opponent within a
SearchRadius
. We will achieve this using a Service. Chalk the opponent we are attacking into the Melee Attacker's Behavior Tree Blackboard.Move towards the opponent we are attacking (indicated by the Blackboard).
If we are within
AttackRadius
units of the opponent, damage the opponent we are attacking everyAttackCooldown
seconds.