Welcome back.
Now we have some basic behavior.
We still have a ways to go, but we know that our melee attackers will continue walking in closer.
And as soon as they get here, they attack.
Now, I'd like them to attack and then kind of shuffle around or move around to a different location
around their target aura.
So that way they don't just stand there in one location and just swing away and swing away.
If they swing, then I'd like them to move around over to here and swing and move around over to here
and swing.
And I'd like that to be random.
So that's what we're going to take care of in this video.
Now, another thing, if we press play, take a look at the melee attacker.
As soon as it reaches that point, there's a little pause.
That point is 500 units away because they're in this branch move to Target.
But as soon as they're no longer far enough to be approaching using this branch, then am I close enough
to attack?
Is suddenly true.
And then they're using this move to get close and there's a little bit of a jerk.
It's very slight.
It's hardly noticeable, but you can see that he kind of jerks a little bit.
Now that can be fixed.
We can take a look at his blend space.
So I'd like to really quickly just take a look at that before moving on.
So we have blueprints and character and we can go to Goblin Spear and in AVP, Goblin Spear, we can
take a look in the asset override to see where it's idle walk, run, blend space is.
It's here.
We can browse to that and here it is.
It's in its animations sphere.
I'm going to go into this and change the blend settings just a bit.
Now the blend space uses a weight speed and this is kind of an interesting way to handle this, but
it's basically the speed at which the sample weights are allowed to change.
So in other words, if we change this value, we can make that jerkiness a little less jerky, a little
smoother.
Now we see that a speed of one means the sample weight can change from 0 to 1 in one second.
A speed of two means that this would take half a second.
So the higher the value, the faster we're going to blend between our sample weights.
That's those inputs for that blend space.
So if I put a value of four, then it'll take a quarter second.
And we should see that jerkiness.
Be a little smoother.
In fact, there is no jerkiness.
There's a slight pause.
I'm okay with a slight pause.
I'm fine with that.
I just don't want jerkiness.
So that looks great.
And I think a value of four for weight speed is perfect.
We can even add that for our slingshot goblin.
That way, if they ever stop and start again, they don't do so jerkily.
So I'm going to go ahead and close out of those.
That takes care of those.
But now for this, finding a new location business.
So when my melee attackers get close and attack, I want them to attack and then find a new location.
And this on screen debug message here is going to be quite annoying.
So what I can do is just go into my Bet service, find nearest player and we can take away that on screen
debug message here.
We don't need that anymore do we?
So next time we compile that will be gone.
Now back to the task at hand.
We need a task, right?
We need a behavior tree task.
We want one for finding a new location around the player.
Now, this is a simple task.
No need to go through the trouble of making one in C plus plus.
Now that we know how to do that, that's an option.
I'm going to make it in blueprint and I'm going to have a new blackboard key for a location for our
AI to move to that's going to be different than Target to follow.
Right?
Because if they're moving to their target to follow, they're moving towards aura.
And as soon as they reach it and attack, I want to find a new location that's different than the target
to follow.
I want a vector location somewhere in the vicinity around Aura.
So I'm going to add a new blackboard key.
We're going to go to Blackboard, add new Key, and we're going to use a vector and I'm going to call
this vector move to location and we're going to set that move to location with a task here.
So I'm going to make a new behavior tree task.
So I'm going to click the dropdown for new task and we can choose one of our tasks.
BT Attack is the Blueprint version and BT task attack is the C plus plus version.
Notice it shows the full C plus plus name minus the U.
And then we have BT Task Blueprint Base.
That's what I'm going to choose and we're going to choose to save it here in our AI folder.
And I'm going to call this BT.
I'm going to use the convention BT without the full task to signify that this is a blueprint task and
I'm going to call this BT go around Target.
So I'm going to go ahead and save it there and here in BT, go around Target, I'm going to use event,
receive, execute AI and we're also going to need to call, finish, execute.
And in between here is where we're going to make things happen.
Now I'm going to want a blackboard key selector so that I can set its value.
We're going to want to set that value for move to location.
So here in BT, go around Target, I'm going to add a new variable and change its type to blackboard.
Key selector, and I'm going to call this new location and we can link this up with our move to location
here.
If we drag off of this and add the task, the task is go around target and if we click on it and select
it.
We can set it here now.
We don't see it yet.
We have to go back and open the eye icon and compile.
And once we do that, we can go back to our behavior tree and see new location and we can link it up
with our move to location here.
Once we've done that here in go around Target, we can set the value of new location.
So how do we set the value of new location?
Well, we're going to need to find a new location around our enemies Target.
Now, the enemy doesn't know what its target is.
Exactly, does it?
Well, it kind of does.
It has a target to follow, which is an object.
So that's another thing that we can sort of link up with one of our blackboard keys.
We can have a new blackboard key selector for the target.
So I'm going to add a new blackboard key selector called Target.
I'm going to open the eye icon compile.
And in our go around target task node, I'm going to set target to be linked up with target to follow.
Now we have access to target to follow here in our task and it's a good idea to make sure that that's
a valid target.
So what we can do is get our target blackboard key selector.
We can get the value of that blackboard key selector by using get blackboard value as and we can choose,
we can choose actor, we can choose object.
And we know if we go into our blackboard and select target to follow that its base class is actor.
So get blackboard value as actor makes sense.
So we're going to use get Blackboard value as actor.
We're going to get that return value which is an actor and I'd like to use is valid to make sure that
it is indeed valid.
So this should be aura, right?
And as soon as we have aura, if it's valid, we can get auras location with get actor location.
What are we going to do with that?
Well, we want to find a new location that's somewhere around Ora's actor location.
But we want this new location to be navigable.
We want it to be a reachable point in our nav mesh.
We don't want to try to get any random location that could be in some hole in the nav mesh.
Right?
If I press P and look at our nav mesh.
Let's say we're close to this pillar.
We don't want a random location that could be somewhere in this hole.
We want it to be a navigable point, which means it's in the green area somewhere we can actually reach.
So the way we can do that is with the function.
Get random location in navigable radius.
This lets us choose a origin.
We're going to use Ora's location.
That's that actor location and some radius, some float value that determines how far away this random
point can be.
I'm going to use 300.
In fact, I can promote this to a variable.
And call this radius.
I can even expose it with the eye icon and we can set this to a default value.
I'll set it to a default of 300.
And then because it's exposed, we could even go back to our behavior tree and we can set the radius
here if we like.
So that's handy.
Now, if the actor's valid, we're going to do this and we're going to take that random location that
we get from it.
And what I'd like to do is set the blackboard value of our new location.
So we're going to get that blackboard key selector and we're going to drag off of it and set blackboard
value as vector.
And we're setting it to that random location.
Like so.
And once we're done with that, we're going to finish, execute and return success.
Now get random location in navigable radius could fail.
And really, in that case, maybe we could just pass a failure in here.
So we're going to go ahead and just add a reroute node here and here and just pass that straight in.
And I think that will be fine.
So now we have this go around target task that will get a random point in the navigable radius.
We can go back in here and as soon as we get that random point, now we have this move to location set.
It should be and we can move to that location.
So we're going to drag off of our sequence.
We're going to use another move to.
And for this move to we're going to set the blackboard key to our move to location.
Now we're moving to that random point around the target.
And after attacking, I'd like just a little bit of a delay, maybe a second plus or minus half a second,
something like that.
So after the attack, let's do a wait node.
And for this wait, we're going to have a wait time of one second plus or -0.5.
So there will be an attack and a little delay and then we'll move around the target.
Now, the thing about moving to a random location is we've seen that our enemy could get a bit stuck.
It could have other enemies in the way those don't create holes in the nav mesh after all.
So if we get hung up on this task too long, I want to abort.
And we can do that with a decorator.
I can right click, add decorator.
And there's a special decorator called Time Limit.
If I select that, then the time limit lets me abort.
If the enemy's been struggling a little too long on this move too, so I'll have this set to observer
abort self.
So we'll abort the self.
That's this specific move to node here.
It can abort itself and we won't be stuck in limbo here and the time limit can be something like two
seconds.
So if we're trying to move to a new location around the target, if that takes longer than two seconds,
we'll go ahead and abort and then we can try things again.
So let's see how our go around target works and we can of course change the name of this node.
I'm going to call it find new location around Target.
Let's see how this is going to work.
Let's go back, press play, and we'll see how our melee character behaves.
Attack.
Couple of seconds delay.
Oh, it attacked again.
It moved.
It moved again.
And notice that it moves to a new location, but then it comes in close again.
So this gives it a little bit of a more aggressive behavior.
And of course, we can change parameters.
We can change that radius of the random point and so on.
We can also test this out really close to a pillar here and we'll see that it should not get stuck.
And if it does ever get stuck, there's a two second time limit and it'll go ahead and just abort that
move to for moving to a random location.
So this gives us a little bit more of some frantic behavior for our enemy.
It's going to attack.
It's going to try to find a new location.
Notice it got stuck there because it was trying to move through.
Ora Ora doesn't create a hole in the nav mesh either.
So if it's trying to move and it's stuck after two seconds, we go ahead and try to find another location
after attacking again.
So this gives us a little bit of some safety.
Okay, so that's looking great.
Now, our attack is still pretty boring.
It's still just drawing a debug sphere, but at least we know when it's attacking, when we see those
red spheres and we know that after attacking, there's a second and a new location is found which we
abort if it takes longer than two seconds to do so and we find a new location and attack again.
Excellent.
So this is looking pretty good.
We still do need to actually have our enemies attack and we'll get to that.
But we also want some behavior for our ranged attackers as well.
Right.
They get to attack, too.
So we'll get to those things in the videos to come.
For now, great job and I'll see you soon.