1
00:00:03,100 --> 00:00:07,960
This lecture was starting the process of shooting, and I'm going to be shooting a bullets that look

2
00:00:07,960 --> 00:00:12,010
like myself, because that's the sprite that I had in my library and I didn't have a bullet.

3
00:00:12,010 --> 00:00:14,560
And I think it's going to be pretty cool and funny for now.

4
00:00:14,560 --> 00:00:18,020
We're just instantiating the bullet in this lecture, so let's get started.

5
00:00:18,040 --> 00:00:22,200
Originally, I wasn't planning on putting shooting into this project, but I thought, you know, it

6
00:00:22,240 --> 00:00:23,620
would be cool shooting.

7
00:00:23,620 --> 00:00:25,840
So hey presto, we're going to do some shooting.

8
00:00:25,840 --> 00:00:27,510
Hopefully that's okay with you.

9
00:00:27,520 --> 00:00:29,560
What I'll do, first of all, is have a look at the player.

10
00:00:29,560 --> 00:00:32,009
I'm going to go into the player prefab for this design.

11
00:00:32,049 --> 00:00:33,700
I'm looking in the prefab.

12
00:00:33,700 --> 00:00:39,700
What we need to say is here's a spot where the bullet will be spawning or instantiated and where it

13
00:00:39,700 --> 00:00:40,840
will be shooting from.

14
00:00:40,840 --> 00:00:44,800
You could do it just in the middle of the player, but sometimes you want to have a gun, so we'll do

15
00:00:44,800 --> 00:00:47,560
it the the quote unquote proper way of giving it a gun.

16
00:00:47,560 --> 00:00:52,750
So when we click on the player, right click Create Empty, that will create an empty game object as

17
00:00:52,750 --> 00:00:55,390
a child should be zeroed, which is good.

18
00:00:55,390 --> 00:00:58,660
And I'll rename this from game object to be Gun.

19
00:00:58,660 --> 00:01:03,550
You could also call it bullet spawn point, but you know, that's kind of what a gun is, isn't it?

20
00:01:03,550 --> 00:01:04,599
A bullet spawn point?

21
00:01:04,599 --> 00:01:09,520
So I'll click on the gun and I'll just move it a little bit to the right and a little bit down in the

22
00:01:09,520 --> 00:01:11,530
spot where I think the bullet should be flowing out.

23
00:01:11,530 --> 00:01:12,640
The character doesn't have a gun.

24
00:01:12,640 --> 00:01:14,680
It's not that sort of visual I'm looking for.

25
00:01:14,680 --> 00:01:18,130
I just want it to be starting a little bit away from the player.

26
00:01:18,130 --> 00:01:19,240
So I think that's fine.

27
00:01:19,240 --> 00:01:24,370
Now back out of the prefab, click on the little arrow next to the prefab back into our main area.

28
00:01:24,400 --> 00:01:25,210
I'm going to create a bullet.

29
00:01:25,210 --> 00:01:30,760
And for this we need a prefab so that we can instantiate it time after time when you push the trigger.

30
00:01:30,760 --> 00:01:35,590
So I didn't I don't actually have a bullet in here, so I'm going to do something a little bit dumb

31
00:01:35,590 --> 00:01:36,370
and a little bit silly.

32
00:01:36,370 --> 00:01:39,160
I'm going to use one of the player's sprites as the bullet.

33
00:01:39,160 --> 00:01:41,800
So you kind of shooting little projections of yourself.

34
00:01:41,800 --> 00:01:44,320
So let's have a look and see which one we want to use.

35
00:01:44,320 --> 00:01:49,150
You could shoot little projection and it could animate as well if you wanted to be super clever about

36
00:01:49,150 --> 00:01:49,270
it.

37
00:01:49,270 --> 00:01:53,830
There's a little extra challenge for you, but I'm going to find let's see, I think this one is fine.

38
00:01:53,830 --> 00:01:56,950
Number 22, just a little character running across the screen.

39
00:01:57,080 --> 00:01:58,390
This one's a good number, 19.

40
00:01:58,390 --> 00:02:05,170
So I'm going to grab number 19 and a drag, just that single number 19 into the hierarchy.

41
00:02:05,200 --> 00:02:07,630
I'll rename it to B Bullet.

42
00:02:07,630 --> 00:02:10,630
We need to give it a particular sorting layer so we can see it.

43
00:02:10,630 --> 00:02:12,310
I'll put it on the same layer as the player.

44
00:02:12,310 --> 00:02:13,120
That makes sense.

45
00:02:13,120 --> 00:02:15,610
You can see the player's bullet at the same layer is the player.

46
00:02:15,610 --> 00:02:18,490
We need to give it some sort of collision as well.

47
00:02:18,580 --> 00:02:23,140
So I'll add another layer and I'll call this one bullets.

48
00:02:23,740 --> 00:02:29,200
Click back on bullet, change this to be on the bullets layer and then jump over into project settings.

49
00:02:29,200 --> 00:02:31,750
So edit project settings just so we don't forget later on.

50
00:02:31,750 --> 00:02:32,800
So I don't forget later on.

51
00:02:32,800 --> 00:02:33,640
It's more the case.

52
00:02:34,210 --> 00:02:36,460
Why we want bullets to collide with ground.

53
00:02:36,460 --> 00:02:37,240
Yeah, probably.

54
00:02:37,240 --> 00:02:39,070
I want them to bounce off the walls and stuff.

55
00:02:39,070 --> 00:02:39,640
That's cool.

56
00:02:39,640 --> 00:02:40,510
And off the ladders.

57
00:02:40,900 --> 00:02:41,950
Off the ladders player.

58
00:02:41,950 --> 00:02:43,900
No background.

59
00:02:43,900 --> 00:02:46,660
No bouncing enemies.

60
00:02:46,660 --> 00:02:47,200
Yes.

61
00:02:47,200 --> 00:02:48,190
Hazards.

62
00:02:48,550 --> 00:02:49,420
Yes, might as well.

63
00:02:49,420 --> 00:02:50,230
And bullets.

64
00:02:50,920 --> 00:02:51,700
Yes, you can.

65
00:02:51,700 --> 00:02:52,930
Bullets can collide with bullets.

66
00:02:52,930 --> 00:02:53,920
I think that's fine.

67
00:02:54,010 --> 00:02:54,550
Okay.

68
00:02:54,550 --> 00:02:55,360
X out of that.

69
00:02:55,360 --> 00:02:58,840
So we've got the first step now we need to turn our bullet into a prefab.

70
00:02:58,840 --> 00:03:00,460
So click over in the prefab folder.

71
00:03:00,460 --> 00:03:03,160
We've just got the player I'm going to drag bullet down into there.

72
00:03:03,160 --> 00:03:05,140
Now we have a bullet prefab.

73
00:03:05,140 --> 00:03:06,250
It looks like the player.

74
00:03:06,250 --> 00:03:12,580
So what I'm going to do on my sprite renderer for the bullet in the in the prefab down here in the folder,

75
00:03:12,610 --> 00:03:16,510
I'm going to change the color to be some sort of red kind of color.

76
00:03:16,960 --> 00:03:17,800
And here we go.

77
00:03:17,830 --> 00:03:23,980
So you're shooting out little red bullets of yourself, so it's nice and clear what's going on.

78
00:03:23,980 --> 00:03:24,400
Okay.

79
00:03:24,400 --> 00:03:25,840
And I'll probably need to change the scale.

80
00:03:25,840 --> 00:03:30,100
We'll come back to that later on after we've actually started shooting it and flying it across the screen.

81
00:03:30,100 --> 00:03:30,880
So I've got some bullets.

82
00:03:30,880 --> 00:03:31,930
We've got the player next.

83
00:03:31,930 --> 00:03:35,470
We need to have a look at our input system to see how we're going to trigger shooting.

84
00:03:35,470 --> 00:03:41,710
If I double click on my input system, the little roadmap thing there and I've got in here already in

85
00:03:41,710 --> 00:03:46,630
action, which is fire and you can see the controls for us on our keyboard.

86
00:03:46,630 --> 00:03:49,450
I've got left Mouse, so I'm just going to use left mouse for now.

87
00:03:49,480 --> 00:03:50,560
Might do something later on.

88
00:03:50,560 --> 00:03:52,540
I might change it to a certain key on the keyboard.

89
00:03:52,540 --> 00:03:53,170
That's fine.

90
00:03:53,170 --> 00:03:53,740
For now.

91
00:03:53,740 --> 00:03:56,830
We're sitting in our keyboards working on this project, so that's cool.

92
00:03:56,830 --> 00:04:00,340
We use the mouse and we need to jump over into our player movement.

93
00:04:00,340 --> 00:04:04,660
So there's a few ways we could do this, but the way I'm going to do it is the simplest conceptually

94
00:04:04,660 --> 00:04:07,060
where we're just going to say on fire.

95
00:04:07,060 --> 00:04:09,430
Then we instantiate a bullet.

96
00:04:09,640 --> 00:04:15,820
So here is a challenge for you to spawn our bullets, specifically use instantiate to spawn our bullet

97
00:04:15,820 --> 00:04:22,780
at our player's gun when the player clicks the trigger, or in other words, we are calling on fire.

98
00:04:22,780 --> 00:04:24,160
So there's your mission.

99
00:04:24,160 --> 00:04:27,550
If you choose to accept it, I'll see you back here when you've given it a shot.

100
00:04:29,990 --> 00:04:30,500
Ryan.

101
00:04:30,500 --> 00:04:34,000
I just realized I made a really accidentally terrible pun just there.

102
00:04:34,010 --> 00:04:34,650
Give it a shot.

103
00:04:34,670 --> 00:04:39,380
I didn't mean to say that, but I guess subliminally, my dad brain just never shuts up, does it?

104
00:04:39,380 --> 00:04:39,890
So.

105
00:04:40,010 --> 00:04:40,880
Ha ha.

106
00:04:40,910 --> 00:04:41,720
Now, let's see.

107
00:04:41,750 --> 00:04:42,650
What have we got in here?

108
00:04:42,680 --> 00:04:43,340
Update on.

109
00:04:43,340 --> 00:04:44,200
Move on, jump.

110
00:04:44,210 --> 00:04:46,190
I'll put this at the top so it's nice and clear.

111
00:04:46,190 --> 00:04:48,410
So it's going to be on fire.

112
00:04:48,410 --> 00:04:54,770
And in parentheses we need to grab ourselves an input value as we've done before.

113
00:04:54,770 --> 00:04:57,770
We've got clues just down below ourselves here input value.

114
00:04:57,770 --> 00:05:02,420
And this will be value and be very similar to the jump because it's just tapping a button, right.

115
00:05:02,420 --> 00:05:03,860
So very, very similar to that.

116
00:05:03,860 --> 00:05:06,080
Get our curly braces as we've done before.

117
00:05:06,080 --> 00:05:10,010
We just need to have our if you're not alive, then you can't do these things.

118
00:05:10,010 --> 00:05:10,730
We'll pop that in there.

119
00:05:10,730 --> 00:05:12,530
So if not alive, then return.

120
00:05:12,740 --> 00:05:15,620
If you are alive, then we want to instantiate a bullet.

121
00:05:15,620 --> 00:05:16,430
So instantiate.

122
00:05:16,430 --> 00:05:17,510
What are we instantiating?

123
00:05:17,510 --> 00:05:19,070
Well, it's going to be bullet.

124
00:05:19,070 --> 00:05:21,590
We need to go and create a reference to that.

125
00:05:21,590 --> 00:05:25,280
So up at the very top we will serialize a field.

126
00:05:25,280 --> 00:05:27,050
This will be of type.

127
00:05:27,050 --> 00:05:27,710
What is this?

128
00:05:27,710 --> 00:05:30,230
I think this is going to be a game object that makes sense.

129
00:05:30,230 --> 00:05:31,130
Game object.

130
00:05:31,130 --> 00:05:33,620
And we'll call this just straight up bullet.

131
00:05:33,620 --> 00:05:35,810
So that's how we need to instantiate something.

132
00:05:35,810 --> 00:05:39,470
We need to have a reference to it, and we're going to drag that into the inspector so we know exactly

133
00:05:39,470 --> 00:05:40,580
what that's going to be.

134
00:05:40,610 --> 00:05:45,230
I'm going to pair up the bullet with the player and then also while I'm here, I'll get to this in a

135
00:05:45,230 --> 00:05:45,530
moment.

136
00:05:45,530 --> 00:05:49,790
Now instantiate method, but we also need a place to instantiate it from.

137
00:05:49,790 --> 00:05:51,440
I know that's coming in a moment.

138
00:05:51,440 --> 00:05:54,680
So I'm going to serialize field a transform.

139
00:05:54,740 --> 00:05:55,760
Call it bullet point.

140
00:05:55,760 --> 00:05:56,090
You know what?

141
00:05:56,090 --> 00:05:56,990
Let's just call it gun.

142
00:05:57,020 --> 00:05:58,460
Only called a bullet spawn.

143
00:05:58,460 --> 00:05:59,240
But what is it?

144
00:05:59,240 --> 00:06:00,380
It's the gun.

145
00:06:00,380 --> 00:06:01,340
Crazy.

146
00:06:01,340 --> 00:06:02,330
So now instantiate.

147
00:06:02,330 --> 00:06:03,860
We need to know what we're spawning.

148
00:06:03,860 --> 00:06:05,210
Where are we spawning it?

149
00:06:05,210 --> 00:06:07,280
That'll be gun dot position.

150
00:06:07,280 --> 00:06:08,600
And when do we want it?

151
00:06:08,600 --> 00:06:09,140
Now.

152
00:06:09,770 --> 00:06:10,670
That's the wrong song.

153
00:06:10,670 --> 00:06:11,480
So what?

154
00:06:11,480 --> 00:06:13,460
Where and rotation.

155
00:06:13,460 --> 00:06:16,670
So rotation would simply say transform rotation.

156
00:06:16,670 --> 00:06:20,270
So whatever rotation the bullet has when it comes to life, that's a rotation.

157
00:06:20,270 --> 00:06:22,160
We're going to use parentheses.

158
00:06:22,160 --> 00:06:24,410
Our on fire squiggle should go away.

159
00:06:24,410 --> 00:06:26,660
If I've done it right, it's not going away yet.

160
00:06:26,930 --> 00:06:29,210
That's because Rick has forgotten the return type.

161
00:06:29,210 --> 00:06:30,140
Sorry about that.

162
00:06:30,140 --> 00:06:32,090
Forgotten that a few times in this section.

163
00:06:32,090 --> 00:06:34,270
Void on fire is what we're looking for.

164
00:06:34,280 --> 00:06:36,440
Save back over into unity.

165
00:06:36,440 --> 00:06:38,690
I'm not looking for it to fly across the screen just yet.

166
00:06:38,690 --> 00:06:43,400
In this first step, we're just looking for a to come to life back over onto our player.

167
00:06:43,400 --> 00:06:47,780
We need to assign those couple of fields that we are serialized.

168
00:06:47,780 --> 00:06:49,670
So you see down here play a movement.

169
00:06:49,670 --> 00:06:51,620
We've got Bullet and we've got Gun Bullet.

170
00:06:51,620 --> 00:06:57,890
We need to grab the prefab, remember, because the prefab is going to be spawned from nothing into

171
00:06:57,890 --> 00:07:01,220
existence and the gun will be on the player.

172
00:07:01,220 --> 00:07:06,890
So we find we open up the player and we say, here is the gun that's on the player.

173
00:07:06,920 --> 00:07:08,780
Now I'm going to get rid of all my errors.

174
00:07:08,780 --> 00:07:12,800
That was from me clicking play prematurely before I'd done those things.

175
00:07:12,800 --> 00:07:20,090
Now I'll click on play No Errors anymore, and if we click on Trigger, we instantiate some little versions

176
00:07:20,090 --> 00:07:20,870
of ourselves.

177
00:07:20,870 --> 00:07:21,390
Excellent.

178
00:07:21,390 --> 00:07:23,180
That's going to be funny and interesting.

179
00:07:23,180 --> 00:07:26,750
Trust me, when we actually shoot it across the screen for now, it looks a little bit dumb, but I

180
00:07:26,750 --> 00:07:27,740
think it's kind of nifty.

181
00:07:27,740 --> 00:07:33,170
Feel free to have an actual proper bullet in there and not be spawning a sprite of yourself because

182
00:07:33,170 --> 00:07:34,730
you didn't actually have a bullet like I did.

183
00:07:34,730 --> 00:07:35,330
So there we go.

184
00:07:35,330 --> 00:07:37,760
We've got the first starting point of creating shooting.

185
00:07:37,760 --> 00:07:40,940
In the next lecture, we'll give it the meaning of flying across the screen.

