WEBVTT

00:02.360 --> 00:08.210
The wonder behavior is what you want a character to do when it has no official goal location to go to.

00:08.240 --> 00:11.930
I guess you could consider it as the idle state of steering.

00:12.230 --> 00:17.990
It has to be random, but not too random to ensure the character isn't jumping around from location

00:17.990 --> 00:20.450
to location in an erratic way.

00:20.540 --> 00:25.100
In this video, we all look at just one way to produce a nice wander.

00:25.670 --> 00:30.710
It works by using an imaginary circle that is sitting in front of the character.

00:30.800 --> 00:37.220
The circle has a target location positioned on the circumference that moves back and forward around

00:37.220 --> 00:39.320
the circle as it moves.

00:39.320 --> 00:43.880
The agent has its target updated and it changes directions.

00:44.360 --> 00:50.900
There are three variables that basically define this system and how it causes the agent to behave.

00:51.200 --> 00:55.910
The wander distance, which is the distance from the agent to the middle of the circle.

00:56.150 --> 01:00.390
The wander radius that defines the size of the circle.

01:00.440 --> 01:02.000
And the wander Gita.

01:02.000 --> 01:07.340
That influences how the target location moves around from update to update.

01:07.430 --> 01:10.940
So let's go ahead and program this into the bot.

01:11.120 --> 01:12.830
Open up the code.

01:12.830 --> 01:15.740
Now we're going to add this same place.

01:15.740 --> 01:16.820
We've been adding everything else.

01:16.820 --> 01:18.950
So just void wander.

01:20.390 --> 01:26.630
And in here we're eventually going to use the Sikh.

01:26.990 --> 01:29.570
But first of all, we need to calculate all of the value.

01:29.570 --> 01:33.110
So we need a float for wander radius.

01:35.300 --> 01:37.100
And we'll set that to ten.

01:37.130 --> 01:40.790
Then we need a float for wander distance.

01:41.990 --> 01:43.490
Set that to 20.

01:44.190 --> 01:47.820
And then we need a float for Wanda Jeter.

01:48.000 --> 01:52.680
And we will set that to one which is really not very much Jeter at all.

01:53.080 --> 01:53.520
Okay.

01:53.520 --> 01:57.480
So these values here are the ones that you want to change.

01:57.480 --> 02:03.660
If you want to change the behavior of the actual wandering, and you could make them public floats at

02:03.660 --> 02:07.830
the top of your code so you could access them through the inspector and change them in there.

02:07.950 --> 02:10.350
Now we need a Wanda target.

02:10.350 --> 02:19.140
That want a target needs to be updated each time with a new value that the target position is going

02:19.140 --> 02:24.330
to move, which means we need to keep remembering where it was so we can't put it in a local variable.

02:24.390 --> 02:30.750
I'm just going to put it above my method here so that you can see it rather than going up to the top

02:30.750 --> 02:31.650
of the code.

02:31.740 --> 02:37.170
But if you'd like to keep everything nice and neat, then you could put it up there.

02:37.890 --> 02:43.200
So I'm going to just make this vector 3.0 to begin with.

02:44.520 --> 02:45.150
Okay.

02:45.150 --> 02:51.270
So then what we're going to do after we've done that, we want to put Wanda target.

02:52.840 --> 02:56.110
Plus equals new vector three.

02:57.610 --> 03:05.950
And we're going to pick a random value between minus one and one multiplied by our jitter value, and

03:05.950 --> 03:08.860
that's going to allow us to go forward and backwards.

03:09.010 --> 03:20.890
So random range between -1.0 F and 1.0 F multiplied by our wonder jitter.

03:22.120 --> 03:24.040
That's for our X value.

03:24.070 --> 03:28.210
The Y value is going to stay at zero because we don't want to affect that.

03:28.210 --> 03:40.450
And then for the Z value, put a comma in there random range, same thing again, -1.0 F and 1.0 F multiplied

03:40.450 --> 03:42.940
by our wonder jitter.

03:44.410 --> 03:48.190
And then we just close that off.

03:48.460 --> 03:56.560
So that's going to give us a new target that moves the existing one to target, which is initially zero.

03:56.560 --> 04:02.530
Now that we want to target actually has to be sitting on the circumference of the circle.

04:02.650 --> 04:09.730
So once you modify it here, you're going to end up with a position that is in the direction of the

04:09.730 --> 04:12.700
circle, but not not sitting on the circle anymore.

04:12.700 --> 04:16.690
So we need to do a few things to put it back on the circumference of the circle.

04:16.840 --> 04:19.510
So we're going to, first of all, normalize it.

04:19.510 --> 04:25.810
So we want to target dot normalize, which will bring it down to a length of one.

04:26.050 --> 04:37.300
And then we're going to go one to target multiplied equals one to radius to push it out to the right

04:37.510 --> 04:38.260
length.

04:38.920 --> 04:39.150
Okay.

04:39.220 --> 04:46.990
So essentially what we've done here is we've created this wander target, which is sitting on the circumference

04:46.990 --> 04:54.640
of a circle which is currently positioned around the agent, which has a000 center.

04:54.760 --> 05:00.730
But our circle, remember, it's actually offset by the wander distance.

05:00.730 --> 05:07.000
So what we need to do with it is to create a position that's in front of our agent.

05:07.000 --> 05:14.110
So let's create a vector three target and we're going to call this local because it's local to the agent,

05:14.110 --> 05:19.210
as though the agent was currently the center of the world, like at 000.

05:19.210 --> 05:28.870
And the circle is sitting around the agent so we'll go target locals going to equal the wander target

05:28.870 --> 05:41.110
plus effective three zero in the Z direction, zero in the Y direction, and then wander distance in

05:41.110 --> 05:45.310
the Z direction because we're moving it forward from the agent.

05:45.940 --> 05:49.540
So I want a distance that moves our local target out.

05:49.540 --> 05:55.420
Then we need to make that local target into a world position that we can then seek.

05:55.810 --> 06:05.350
So we just need to convert it to target world which will equal this dot game object dot transform.

06:05.350 --> 06:11.620
And just like we had the transform vector before to go from world space into local space, we're now

06:11.620 --> 06:12.580
going to go back the other way.

06:12.580 --> 06:19.960
So inverse transform vector and target local.

06:19.960 --> 06:23.800
And now we have a world position which we can seek.

06:23.800 --> 06:26.860
So put seek target.

06:28.230 --> 06:28.980
World.

06:29.070 --> 06:33.450
Now, to get this wonder to happen, don't forget to change it in your update.

06:33.450 --> 06:36.000
So let's just put Wanda down in there.

06:36.300 --> 06:36.810
Okay?

06:36.810 --> 06:40.500
Let's save this and let's go back into unity.

06:40.500 --> 06:40.890
All right.

06:40.890 --> 06:46.890
So let's press play and have a look at the movement of this guy.

06:46.890 --> 06:47.520
Okay.

06:47.760 --> 06:49.590
You can see a little bit of movement.

06:49.650 --> 06:49.990
Okay.

06:50.060 --> 06:51.600
Always decided to change direction.

06:51.600 --> 06:58.350
And that's because we've got a negative value in there, which allows us to create negative directions

06:58.350 --> 07:00.210
and turn the bot around.

07:00.210 --> 07:06.660
He's not very erratic because our Wanda Gita is down quite small.

07:07.410 --> 07:10.470
So let's just now go back into our code and change that.

07:10.470 --> 07:11.640
So here's our Wanda Gita.

07:11.640 --> 07:17.280
Let's change that to ten and have a look how that affects our agent.

07:17.280 --> 07:18.180
So save that.

07:18.180 --> 07:19.890
Let's run it again.

07:20.850 --> 07:22.980
So let's find him.

07:25.360 --> 07:33.790
And he's more erratic as far as changing direction and even just from side to side wandering around.

07:34.330 --> 07:35.530
You can see there.

07:38.860 --> 07:45.370
So the other things you can try if we go back into our code again is increase the wander radius, which

07:45.370 --> 07:46.870
is going to make that circle bigger.

07:46.870 --> 07:54.790
So if you put that to like 50 and the distance, if you increase the distance from the circle, then

07:55.180 --> 08:00.190
as you move that circle further and further away, then so your target point is getting further and

08:00.190 --> 08:05.950
further away, which means you're going to get a bit more smoother movement as you do this.

08:05.950 --> 08:15.940
But do be very sure that the position you're calculating exists on your nav mesh.

08:16.330 --> 08:16.580
Okay?

08:16.720 --> 08:23.740
Otherwise you might end up with issues when you try and set the location for your agent to move towards

08:23.740 --> 08:27.340
to a position that actually isn't on the nav mesh.

08:27.340 --> 08:32.770
So just make sure you've got a huge nav mesh or that you keep these values reasonable.

08:32.800 --> 08:38.410
Let's just save that at 50 for radius and let's have a look how that affects our behaviour.

08:38.410 --> 08:39.760
So we'll go back in here.

08:42.660 --> 08:43.010
Okay.

08:43.020 --> 08:45.740
So exactly what I was just mentioning.

08:45.750 --> 08:52.620
You can now see that he tried to produce some points there and decided to do absolutely nothing initially

08:52.620 --> 08:56.730
because of those random values and where it was producing the circle.

08:56.730 --> 09:04.500
So I guess you can imagine that this guy's radius, we just zoom in on him.

09:05.670 --> 09:12.180
And if I zoom out a little bit, is that what was happening there would have been that the circle was

09:12.180 --> 09:15.270
probably around this area here.

09:15.420 --> 09:15.990
Okay.

09:15.990 --> 09:22.440
And there were points along this part that we were creating, but there is no nav mesh for those.

09:22.440 --> 09:28.200
But when we go in this side of the circle, it was creating them and then allowed him to actually turn

09:28.200 --> 09:31.140
around after he stood there and did nothing for a little while.

09:31.140 --> 09:40.620
But if you changed your wander distance so that it's the circle is like right out in no man's land out

09:40.620 --> 09:47.910
here, then no point on that circle is ever going to be on your nav mesh and therefore it will cause

09:48.120 --> 09:49.080
an issue.

09:49.080 --> 09:55.170
So with that in mind, let's leave our wander as we initially had it.

09:55.170 --> 10:01.380
Let's put our radius back at C ten I think it was and the Gita back down to one.

10:01.380 --> 10:05.550
So let's just make it a little bit nicer and a little bit smaller.

10:05.550 --> 10:10.920
Now, granted, he could walk to the edge of the map and then just stop there.

10:10.920 --> 10:17.430
But as long as you've got enough sort of radius around you, then you'll be able to turn him around

10:17.430 --> 10:17.990
at some point.

10:18.000 --> 10:24.390
Now, if we look at this, the distance for this circle is 20, and then it's going to have a radius

10:24.390 --> 10:29.850
of ten, which means it's not going to go around the agent or overlap the agent.

10:29.850 --> 10:36.450
So maybe if we turn this down to ten here as well, we can be assured that the circle's always going

10:36.450 --> 10:40.560
to be slightly offset from the agent, but behind the agent as well.

10:40.560 --> 10:46.740
So the agent is going to turn around and be allowed to turn around on that nav mesh.

10:46.770 --> 10:52.950
If you're not using a nav mesh and you're using like three D coordinates in sort of an open, free world,

10:52.950 --> 10:59.660
you know, then fine, you won't have that same problem that you will when you try and run agent set

10:59.730 --> 11:03.150
destination for a point that doesn't exist.

11:03.180 --> 11:10.980
Okay, so that is wander and a few of the little nuances to do with wander and you can see how it's

11:10.980 --> 11:15.540
very adjustable just by playing around with these values.
