1
00:00:03,060 --> 00:00:05,460
So what's the problem that we'll get with our current code?

2
00:00:05,460 --> 00:00:09,510
Because we're saying an update, do a thing every single frame.

3
00:00:09,510 --> 00:00:14,850
If I'm on a fast computer, then it's going to do that a lot of times and maybe have my car move different

4
00:00:14,850 --> 00:00:19,830
to someone who's on a slower computer and they're doing things less per second.

5
00:00:19,830 --> 00:00:25,140
So we need to understand how to use time, delta time and what it is time to tell to.

6
00:00:25,140 --> 00:00:29,760
Time is a way that unity can tell us how long each frame took to execute.

7
00:00:29,760 --> 00:00:35,250
So on a fast computer, it's going to take a short amount of time for each frame on a slow computer,

8
00:00:35,250 --> 00:00:36,870
it'll take a longer amount of time.

9
00:00:37,050 --> 00:00:43,650
And then when we multiply something by the amount of time it took for each frame, it allows us to make

10
00:00:43,650 --> 00:00:45,960
things frame rate independent.

11
00:00:45,960 --> 00:00:51,150
So in other words, the game behaves the same on fast computers as it will on slow computers.

12
00:00:51,150 --> 00:00:56,040
Or if you're working on a mobile device versus a computer, etc. We want to make sure on everything

13
00:00:56,040 --> 00:00:57,420
we get the same sort of behavior.

14
00:00:57,420 --> 00:01:01,530
So let me give you a little example here, if that hasn't clicked just yet on UPDATE.

15
00:01:01,530 --> 00:01:06,720
So in other words, each frame we want to move our player one unit to the left.

16
00:01:06,720 --> 00:01:12,180
So on a slow computer, we might be getting, say, ten frames per second on a fast computer, we might

17
00:01:12,180 --> 00:01:14,790
be getting, say, 100 frames per second.

18
00:01:14,790 --> 00:01:17,280
And it's going to vary even on a fast computer.

19
00:01:17,280 --> 00:01:22,650
Sometimes it'll be 100 frames, sometimes it'll be 200 frames, some 150 frames, depending upon what

20
00:01:22,650 --> 00:01:24,360
else the computer needs to calculate.

21
00:01:24,360 --> 00:01:25,500
It bounces up and down.

22
00:01:25,500 --> 00:01:27,060
So we need to smooth all of that out.

23
00:01:27,060 --> 00:01:32,670
But look, in my example here, if we have ten frames per second, that means each frame is 0.1 second.

24
00:01:32,670 --> 00:01:34,620
Yes, .1.41.1.

25
00:01:34,620 --> 00:01:40,440
If you do that ten times and reaches 1/2 on a fast computer, if we've got one 100 frames, then each

26
00:01:40,440 --> 00:01:47,940
frame is lasting 0.01 of a second because 100 of the 0.0 ones is going to add up to 1/2.

27
00:01:47,940 --> 00:01:49,800
So the distance per second.

28
00:01:49,800 --> 00:01:53,340
Remember, we're saying move one to the left, every single frame.

29
00:01:53,340 --> 00:01:59,880
So one, and we're doing ten frames per second if we multiply by 0.1.

30
00:01:59,880 --> 00:02:03,300
In other words, how long is a frame we get a value of one.

31
00:02:03,300 --> 00:02:04,560
It'll move one to the left.

32
00:02:04,560 --> 00:02:09,990
If we're saying in a fast computer one frame and we're doing that 100 times per second, then we need

33
00:02:09,990 --> 00:02:14,970
to multiply that by 0.01 to continue to get our one unit.

34
00:02:14,970 --> 00:02:20,430
So that's just a little bit of an example of showing that when we multiply by this time Delta time,

35
00:02:20,430 --> 00:02:22,500
in other words, how long is each frame taking?

36
00:02:22,500 --> 00:02:27,570
We can end up with exactly the same value no matter how fast the computer is running.

37
00:02:27,570 --> 00:02:33,750
And so if I jump back over into my code here, I can find my sticker amount and my move amount.

38
00:02:33,750 --> 00:02:39,750
The steer amount, I've got a value from minus one to plus one because it's on the horizontal axis multiplying

39
00:02:39,750 --> 00:02:44,880
by the sticker amount, which at the moment I think in my code I've got as 0.05.

40
00:02:44,880 --> 00:02:45,180
Yep.

41
00:02:45,180 --> 00:02:54,090
And then I'm finally going to multiply that by time capital T, dot delta, small D and then capital

42
00:02:54,090 --> 00:02:56,130
T time, delta time.

43
00:02:56,130 --> 00:02:57,420
It's as simple as that.

44
00:02:57,420 --> 00:03:00,030
So really simple micro challenge for you here.

45
00:03:00,060 --> 00:03:04,680
Do the same for our move amount times that by time delta time.

46
00:03:06,100 --> 00:03:07,230
Pretty easy.

47
00:03:07,240 --> 00:03:08,920
That's why I didn't give this to you as a challenge.

48
00:03:08,920 --> 00:03:09,700
Slide time.

49
00:03:09,940 --> 00:03:11,380
Delta time.

50
00:03:11,380 --> 00:03:13,390
So now they're both frame rate independent.

51
00:03:13,390 --> 00:03:18,700
However, when we save this jump back over into unity, we're now going to be getting exactly the same

52
00:03:18,700 --> 00:03:19,810
behavior on all computers.

53
00:03:19,810 --> 00:03:25,930
But it's going to have messed up our tuning because we tuned it in a way where we weren't frame rate

54
00:03:25,930 --> 00:03:26,380
independent.

55
00:03:26,380 --> 00:03:31,390
So now when I move, I'm moving, but so slowly you can't even see it.

56
00:03:31,390 --> 00:03:37,270
So now we'd need to go in to change the steer speed and move speed so that our car drives properly.

57
00:03:37,270 --> 00:03:39,520
And this is what I'm going to give to you as a challenge.

58
00:03:39,520 --> 00:03:44,800
So if you will change your speed amount in the inspector to have your car respond to input as you would

59
00:03:44,800 --> 00:03:45,370
like it to.

60
00:03:45,370 --> 00:03:48,550
So in other words, to drive and steer at the right speed.

61
00:03:48,550 --> 00:03:49,270
Pause video.

62
00:03:49,270 --> 00:03:50,470
Take on that challenge now.

63
00:03:52,820 --> 00:03:53,210
Okay.

64
00:03:53,210 --> 00:04:00,500
So first I'm going to do is try increasing it a lot to say 101 hundred just to see what happens there.

65
00:04:00,650 --> 00:04:08,810
I'm going to click on play push forward backwards to fast on the movement and not terrible on the steering.

66
00:04:08,810 --> 00:04:11,320
So I'm going to iterate while in play mode.

67
00:04:11,330 --> 00:04:16,220
I'm going to bring the steer speed down, say to 20 no wrong one.

68
00:04:16,220 --> 00:04:21,380
I'm going to move the move speed down to 20 and then click back in the game window.

69
00:04:21,410 --> 00:04:22,490
Moving, moving.

70
00:04:22,490 --> 00:04:23,210
Not too bad.

71
00:04:23,210 --> 00:04:28,610
I think it's steering a little bit too slowly because it's taking a while to do a big arc like that.

72
00:04:28,610 --> 00:04:33,730
So I'm going to increase my steer speed to 200 click back in the game window.

73
00:04:33,740 --> 00:04:37,760
Now, as I drive around, it's not too bad, you know, that's pretty good.

74
00:04:37,760 --> 00:04:39,590
I could have the steer speed a little bit more.

75
00:04:39,590 --> 00:04:41,480
I think probably to 300.

76
00:04:41,480 --> 00:04:43,040
Want it to feel nice and snappy.

77
00:04:43,070 --> 00:04:47,150
Click back in the game window and remember, okay, now it's feeling kind of cool.

78
00:04:47,150 --> 00:04:48,740
So I need to remember these values.

79
00:04:48,740 --> 00:04:50,930
320 click out of play mode.

80
00:04:50,930 --> 00:04:52,730
I'll lose my values, but pretty easy.

81
00:04:52,730 --> 00:04:56,660
320 are the two values that I came up with.

82
00:04:56,690 --> 00:04:58,910
You're welcome to come up with whatever values you want.

83
00:04:58,910 --> 00:05:00,290
You don't have to do the same as mine.

84
00:05:00,290 --> 00:05:02,390
Your game should feel the way you want your game to feel.

85
00:05:02,390 --> 00:05:04,460
And that is all for this lecture.

86
00:05:04,460 --> 00:05:08,420
We've added frame rate independence by using time delta time.

87
00:05:08,420 --> 00:05:09,530
I'll see you in the next lecture.

