The one the 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 ideal 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 wonder.
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 the system and how it causes the agent to behave.
The one the distance, which is the distance from the agent to the middle of the circle, the one to
radius that defines the size of the circle and the one the jitter 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 wonder.
And in here.
Or eventually going to use the seek, 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 10:00, then we need a float for Wanda Dist..
Set that to 20.
And then we need a float for Wanda Jittered and we will set that to one which is really not very much
jitter at all.
OK, 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 wand, a target that want to 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 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.
OK, so then what we're going to do after we've done that, we want to put Wanda Target.
Plus equals new Victa three.
And we're going to pick a random value between minus one and one multiplied by a jeta value, and that's
going to allow us to go forward and backwards.
So random dot range between minus one point zero F and 1.0 F.
Multiplied by our Wanda Gita.
That's for our X value, the Y value is going to start at zero because we don't want to affect that.
And then for the Z value common, the random range.
Same thing again, minus one point zero F and one point zero F multiplied by L Wonder Jitter.
And then we just close that off.
OK, 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 modified 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.
The target multiplied equals one to radius to push it out to the right length.
OK, so essentially what we've done here is we've created this one to target, which is sitting on the
circumference of a circle which is currently positioned around the agent, which has a zero zero zero
center.
But our circle, remember, is actually offset by the wonder 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 zero zero zero in the circle is
sitting around the agent.
So we'll go target locals going to equal the Wanda target plus vector three.
Zero in the Z direction, zero in the water action, and then wander distance in the Z direction because
we're moving it forward from the agent, OK, so we want to distance that moves our local target out,
then we need to make that local target into a world position that we can then seek.
OK, so we just need to convert it to target world, which will equal this game, object, dot, transform
and just what we had the transform vector before to go from WorldSpace into local space.
We're now going to go back the other way.
So inversed transform vector and Target Local.
And now we have a world position which we can say, OK, so put seke target.
World now to get this one to two happened, don't forget to change it in your updates.
So let's just put wonder down in there, OK, 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.
You see a little bit of movement, OK?
Always decided to change direction.
And that's because we've got a negative value in there to allow us to create negative directions and
turn the boat around.
He's not very erratic because our Wanda Gita is down quite small.
OK, so let's just now go back into our code and change that.
So he's a wanted to let's change that to 10 and have a look how that affects our agent.
So save that.
Let's run it again.
OK, so let's find him.
And he's more erratic as far as changing direction and even just from side to side wondering around,
you can say they're.
OK.
That's great, so the other things you can try if we get back into our book code again is increase the
wonder 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 with a bit more smoother movement as you do this.
But to be very sure that the position you're calculating exists on your nav mesh.
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 varieties and let's have a look how that affects our behavior.
So we'll go back in here.
OK, 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 zoom out a little bit.
Is that what was happening?
There would have been that the circle was probably around this area here, OK?
And there were points along this part that we were creating, but there is no need 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 wandered 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 readiest back at, say, 10, I think it was, and the Jidda back down to one.
So let's just make it a little bit nicer, 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 original overlap, the agent.
So maybe if we turn this down to ten here as well, we can be assured that the circles are 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 3D coordinates in a 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 Dot
set destination for a point that doesn't exist.
OK, so that is Wanda and a few of the little nuances to do with Wanda.
And you can see how it's very adjustable just by playing around with these values.