1
00:00:00,000 --> 00:00:04,160
Welcome back, my fellow game developers.

2
00:00:04,155 --> 00:00:08,045
In this video, we are going to add automatic firing.

3
00:00:08,040 --> 00:00:11,600
So you can see right here that we have a time between shots.

4
00:00:11,595 --> 00:00:14,195
How much time do you want between shots? I can hear you.

5
00:00:14,190 --> 00:00:15,770
You want to say 0.5?

6
00:00:15,765 --> 00:00:17,595
Okay, sir, There you go.

7
00:00:17,595 --> 00:00:18,615
We play run.

8
00:00:18,615 --> 00:00:19,995
We had the Play button.

9
00:00:19,995 --> 00:00:21,515
We run the gain,

10
00:00:21,510 --> 00:00:24,390
and we hold down the left mouse button.

11
00:00:24,390 --> 00:00:27,480
And there you go. You can see that we should

12
00:00:27,480 --> 00:00:31,080
every single frame and you can see that they are very consistent.

13
00:00:31,080 --> 00:00:35,180
So I'm not playing a game and here I'm holding down the button and it

14
00:00:35,175 --> 00:00:39,855
keeps firing every 0.5 seconds or each have second,

15
00:00:39,855 --> 00:00:41,335
we keep firing able.

16
00:00:41,335 --> 00:00:43,405
So let's not waste any more time.

17
00:00:43,400 --> 00:00:46,420
We're going to learn about counters.

18
00:00:46,415 --> 00:00:48,955
And let's get started.

19
00:00:48,950 --> 00:00:50,750
O k.

20
00:00:50,750 --> 00:00:53,440
So we want to have the ability to

21
00:00:53,435 --> 00:00:58,135
automatically fire bullets when the mouse button is held down.

22
00:00:58,130 --> 00:01:01,190
So currently whenever we press the button,

23
00:01:01,190 --> 00:01:03,020
once we can shoot,

24
00:01:03,020 --> 00:01:05,200
but what we want is to have some kind of

25
00:01:05,195 --> 00:01:08,935
automatic firing while we are holding down the button,

26
00:01:08,930 --> 00:01:11,060
just like a rifle,

27
00:01:11,060 --> 00:01:14,180
even though we are using a shotgun right here,

28
00:01:14,180 --> 00:01:18,470
we'll just use our imagination and imagine that this is an automatic shotgun.

29
00:01:18,470 --> 00:01:22,570
And while we're holding down the button, it keeps firing.

30
00:01:22,565 --> 00:01:25,685
So how are we going to do this?

31
00:01:25,685 --> 00:01:27,685
Currently, if you look right here,

32
00:01:27,680 --> 00:01:29,350
you can see that this method,

33
00:01:29,345 --> 00:01:31,085
the get mouse button down,

34
00:01:31,085 --> 00:01:36,355
returns a true during the frame the user pressed the given mouse button.

35
00:01:36,350 --> 00:01:39,980
So it only returns true once in

36
00:01:39,980 --> 00:01:43,790
the frame that the player or the user has pressed the button.

37
00:01:43,790 --> 00:01:49,060
What we want is we want to use something called the input dot get mouse button.

38
00:01:49,055 --> 00:01:52,525
So notice this was mouse button down.

39
00:01:52,520 --> 00:01:55,520
This is the get mouse button.

40
00:01:55,520 --> 00:01:57,050
And if you read the description,

41
00:01:57,050 --> 00:02:02,830
you can see that it returns a true whether the given mouse button is held down.

42
00:02:02,825 --> 00:02:05,005
So while we are holding down,

43
00:02:05,000 --> 00:02:06,640
this keeps returning true.

44
00:02:06,635 --> 00:02:12,055
And this will allow us to keep instantiating bullets all the time, every single frame.

45
00:02:12,050 --> 00:02:15,230
But we come up to a problem.

46
00:02:15,230 --> 00:02:17,620
You've noticed that when we run the game,

47
00:02:17,615 --> 00:02:20,585
if you remember, we can see the stats right here.

48
00:02:20,585 --> 00:02:25,795
So we have about 800 to a thousand frames per second.

49
00:02:25,790 --> 00:02:27,670
So every single frame,

50
00:02:27,665 --> 00:02:30,145
while we are holding down the mouse button,

51
00:02:30,140 --> 00:02:32,930
we are going to be instantiating a bullet,

52
00:02:32,930 --> 00:02:34,400
which is very bad.

53
00:02:34,400 --> 00:02:38,740
So we are going to have around the 1000 bullets every single second.

54
00:02:38,735 --> 00:02:39,985
Now, it's fun.

55
00:02:39,980 --> 00:02:41,920
It's a very OOP weapon,

56
00:02:41,915 --> 00:02:43,955
but that's not what we want.

57
00:02:43,955 --> 00:02:47,515
We want to have a certain time between

58
00:02:47,510 --> 00:02:52,360
firing each book while we are holding down just like a real weapon.

59
00:02:52,355 --> 00:02:53,665
What's the mattering here?

60
00:02:53,660 --> 00:02:54,770
Okay, So no worries.

61
00:02:54,770 --> 00:02:57,590
I think this will sort itself out. Anyways.

62
00:02:57,590 --> 00:02:58,580
Let's start off.

63
00:02:58,580 --> 00:03:02,230
The first thing we're going to do is we'll create a serialized field,

64
00:03:02,225 --> 00:03:04,265
which is going to be a float,

65
00:03:04,265 --> 00:03:08,685
and it's going to be the time between shots.

66
00:03:08,689 --> 00:03:17,569
A second variable that we'll have as a private float and the shot counter.

67
00:03:17,570 --> 00:03:20,950
And what we'll do is as soon as we start,

68
00:03:20,945 --> 00:03:25,645
we are going to set the shot counter to be around 0,

69
00:03:25,640 --> 00:03:30,850
or we can set it immediately in here instead of setting it and the start.

70
00:03:30,845 --> 00:03:33,215
So we start off at 0.

71
00:03:33,215 --> 00:03:35,435
Now if we scroll down in here,

72
00:03:35,435 --> 00:03:38,905
we are going to create another condition.

73
00:03:38,900 --> 00:03:47,220
And we'll say that if the input dot get mouse button,

74
00:03:47,224 --> 00:03:51,564
which returns whether the given modes button is held down.

75
00:03:51,560 --> 00:03:56,030
And we also use the index 0 for the left mouse button.

76
00:03:56,030 --> 00:04:00,620
We are going to start using the shot counter and

77
00:04:00,620 --> 00:04:06,250
removing from it the time dot delta time.

78
00:04:06,245 --> 00:04:10,015
So do you remember when we use the plus equals

79
00:04:10,010 --> 00:04:13,790
somewhere up here when we were using the transform,

80
00:04:13,790 --> 00:04:14,960
now we don't have it.

81
00:04:14,960 --> 00:04:17,690
Well, this is similar to the plus equals.

82
00:04:17,690 --> 00:04:24,010
So this represents the shot counter minus Time.deltaTime.

83
00:04:24,005 --> 00:04:28,135
And you know what, I'm going to duplicate this, comment this out.

84
00:04:28,130 --> 00:04:33,320
And then I will make this a minuss equals.

85
00:04:33,320 --> 00:04:36,830
So you'll remember that these are the same things.

86
00:04:36,830 --> 00:04:42,170
And I'll even move it down next to the line right here.

87
00:04:42,170 --> 00:04:46,790
You can keep this as a reminder of why we are setting minus equals,

88
00:04:46,790 --> 00:04:48,880
plus equals and all of these things.

89
00:04:48,875 --> 00:04:52,975
It's much easier to read and it's much cooler

90
00:04:52,970 --> 00:04:54,890
and will make you much more professional

91
00:04:54,890 --> 00:04:57,050
looking whenever we are showing someone your game.

92
00:04:57,050 --> 00:04:59,000
So let's continue one,

93
00:04:59,000 --> 00:05:00,440
what do we need in here?

94
00:05:00,440 --> 00:05:02,750
While we are holding down the mouse button,

95
00:05:02,750 --> 00:05:06,160
the shot counter is being counted down.

96
00:05:06,155 --> 00:05:12,085
So what we want is whenever this shot counter ends or becomes 0,

97
00:05:12,080 --> 00:05:15,040
we want to fire a bullet and then

98
00:05:15,035 --> 00:05:19,625
refresh the shot counter back to the time between shots.

99
00:05:19,625 --> 00:05:22,525
So how are we going to do this?

100
00:05:22,520 --> 00:05:25,150
Instead of immediately instantiating,

101
00:05:25,145 --> 00:05:27,575
I'm going to issue a challenge.

102
00:05:27,575 --> 00:05:29,945
Was that a better transition than last time?

103
00:05:29,945 --> 00:05:33,875
Anyways, your challenge is to check if the counter has,

104
00:05:33,875 --> 00:05:39,045
and so create an if condition that checks the shot counter.

105
00:05:39,049 --> 00:05:43,489
If the shot counter is less than or equal to 0,

106
00:05:43,490 --> 00:05:45,530
why is it less than or equal to 0?

107
00:05:45,530 --> 00:05:48,770
You might be asking why not immediately 0?

108
00:05:48,770 --> 00:05:51,600
Well, because as we're counting.

