WEBVTT

00:02.450 --> 00:05.300
Time is an essential measure in physics systems.

00:05.300 --> 00:11.210
And until the late 1960s, it was defined in terms of the Earth's rotational spin.

00:11.330 --> 00:18.020
But since the Earth has been slowing down ever so slightly every year, this baseline is no longer accurate

00:18.020 --> 00:19.430
enough for scientists.

00:19.730 --> 00:26.420
Today, time measured in seconds, is based on the duration of rotation of a radioactive atom.

00:27.220 --> 00:29.980
Time is a fundamental mechanic used in games.

00:30.010 --> 00:35.710
It's not only presented to the player for challenges, but it's also used for everything from moving

00:35.710 --> 00:41.200
objects, calculating physics, synchronizing networks and writing shaders.

00:41.980 --> 00:47.380
With respect to artificial intelligence programming, time is used to move characters around an environment

00:47.380 --> 00:52.210
and synchronize movements, transitions and align them with animations.

00:52.630 --> 00:57.340
It's also the basic measure that crops up in all physics calculations.

00:57.370 --> 01:03.910
In this lecture, we're going to have a look at how game engines, in particular unity, work with time

01:03.910 --> 01:11.510
and the different types of time that there are time in a computer game comes from numerous sources.

01:11.530 --> 01:18.730
Of course, there's the computer's own clock, then there's the engine itself, which also runs cycles

01:18.730 --> 01:23.470
that aren't based on real time, but on the processing power of your computer.

01:23.680 --> 01:26.650
You'll be familiar with Unity's update method.

01:26.740 --> 01:29.080
This runs once per frame.

01:29.470 --> 01:36.520
How fast it takes to run depends on your hardware operating system and the load already on your computer.

01:36.760 --> 01:42.610
If you try and use update as the basis for real time in a game, then your game will run at different

01:42.610 --> 01:44.710
speeds on different computers.

01:45.870 --> 01:49.110
Unity also has a late update method.

01:49.140 --> 01:54.120
This runs once per frame as well, but after the animation updates.

01:54.480 --> 01:56.550
Then there's the fixed update.

01:56.580 --> 02:01.770
This occurs in the physics cycle and it may run more than once per frame.

02:02.310 --> 02:07.260
And last but not least, there's the graphical user interface loop.

02:07.290 --> 02:11.070
It may also run multiple times per frame.

02:11.460 --> 02:16.740
Of course, there's a whole lot more going on between these loops and there's great documentation in

02:16.740 --> 02:19.050
the Unity Manual that illustrates it.

02:19.080 --> 02:28.020
It's really worth having a look at, but what we're most interested in for working with our AI are the

02:28.020 --> 02:31.860
fixed update, the update and the late update.

02:33.130 --> 02:37.450
Which one of these you use will depend on what you are trying to achieve.

02:37.480 --> 02:43.810
But my goal in this lecture is to examine the different speeds at which they run and to devise a way

02:43.810 --> 02:46.900
for moving an AI in real time.

02:47.260 --> 02:53.380
So to investigate each of these loops, what I'd like you to do is to create a new unity project and

02:53.380 --> 02:57.160
bring in the Stata file, which goes with this lecture.

02:57.460 --> 03:06.790
In it, when you open up the time scene, so this one back here, you'll find that there are four atoms

03:06.790 --> 03:07.300
in here.

03:07.300 --> 03:10.840
So this is the unity asset of the atom model.

03:10.960 --> 03:19.630
One is called update atom, once called late atom, once called fixed atom and one is called seconds

03:19.630 --> 03:20.290
atom.

03:20.320 --> 03:25.320
If you have a look at each of these in turn, you'll also notice they have different scripts on them.

03:25.330 --> 03:31.120
So we're going to actually use those scripts to investigate the different loops and the update.

03:31.120 --> 03:41.050
So Update Atom has update move late Atom has late update move fixed atom has fixed update move and seconds

03:41.050 --> 03:48.520
atom has seconds update, which is not actually a loop, but we will talk about that a little bit later.

03:48.520 --> 03:55.900
To begin, though, we're going to open up the update move script that's attached to the update Atom.

03:56.170 --> 03:58.510
So those scripts are in the script folder.

03:58.540 --> 04:01.930
Let's open up update move in.

04:01.930 --> 04:04.330
Here is just the basic update loop.

04:04.330 --> 04:06.280
There's nothing happening at all.

04:06.310 --> 04:10.570
What we want to do is move our character along its Z axis.

04:10.570 --> 04:12.880
So that's forward facing axis.

04:12.880 --> 04:18.550
We've been working with moving our characters since the beginning pretty much of this course.

04:18.550 --> 04:26.770
So if you click on one of the atoms or let's take that one, hit the W key, it will show you the orientation

04:26.770 --> 04:27.280
of that.

04:27.280 --> 04:32.860
So you can see that we're working with the Z axis, the blue axis to push it forward here.

04:33.430 --> 04:38.410
So in update move, we're going to translate our character along the Z.

04:38.440 --> 04:49.780
Okay, so this dot transform translate and we'll add zero in the X, zero in the Y and 0.01 F in the

04:49.780 --> 04:50.680
Z.

04:51.220 --> 04:51.610
Okay.

04:51.610 --> 04:58.780
So each update, it's going to move along its Z axis in 0.01 each time.

04:59.020 --> 05:00.100
So save that.

05:00.100 --> 05:05.380
Let's flip back into unity and press play to see what we get.

05:05.380 --> 05:10.240
The animations that are on all of these characters are just going to play automatically.

05:10.240 --> 05:15.640
We're not sinking them with any movement or anything that's just there for a simple effect.

05:15.760 --> 05:23.140
Okay, so now you can see that this first update Atom is moving along quite nicely in the game view.

05:23.140 --> 05:29.020
I've got a top down view so that we can actually compare the speeds and the movements of these different

05:29.020 --> 05:30.900
guys as we go on.

05:30.910 --> 05:37.510
Okay, let's now go and use the same code in the fixed update and the late update.

05:37.510 --> 05:44.710
So I'm just going to copy this out of update move and we'll go into the fixed update and use that in

05:44.740 --> 05:48.160
there and then the late update and use that in there.

05:48.160 --> 05:56.590
Now with each of these updates run the same rate, then the atoms will all be moving together.

05:56.590 --> 06:01.000
If they don't, then you'll notice there's a lag or a faster atom.

06:01.000 --> 06:06.880
Make sure that you save all of these and let's go back and have a look at these.

06:06.880 --> 06:10.360
So update atom is down the bottom.

06:11.110 --> 06:13.870
This one here, then we have late.

06:14.820 --> 06:17.250
Adam and fixed Adam.

06:17.250 --> 06:21.150
Let's have a look at the results of this.

06:21.810 --> 06:23.730
So as they start moving.

06:24.180 --> 06:32.490
What you're going to find is that you get in this case, the fixed Adam is actually walking faster.

06:32.490 --> 06:35.220
So these here, this is the update.

06:35.220 --> 06:43.680
Adam, we know that it runs once per frame and you can see that the late Adam is also running once per

06:43.680 --> 06:44.260
frame.

06:44.280 --> 06:51.810
The only way that this fixed Adam guy can be out in front is that his fixed update is running more than

06:51.810 --> 06:58.740
once per frame, which actually gives him more of a chance to add more translations than these other

06:58.740 --> 06:59.460
two.

07:00.950 --> 07:01.460
Okay.

07:01.460 --> 07:05.700
So now let's have a look at the seconds.

07:05.720 --> 07:11.780
Adam what we're going to do is use the computer's clock to move this atom.

07:12.200 --> 07:19.190
What's going on with these other atoms is it's just running those updates as fast as your computer can

07:19.190 --> 07:20.210
do it, basically.

07:20.210 --> 07:27.140
So on another computer, these guys would possibly be running faster or they could be going slower,

07:27.140 --> 07:29.240
depending on what it is.

07:29.480 --> 07:35.300
Therefore, you don't have consistent movement between your characters and that's very important for

07:35.300 --> 07:36.920
player experience.

07:37.220 --> 07:43.820
If we use your computer clock to move something in, say, meters per second, which is a very common

07:43.820 --> 07:49.580
measurement in physics systems, then it's going to be in meters per se, but it's going to be the same

07:49.580 --> 07:53.570
rate of movement no matter which computer that you run it on.

07:53.570 --> 08:01.610
So let's go into our seconds update and modify this code to move using the clock.

08:01.970 --> 08:05.000
First of all, I'm just going to add a couple of variables.

08:05.000 --> 08:13.130
What we need to do is keep track of when our update starts so that we can take away any sort of ticks

08:13.130 --> 08:17.480
that have happened before we've got to this point and not use those ticks.

08:17.480 --> 08:22.040
So that's going to be our sort of time start offset equals zero.

08:22.040 --> 08:25.610
And then we only really need to set that up once.

08:25.610 --> 08:28.010
And I'm going to create a bool here, not a float.

08:28.010 --> 08:30.530
So bool got start.

08:30.530 --> 08:32.750
Time equals false.

08:33.080 --> 08:39.620
So Unity has a measure to tell us how long our application has actually been running, but we need to

08:39.650 --> 08:44.120
know when this update actually happened so the application could have started running.

08:44.120 --> 08:47.690
And then 5 seconds later, for example, this update started.

08:47.690 --> 08:51.290
We want to make sure we get rid of those initial 5 seconds.

08:51.290 --> 08:57.080
So in here, if not got start time.

08:58.550 --> 08:59.510
Then.

09:02.170 --> 09:13.630
Time start off set equals time real time sense start up and then we'll set got start time equals true

09:13.630 --> 09:19.900
so that it doesn't go back inside of that if statement, then we'll use this time to actually move our

09:19.900 --> 09:20.350
character.

09:20.350 --> 09:29.020
So this dot transform dot position, we're setting the position because this is ticking up.

09:29.230 --> 09:33.670
So it's getting bigger and bigger and bigger the longer this application actually runs.

09:33.670 --> 09:38.410
So we don't want to keep translating it because it would be like some sort of exponential translation

09:38.410 --> 09:40.150
and move your character too far.

09:40.150 --> 09:43.300
So we're just using the position to update that.

09:43.300 --> 09:45.490
So new vector three.

09:45.580 --> 09:50.380
The first will be the current x value of this character.

09:50.380 --> 09:58.390
So transform dot position dot x, then we want the Y to be the same.

09:58.390 --> 10:09.790
So this dot transform dot position dot y and the z value will be time dot real time since start up minus

10:09.790 --> 10:11.920
our time start.

10:13.360 --> 10:14.020
Offset.

10:15.100 --> 10:15.810
Okay.

10:15.820 --> 10:16.150
Right.

10:16.150 --> 10:16.930
So save that.

10:16.930 --> 10:19.990
Let's go back into Unity Press play and have a look.

10:19.990 --> 10:25.810
And now this character will actually be walking in meters per second.

10:25.810 --> 10:32.770
No matter which computer you put it on, it's going to run at the same rate because it's using the actual

10:32.770 --> 10:33.910
time.

10:34.240 --> 10:42.250
And from this, you can now clearly see that these other update loops aren't sort of synchronized with

10:42.280 --> 10:44.110
the real time.

10:44.110 --> 10:46.660
It's just the speed of your computer.

10:46.660 --> 10:55.660
Whereas this other guy who's now getting out of frame is going at a consistent meters per second rate.

10:56.740 --> 10:57.220
Okay.

10:57.220 --> 11:04.870
So that's a demonstration of the different times that are kind of at work in an application.

11:04.870 --> 11:11.200
And what we're striving for is to be more like this guy here in seconds where he's more accurate to

11:11.200 --> 11:12.070
the clock.

11:12.070 --> 11:17.860
Luckily for us, unity provides some great tools and we've actually already looked at them in previous

11:17.860 --> 11:23.290
lectures and you might, you know, wrack your brain and see if you can remember what it was to get

11:23.290 --> 11:27.830
these guys to move consistently inside of those loops.

11:27.850 --> 11:33.700
When we come back in the next video, we will actually implement that and then compare the movement

11:33.700 --> 11:35.710
of all of these characters.
