The wonder behavior is what you want a character to do when it has no official goal location to go to.
I guess you could consider it as the idle state of steering.
It has to be random, but not too random to ensure the character isn't jumping around from location
to location in an erratic way.
In this video, we all look at just one way to produce a nice wander.
It works by using an imaginary circle that is sitting in front of the character.
The circle has a target location positioned on the circumference that moves back and forward around
the circle as it moves.
The agent has its target updated and it changes directions.
There are three variables that basically define this system and how it causes the agent to behave.
The wander distance, which is the distance from the agent to the middle of the circle.
The wander radius that defines the size of the circle.
And the wander Gita.
That influences how the target location moves around from update to update.
So let's go ahead and program this into the bot.
Open up the code.
Now we're going to add this same place.
We've been adding everything else.
So just void wander.
And in here we're eventually going to use the Sikh.
But first of all, we need to calculate all of the value.
So we need a float for wander radius.
And we'll set that to ten.
Then we need a float for wander distance.
Set that to 20.
And then we need a float for Wanda Jeter.
And we will set that to one which is really not very much Jeter at all.
Okay.
So these values here are the ones that you want to change.
If you want to change the behavior of the actual wandering, and you could make them public floats at
the top of your code so you could access them through the inspector and change them in there.
Now we need a Wanda target.
That want a target needs to be updated each time with a new value that the target position is going
to move, which means we need to keep remembering where it was so we can't put it in a local variable.
I'm just going to put it above my method here so that you can see it rather than going up to the top
of the code.
But if you'd like to keep everything nice and neat, then you could put it up there.
So I'm going to just make this vector 3.0 to begin with.
Okay.
So then what we're going to do after we've done that, we want to put Wanda target.
Plus equals new vector three.
And we're going to pick a random value between minus one and one multiplied by our jitter value, and
that's going to allow us to go forward and backwards.
So random range between -1.0 F and 1.0 F multiplied by our wonder jitter.
That's for our X value.
The Y value is going to stay at zero because we don't want to affect that.
And then for the Z value, put a comma in there random range, same thing again, -1.0 F and 1.0 F multiplied
by our wonder jitter.
And then we just close that off.
So that's going to give us a new target that moves the existing one to target, which is initially zero.
Now that we want to target actually has to be sitting on the circumference of the circle.
So once you modify it here, you're going to end up with a position that is in the direction of the
circle, but not not sitting on the circle anymore.
So we need to do a few things to put it back on the circumference of the circle.
So we're going to, first of all, normalize it.
So we want to target dot normalize, which will bring it down to a length of one.
And then we're going to go one to target multiplied equals one to radius to push it out to the right
length.
Okay.
So essentially what we've done here is we've created this wander target, which is sitting on the circumference
of a circle which is currently positioned around the agent, which has a000 center.
But our circle, remember, it's actually offset by the wander distance.
So what we need to do with it is to create a position that's in front of our agent.
So let's create a vector three target and we're going to call this local because it's local to the agent,
as though the agent was currently the center of the world, like at 000.
And the circle is sitting around the agent so we'll go target locals going to equal the wander target
plus effective three zero in the Z direction, zero in the Y direction, and then wander distance in
the Z direction because we're moving it forward from the agent.
So I want a distance that moves our local target out.
Then we need to make that local target into a world position that we can then seek.
So we just need to convert it to target world which will equal this dot game object dot transform.
And just like we had the transform vector before to go from world space into local space, we're now
going to go back the other way.
So inverse transform vector and target local.
And now we have a world position which we can seek.
So put seek target.
World.
Now, to get this wonder to happen, don't forget to change it in your update.
So let's just put Wanda down in there.
Okay?
Let's save this and let's go back into unity.
All right.
So let's press play and have a look at the movement of this guy.
Okay.
You can see a little bit of movement.
Okay.
Always decided to change direction.
And that's because we've got a negative value in there, which allows us to create negative directions
and turn the bot around.
He's not very erratic because our Wanda Gita is down quite small.
So let's just now go back into our code and change that.
So here's our Wanda Gita.
Let's change that to ten and have a look how that affects our agent.
So save that.
Let's run it again.
So let's find him.
And he's more erratic as far as changing direction and even just from side to side wandering around.
You can see there.
So the other things you can try if we go back into our code again is increase the wander radius, which
is going to make that circle bigger.
So if you put that to like 50 and the distance, if you increase the distance from the circle, then
as you move that circle further and further away, then so your target point is getting further and
further away, which means you're going to get a bit more smoother movement as you do this.
But do be very sure that the position you're calculating exists on your nav mesh.
Okay?
Otherwise you might end up with issues when you try and set the location for your agent to move towards
to a position that actually isn't on the nav mesh.
So just make sure you've got a huge nav mesh or that you keep these values reasonable.
Let's just save that at 50 for radius and let's have a look how that affects our behaviour.
So we'll go back in here.
Okay.
So exactly what I was just mentioning.
You can now see that he tried to produce some points there and decided to do absolutely nothing initially
because of those random values and where it was producing the circle.
So I guess you can imagine that this guy's radius, we just zoom in on him.
And if I zoom out a little bit, is that what was happening there would have been that the circle was
probably around this area here.
Okay.
And there were points along this part that we were creating, but there is no nav mesh for those.
But when we go in this side of the circle, it was creating them and then allowed him to actually turn
around after he stood there and did nothing for a little while.
But if you changed your wander distance so that it's the circle is like right out in no man's land out
here, then no point on that circle is ever going to be on your nav mesh and therefore it will cause
an issue.
So with that in mind, let's leave our wander as we initially had it.
Let's put our radius back at C ten I think it was and the Gita back down to one.
So let's just make it a little bit nicer and a little bit smaller.
Now, granted, he could walk to the edge of the map and then just stop there.
But as long as you've got enough sort of radius around you, then you'll be able to turn him around
at some point.
Now, if we look at this, the distance for this circle is 20, and then it's going to have a radius
of ten, which means it's not going to go around the agent or overlap the agent.
So maybe if we turn this down to ten here as well, we can be assured that the circle's always going
to be slightly offset from the agent, but behind the agent as well.
So the agent is going to turn around and be allowed to turn around on that nav mesh.
If you're not using a nav mesh and you're using like three D coordinates in sort of an open, free world,
you know, then fine, you won't have that same problem that you will when you try and run agent set
destination for a point that doesn't exist.
Okay, so that is wander and a few of the little nuances to do with wander and you can see how it's
very adjustable just by playing around with these values.