Welcome back.
So we've now created a nice environment query that will return a location that we can reach on our NAV
mesh bounds volume that is the closest to the Querier that can see aura.
This is perfect for our range attackers because they're going to want to be able to see aura.
And so this is how we're going to move our ranged attackers into place.
So let's go back to our behavior tree and see about setting this up.
The first thing we need to do is run that X query.
So how do we do that?
Well, if we drag off of our sequence for the ranged attacker and expand tasks, there's one called
Run X query.
If we select that, then we can choose an X query to run by expanding x request.
And for query template, we can choose our query.
We're going to use find ranged attack position.
I'm going to change the node name here to say get in position to fire or actually let's call it get
fire position because this is not actually moving our AI.
It's just calculating the best position.
Now, after it runs the query, it's going to set one of our blackboard keys that we specify so we can
take our blackboard key and choose which key we want to use.
I'm going to use move to location.
So after running this query, move to location should be set to the location from among those items
that we've filtered out and scored.
So after getting the fire position, we are going to want to move to that location.
So let's drag off.
We'll get a move to task.
And for Move two, we're going to select the blackboard key.
Move to location and I'm going to rename this node to get in fire position.
I'll call it get in firing position.
So we get fire position, we get in fire position.
This looks a little bit confusing.
They're almost the same message.
So I'm going to change this one.
Sorry for changing this so much.
I'm going to call this calculate fire position.
That way it's just a little more clear.
First we calculate it, then we get in fire position and then we can attack.
So at this point, I'm going to use our attack node.
We'll drag off, we'll use tasks attack, and that's going to result in a nice red debug sphere for
now.
And after that I'd like just a little bit of a delay.
So we're going to have a wait here and this one will be one second plus or -0.5.
And there we have our ranged attacker behavior.
Now all that's left is to test it out.
So I'm going to, first of all, save all and go back to our other map, our start up map here.
And we can just play test.
I'm going to hit play and we'll see how far they run before they stop.
It looks like they don't stop.
They just keep running towards us.
So we shouldn't see that.
So what I'm going to do is take my goblin spear and I'm going to get rid of it.
I'm also going to get rid of one of my ranged attackers so we can just test the one and I'm going to
take a look at my behavior tree.
Looks like we're in move to Target.
And then we failed this one and went straight to am I close enough to attack here?
So it's basically doing this melee code.
It does get in fire position.
We can see sometimes it goes to right here, moves to the move to location.
Let's see if we can get our EQs query working more to our liking.
The grid half size was pretty big.
We can lower this to perhaps 500 grid half size.
Let's see if that helps.
Anything.
And it looks like it's coming straight to me, so something isn't right.
And I notice right away that I used a tack.
That's the C plus plus version.
I'd rather use my BT attack.
That's my blueprint version.
That's the one I want to use.
So let's see how that works.
I'm going to press play.
I expect it to pause and attack.
And there it is.
And we can also use obstacles and see if any obstacles block the path.
I'm going to press P to see my nav mesh.
And let's go to Assets Dungeon, and I'm going to get a nice wall here.
Here's a wall.
Four by two.
Nice big wall.
I'm going to rotate it by 90 degrees.
And bring it over here.
That way we can test out obstacles.
So I'm going to press play and get behind this obstacle.
And so far, so good.
I'm going to get behind it here.
And it looks like we got a crash indexing an array out of bounds at or a player controller line 106
here on line 106.
I'm trying to access path path points at a specific point, and I believe it's because I was hitting
that wall.
This should be something we can place a check here for.
And I'm getting red, so I'm just going to control shift B to compile real quick.
Okay.
So we are compiling just fine.
I'm going to check the number of points here before attempting to access this cached destination.
So I'm going to say if.
NAV path.
Path points.
Dot.
Num is greater than zero.
That way we don't go trying to do anything we shouldn't be doing.
There we go.
Okay, so riders throwing a bit of a fit showing everything is red.
So I'm going to go ahead and close out of Ryder.
And I'm going to delete my intermediate and my binaries folders and force writer to make those again.
So I'm going to open with Jetbrains rider.
All right.
So now that things are looking good here, I'm going to go ahead and run.
I was running without debug mode.
I'm going to run in debug mode this time.
Okay, we're back.
I'm going to go ahead and close out of this.
Three by three by three tile.
Don't need it.
I'm going to close my X player context.
Don't need that.
I don't need my BT go around target open and I don't need my attack open.
Although before I close it, I'm going to set the duration for this attack to 0.5.
So we only see that sphere for just a second.
I said duration of the attack, but what I meant was the duration of the debug sphere here.
We're just going to show it for half a second and I'll go ahead and close the attack.
And we've got our behavior tree open and we can play test.
This looks like I have my three goblins back because I crashed and deleting them was a change that was
not kept.
I'm going to just bring in a smaller wall two by two.
This time I'm going to rotate it a little bit.
We'll rotate it about like this.
And I'm also going to select the wall because I don't want this to block the visibility channel as that's
what I'm using for my click to move.
So for collision, I'm going to set collision presets to custom and ignore the visibility and the camera
channels.
I'm going to save all and I'm going to test.
So let's see.
We've got two Rangers.
One melee melee comes up close.
Actually, the Rangers come up close as well.
So we need to see why.
Oh, and that's because we're using the C plus plus attack, which does nothing.
We want to use the blueprint.
Attack, BT attack.
Let's go ahead and save all.
Try this again.
We'll go behind this wall.
Okay.
And it looks like.
We do have an issue.
Our range attackers, their environment query is able to trace and hit me so they don't consider this
wall something that they can't see through.
I would have to go back and set it to block visibility and hide behind it and then they would have to
come around the corner until they can actually see me and then stop at a place where they can shoot
me.
So there they are.
Here they come back.
And they're finding a spot where they can shoot me, it looks like.
Okay.
So I just don't get any response when I click on my wall here with a short press.
If it's a long press, then of course I get my auto run.
But a short press, it's not going to register a hit on this wall.
It's not going to make me walk around the wall because I'm hitting the wall and it's going to try to
walk up onto the wall.
So if we really want the best of both worlds, we may have to have a collision channel specifically
for our click to move or specifically for our AI.
So that's something to think about.
But for now, we do have our range attackers and they're coming up close, but staying a bit of a distance
away so that they can attack from a distance and we can see them attacking there, drawing those debug
spheres.
They're attacking over and over again.
And if I move, they're going to chase, but they're going to chase so that they can see me.
As they're coming around the corners, but they're still staying a bit farther away.
So this is looking pretty good.
Awesome.
So our behavior tree is looking pretty fleshed out.
It's looking pretty good.
We'll playtest, of course, and see if any other edge cases come into play.
But we're now making use of an environment query to find a good ranged position to fire from.
And we've also got our melee attackers here.
They're coming in close.
They're attacking.
They're finding a new location around the target.
So we've got some nice, relatively simple AI working here.
And this is looking pretty good.
So with that, our basic logic for our behavior tree is done.
But all we're doing for our attack is drawing red debug spheres.
We are going to want to actually attack, right?
So that's the next step for us.
We want our enemies to attack and try to do damage and we'll do that in the videos to come.
So great job.
I'll see you soon.