How can i add range attacking to my behavior tree?

I added melee attacking and checking the distance from the player character before attacking but im a little stuck. Where would be a good spot to add a melee attack on the behavior tree?

Are you planning on doing mixed attacks? Can a character have both a ranged and melee weapon?

Then you need a way to decide if the character is to back up and fire range.

For instance if the player runs up to less than 1/2 the range the character can decide to go melee otherwise retreat to random area within firing range and continue attacks.

Does your enemy have infinite ammo? That can also force them to melee attack after a while.

Write down the behavior you want to see and you should get a vague notion of how the tree should look like.

so this is what i came up with so far. I want the tree to first check if the character is out of melee ranged ( I called the boolean “IsInRangedRange”) so that way they can do a ranged attack first with a cooldown.

If they are in melee range, they will do the melee attack. (works perfectly fine)

The ranged attack isnt working correctly which is what im trying to figure out at the moment

Sorry for the slow reply. Had a bit of backlogged posts I had to attend to.
A simple prototype might give you an idea.

i checked the .zip and theres only skeletons, physics and some static meshes. I’m not seeing anything else

Just downloaded and tested it (321MB)




and more

Did you unpack the whole project?

Your BT is just a bit more detailed than mine yet its having an issue firing off the ranged attack i have on mine. It goes to follow character and immediately works but it wont go to the right at all.

I did find that when i set the decorator to none
or lower priority, it works but only once.

FYI That is not the decorator I use in my tree

which version of unreal are you using?

5.1

I took sort of a break but i got to try out the code today and it work a bit, but 1. The actor ai isnt moving towards the main character 2. I believe the code isnt reading correctly?

Did you set the MeleeRange and RangedRange parameter for the blackboard? They are uninitialized by default (I set mine via the ai controller)

Yea and the ai in the project you sent doesnt move aswell. It just shows the line trace and rotates the body to look at the target actor

Could it be that the navmeshbounds got corrupted in some way? Maybe try deleting it and inserting a fresh one (it happens sometimes in 5.1)
Also check if the ranges aren’t too low. Perhaps the melee range is just around the enemy actors feet.

I fixed it it just needed the navmesh bounds volume. I just got to relook over everything and see why my character isnt working properly

Ok i believe i found the issue. The service is showing up invalid instead of valid but i have no clue why.

Maybe go into the decorator bp and see if it needs a recompile?.(that could explain why it’s flagged as red in your screenshot)

Yea i’ve been at it for days im not understanding why its not working when both of the melee and ranged sequences are together

I did fix why the AI wasnt following the character though. In the Project you sent, it uses ai perception but in my project, it doesnt so i had to change the object value.