1
00:00:00,000 --> 00:00:03,140
Welcome back, my fellow game developers.

2
00:00:03,135 --> 00:00:05,075
In this video, when we run the game,

3
00:00:05,070 --> 00:00:07,040
you'll see we have lots of errors.

4
00:00:07,035 --> 00:00:08,285
We don't fix all of them,

5
00:00:08,280 --> 00:00:09,860
but we fixed most of them.

6
00:00:09,855 --> 00:00:12,545
So I'm going to go ahead and pick up the shotgun,

7
00:00:12,540 --> 00:00:13,700
pick up some coins,

8
00:00:13,695 --> 00:00:15,225
get hit by the spikes,

9
00:00:15,225 --> 00:00:16,655
go through the door,

10
00:00:16,650 --> 00:00:18,800
go through this portal.

11
00:00:18,795 --> 00:00:20,015
And there you go.

12
00:00:20,010 --> 00:00:22,620
You can see that I'm on the other side was my shotgun,

13
00:00:22,620 --> 00:00:24,570
was my pistol with my health,

14
00:00:24,570 --> 00:00:25,590
but it's not visible

15
00:00:25,590 --> 00:00:27,360
because we haven't tweak those things.

16
00:00:27,360 --> 00:00:29,400
But if we go to doing player scroll down,

17
00:00:29,400 --> 00:00:30,740
you'll notice we had 500.

18
00:00:30,735 --> 00:00:33,555
We cut it by the spikes and it's 490,

19
00:00:33,555 --> 00:00:35,255
even though we didn't do anything.

20
00:00:35,250 --> 00:00:38,310
So let's not waste any more time and let's get started.

21
00:00:38,310 --> 00:00:41,070
The things we learn in this video are very important,

22
00:00:41,070 --> 00:00:43,190
a bit cumbersome, but very important.

23
00:00:43,190 --> 00:00:45,040
So let's get stomped.

24
00:00:45,035 --> 00:00:46,915
O k.

25
00:00:46,910 --> 00:00:50,540
So currently we can go from level to level.

26
00:00:50,540 --> 00:00:52,670
We have options where we

27
00:00:52,670 --> 00:00:56,240
choose one of the random levels that we have.

28
00:00:56,240 --> 00:00:57,620
Obviously, there are a couple

29
00:00:57,620 --> 00:00:58,790
of things that we will need to

30
00:00:58,790 --> 00:01:00,080
tweak with the appearance of

31
00:01:00,080 --> 00:01:02,080
the door and disappearance and so on.

32
00:01:02,075 --> 00:01:04,825
I feel there is still a bit of tweaking left,

33
00:01:04,820 --> 00:01:07,510
but there is a even greater problem.

34
00:01:07,505 --> 00:01:10,295
And that is once we pick up the coins,

35
00:01:10,295 --> 00:01:14,035
the weapon, and get hit by these bikes or something.

36
00:01:14,030 --> 00:01:15,100
Let me show you.

37
00:01:15,095 --> 00:01:16,645
When we pick up the shotgun,

38
00:01:16,640 --> 00:01:17,840
pick up some money,

39
00:01:17,840 --> 00:01:19,910
get hit by the spikes.

40
00:01:19,910 --> 00:01:23,530
There we go. So we can see our life is for 9500,

41
00:01:23,525 --> 00:01:25,835
we go to a number level.

42
00:01:25,835 --> 00:01:29,555
Maybe this will be, and you can see right now,

43
00:01:29,555 --> 00:01:32,065
whereas the player, if I

44
00:01:32,060 --> 00:01:35,780
look at D1 player, he is detached.

45
00:01:35,780 --> 00:01:37,490
He's not there. So you can see

46
00:01:37,490 --> 00:01:39,470
that his life is back to 50.

47
00:01:39,470 --> 00:01:41,710
If I tried to choose the shotgun,

48
00:01:41,705 --> 00:01:43,525
I cannot. And there you go.

49
00:01:43,520 --> 00:01:45,590
You can see that the coins are

50
00:01:45,590 --> 00:01:48,530
0 and that is something that we will want to

51
00:01:48,530 --> 00:01:51,380
change to make sure that our player actually

52
00:01:51,380 --> 00:01:54,380
keeps everything with him as he goes through the level.

53
00:01:54,380 --> 00:01:56,600
So let's go ahead and just do that.

54
00:01:56,600 --> 00:01:58,010
So I'm going to open up

55
00:01:58,010 --> 00:02:00,020
my scripts in here and I'm going to

56
00:02:00,020 --> 00:02:04,210
go to the player controller, scroll up.

57
00:02:04,205 --> 00:02:07,415
And what we want is obviously we want to keep

58
00:02:07,415 --> 00:02:08,995
our weapons from level to

59
00:02:08,990 --> 00:02:11,570
level and the health of the player.

60
00:02:11,570 --> 00:02:13,070
So to keep that,

61
00:02:13,070 --> 00:02:16,430
we need to make sure that we do not destroy the player.

62
00:02:16,430 --> 00:02:18,770
We can save it on some other object

63
00:02:18,770 --> 00:02:21,530
or somewhere in the project.

64
00:02:21,530 --> 00:02:23,600
But obviously there is,

65
00:02:23,600 --> 00:02:25,180
the problem was loading

66
00:02:25,175 --> 00:02:27,725
a new scene which resets everything.

67
00:02:27,725 --> 00:02:31,205
So what I'm going to do is in here I'm going to create

68
00:02:31,205 --> 00:02:35,335
a public static player controller.

69
00:02:35,330 --> 00:02:38,060
And it's going to be the instance.

70
00:02:38,060 --> 00:02:40,510
And in the instance I'm going to

71
00:02:40,505 --> 00:02:42,595
create a private or know what,

72
00:02:42,590 --> 00:02:44,920
let's just write awake.

73
00:02:44,915 --> 00:02:46,855
And in here I'm going to say that

74
00:02:46,850 --> 00:02:50,620
the instance is equal to this.

75
00:02:50,615 --> 00:02:55,175
And then in here I'm going to don't destroy on load.

76
00:02:55,175 --> 00:02:57,725
And it will be this.

77
00:02:57,725 --> 00:03:01,105
So if I save this and hover over onto don't destroy load,

78
00:03:01,100 --> 00:03:02,630
you can see that to not destroy

79
00:03:02,630 --> 00:03:05,440
the target object when loading a new scene.

80
00:03:05,435 --> 00:03:07,445
And that's exactly what we want.

81
00:03:07,445 --> 00:03:09,025
So what will happen now?

82
00:03:09,020 --> 00:03:12,590
The object or the player and our case,

83
00:03:12,590 --> 00:03:14,360
we'll keep everything that he

84
00:03:14,360 --> 00:03:16,310
had with him from scene to scene.

85
00:03:16,310 --> 00:03:17,530
It will not be destroyed,

86
00:03:17,525 --> 00:03:19,505
it will not be reinstantiated.

87
00:03:19,505 --> 00:03:21,655
But there is the problem that

88
00:03:21,650 --> 00:03:23,770
the start will not be called the Gap.

89
00:03:23,765 --> 00:03:26,135
So we will not be setting the main camera.

90
00:03:26,135 --> 00:03:27,865
We will keep the player animator,

91
00:03:27,860 --> 00:03:29,660
but the main camera will change.

92
00:03:29,660 --> 00:03:31,730
And you'll see that this will produce a lot of

93
00:03:31,730 --> 00:03:34,630
errors that we will fix as we move on.

94
00:03:34,625 --> 00:03:36,445
But for now, understand the don't

95
00:03:36,440 --> 00:03:39,260
destroy load and what it does.

96
00:03:39,260 --> 00:03:41,650
Now, I'm going to go back in here.

97
00:03:41,645 --> 00:03:44,435
And if you look at the,

98
00:03:44,435 --> 00:03:45,655
where is it, the room,

99
00:03:45,650 --> 00:03:48,230
the portal, I've changed the portal one,

100
00:03:48,230 --> 00:03:50,950
which is this one, to go to level four.

101
00:03:50,945 --> 00:03:53,015
So we can go to the cowardly.

102
00:03:53,015 --> 00:03:58,105
And let me just go to the scenes because I was

103
00:03:58,100 --> 00:03:59,860
trying something out and I turned

104
00:03:59,855 --> 00:04:03,175
off the dome player in here. So there we go.

105
00:04:03,170 --> 00:04:04,420
That be turned him on.

106
00:04:04,415 --> 00:04:05,825
Let's save that.

107
00:04:05,825 --> 00:04:14,585
Let's go back to wait for this to load any day now,

108
00:04:14,585 --> 00:04:17,275
save that back to where we're serving.

109
00:04:17,270 --> 00:04:19,490
The skull is where we have Kelly's.

110
00:04:19,490 --> 00:04:20,890
Yep, We were a skeleton.

111
00:04:20,890 --> 00:04:22,030
So save that.

112
00:04:22,025 --> 00:04:24,875
And you'll notice in the hierarchy right now,

113
00:04:24,875 --> 00:04:28,865
and unity, the new update, sorry, it's terrible.

114
00:04:28,865 --> 00:04:31,615
So in the hierarchy you'll notice that we have the don't

115
00:04:31,610 --> 00:04:35,090
destroy it up and it has the doom play or under it.

116
00:04:35,090 --> 00:04:37,150
So I'm going to pick up the shotgun,

117
00:04:37,145 --> 00:04:40,235
become the Bitcoins get hit by the spikes,

118
00:04:40,235 --> 00:04:43,205
open up the door, moved to level 4.

119
00:04:43,205 --> 00:04:44,915
And there we go.

120
00:04:44,915 --> 00:04:46,735
And you can see that now I am here,

121
00:04:46,730 --> 00:04:48,100
but the problem is,

122
00:04:48,095 --> 00:04:51,325
you can see a player down and I layer up here.

123
00:04:51,320 --> 00:04:53,350
So now we have two players.

124
00:04:53,345 --> 00:04:56,245
One of them has a shotgun, the other doesn't.

125
00:04:56,240 --> 00:05:00,140
And you can see we have a ton of errors and the cons.

126
00:05:00,140 --> 00:05:02,120
So I'm going to open up the console.

127
00:05:02,120 --> 00:05:03,970
I'm going to pause the game.

128
00:05:03,965 --> 00:05:06,295
And first of all, we have the doom player.

129
00:05:06,290 --> 00:05:09,160
And here this is a debug I was showing.

130
00:05:09,155 --> 00:05:10,435
Let me delete this.

131
00:05:10,430 --> 00:05:12,440
You'll see what I mean in just a bit.

132
00:05:12,440 --> 00:05:14,090
I'm going to delete this.

133
00:05:14,090 --> 00:05:14,600
Next.

134
00:05:14,600 --> 00:05:16,090
We will have the,

135
00:05:16,085 --> 00:05:17,785
this arrow right here,

136
00:05:17,780 --> 00:05:21,020
which is okay, so this is the problems at all.

137
00:05:21,020 --> 00:05:23,500
We'll fix this in the later video.

138
00:05:23,495 --> 00:05:25,135
Then finally we have

139
00:05:25,130 --> 00:05:27,620
the object of type camera has been destroyed.

140
00:05:27,620 --> 00:05:29,840
You are still trying to access it.

141
00:05:29,840 --> 00:05:31,310
So I'm going to double-click in

142
00:05:31,310 --> 00:05:33,200
here and it's the main camera,

143
00:05:33,200 --> 00:05:35,530
and it's exactly what I told you about.

144
00:05:35,525 --> 00:05:39,425
So now that we know that our player is transitioning,

145
00:05:39,425 --> 00:05:41,305
and we can see right here in the scene,

146
00:05:41,300 --> 00:05:43,130
if I click on the doom player,

147
00:05:43,130 --> 00:05:44,690
which is the don't destroy,

148
00:05:44,690 --> 00:05:46,070
we have to do player,

149
00:05:46,070 --> 00:05:47,570
so we'll have to fix that.

150
00:05:47,570 --> 00:05:49,850
But one of them still has the shotgun.

151
00:05:49,850 --> 00:05:51,460
So technically, he will be

152
00:05:51,455 --> 00:05:53,705
able to move with that shotgun.

153
00:05:53,705 --> 00:05:55,795
And I look on the tomb player,

154
00:05:55,790 --> 00:05:58,790
scroll down to his health and you can see that the,

155
00:05:58,790 --> 00:06:01,140
his current health has also transition.

156
00:06:01,144 --> 00:06:02,724
Unfortunately, there is still

157
00:06:02,720 --> 00:06:04,640
the problem of the bitcoins,

158
00:06:04,640 --> 00:06:06,650
but we will fix that step-by-step.

159
00:06:06,649 --> 00:06:08,569
Let's first of all start off with

160
00:06:08,570 --> 00:06:11,480
managing the double layer standards.

161
00:06:11,480 --> 00:06:13,810
So this is something that we will need to address.

162
00:06:13,805 --> 00:06:16,235
And how are we going to adjust this?

163
00:06:16,235 --> 00:06:19,555
Well, there is a very nifty trick that we can use,

164
00:06:19,550 --> 00:06:21,670
which is called a singleton pattern.

165
00:06:21,665 --> 00:06:23,425
Let me show you using

166
00:06:23,420 --> 00:06:25,940
code what that is and then we will explain.

167
00:06:25,940 --> 00:06:28,000
So first of all, in here on awake,

168
00:06:27,995 --> 00:06:30,415
instead of immediately setting the instance,

169
00:06:30,410 --> 00:06:34,670
I'm going to say that if we don't have an instance,

170
00:06:34,670 --> 00:06:37,310
so the instance is

171
00:06:37,310 --> 00:06:39,620
amines are if we already have an instance,

172
00:06:39,620 --> 00:06:41,390
so it's different than null,

173
00:06:41,390 --> 00:06:47,980
and the instance is different than this.

174
00:06:47,975 --> 00:06:50,725
Then what I'm going to do is I'm going to

175
00:06:50,720 --> 00:06:55,620
destroy this game object.

176
00:06:55,870 --> 00:06:59,420
So what am I doing exactly in here?

177
00:06:59,420 --> 00:07:02,060
I'm going to check as soon as we await.

178
00:07:02,060 --> 00:07:05,120
Is the instance different than null.

179
00:07:05,120 --> 00:07:08,230
So that means if the instance is not null,

180
00:07:08,225 --> 00:07:10,355
I'm going to destroy the game object.

181
00:07:10,355 --> 00:07:12,535
But there is another thing

182
00:07:12,530 --> 00:07:15,460
if the instance is different than this.

183
00:07:15,455 --> 00:07:17,135
So what I'm doing in here,

184
00:07:17,135 --> 00:07:19,045
as I'm starting the game off,

185
00:07:19,040 --> 00:07:22,240
I'm checking if we either have an instance already

186
00:07:22,235 --> 00:07:25,265
or the instance is equal to this game object,

187
00:07:25,265 --> 00:07:27,665
I'm going to destroy this game object.

188
00:07:27,665 --> 00:07:29,795
And this will ensure that we,

189
00:07:29,795 --> 00:07:31,145
first of all don't create

190
00:07:31,145 --> 00:07:33,475
a second instance of this game object.

191
00:07:33,470 --> 00:07:34,790
And second of all,

192
00:07:34,790 --> 00:07:38,330
we don't duplicate the object that is already in here.

193
00:07:38,330 --> 00:07:40,310
So if we go to a new scene

194
00:07:40,310 --> 00:07:42,790
and we find that we have in a game object,

195
00:07:42,785 --> 00:07:44,725
it will be destroyed immediately.

196
00:07:44,720 --> 00:07:48,290
But else, in case we don't have an instance,

197
00:07:48,290 --> 00:07:50,210
so we're starting the game all over again,

198
00:07:50,210 --> 00:07:52,310
or when we start a new scene and there

199
00:07:52,310 --> 00:07:54,850
is no instance that is equal to this,

200
00:07:54,845 --> 00:08:00,185
then I'm going to say that instance as equal to this.

201
00:08:00,185 --> 00:08:02,635
This will just create a new instance.

202
00:08:02,630 --> 00:08:04,970
And this will make sure that we do not have

203
00:08:04,970 --> 00:08:09,130
two instances or that there is already one available.

204
00:08:09,125 --> 00:08:10,855
So I'm going to save that and I'm

205
00:08:10,850 --> 00:08:12,830
going to demonstrate how this will

206
00:08:12,830 --> 00:08:16,720
work and buckle up because we will have many errors.

207
00:08:16,715 --> 00:08:18,325
And I was testing this out to make

208
00:08:18,320 --> 00:08:21,230
sure I can properly manage everything.

209
00:08:21,230 --> 00:08:22,520
So I pick up the coins,

210
00:08:22,520 --> 00:08:24,370
I get hit for 90,

211
00:08:24,365 --> 00:08:28,255
open up the door, and I'm going to hit the portal,

212
00:08:28,250 --> 00:08:30,580
move away from it. And there we go.

213
00:08:30,575 --> 00:08:32,015
So you can see right now,

214
00:08:32,015 --> 00:08:33,545
I don't have a team player.

215
00:08:33,545 --> 00:08:36,275
The second player has been destroyed.

216
00:08:36,275 --> 00:08:39,175
That's because it checked and it's awake.

217
00:08:39,170 --> 00:08:40,460
Is there an instance?

218
00:08:40,460 --> 00:08:42,920
Is instance different than null?

219
00:08:42,920 --> 00:08:44,690
Yes, it was.

220
00:08:44,690 --> 00:08:47,650
And is instance equal to this?

221
00:08:47,645 --> 00:08:51,565
Yes, it was. So we are going to go ahead and destroy.

222
00:08:51,560 --> 00:08:54,070
So back in here you'll see that we have

223
00:08:54,065 --> 00:08:56,795
only 12 employer which has a shotgun.

224
00:08:56,795 --> 00:08:57,775
He can shoot.

225
00:08:57,770 --> 00:09:00,980
He cannot point and he cannot change his weapon,

226
00:09:00,980 --> 00:09:02,780
but he does have the shotgun.

227
00:09:02,780 --> 00:09:04,690
He is animating,

228
00:09:04,685 --> 00:09:07,325
everything is working and you'll notice that

229
00:09:07,325 --> 00:09:09,565
he is in the same position that

230
00:09:09,560 --> 00:09:11,870
he was in the previous scene.

231
00:09:11,870 --> 00:09:13,550
And that's because he will actually

232
00:09:13,550 --> 00:09:15,580
keep that with him as he moves.

233
00:09:15,575 --> 00:09:17,975
So you'll notice that when I entered the portal,

234
00:09:17,975 --> 00:09:19,765
I then moved away because I didn't

235
00:09:19,760 --> 00:09:21,940
want to stay in that portal.

236
00:09:21,935 --> 00:09:23,515
So with that information,

237
00:09:23,510 --> 00:09:26,560
after all the way, Let's see what errors we have in here.

238
00:09:26,555 --> 00:09:29,425
So I'm going to pause because there is a lot.

239
00:09:29,420 --> 00:09:34,330
So first of all, you'll see the problem with the camera,

240
00:09:34,325 --> 00:09:36,215
which we will fix in just a bit.

241
00:09:36,215 --> 00:09:37,885
You'll see that we have a problem

242
00:09:37,880 --> 00:09:39,320
with the cowardly enemies.

243
00:09:39,320 --> 00:09:42,740
They are trying to find the object which is the player,

244
00:09:42,740 --> 00:09:44,330
but they simply cannot.

245
00:09:44,330 --> 00:09:46,400
Not only that, it's telling us that

246
00:09:46,400 --> 00:09:48,590
the object transform has been destroyed,

247
00:09:48,590 --> 00:09:50,720
but you are still trying to access it.

248
00:09:50,720 --> 00:09:52,250
What do they mean by that?

249
00:09:52,250 --> 00:09:55,090
Well, as soon as they came into this world,

250
00:09:55,085 --> 00:09:58,845
they saw the little doom player that was here initially.

251
00:09:58,849 --> 00:10:01,039
And then when the scene started,

252
00:10:01,040 --> 00:10:04,220
he got destroyed and they were still trying to access.

253
00:10:04,220 --> 00:10:05,930
And if we double-click in here,

254
00:10:05,930 --> 00:10:08,690
you'll notice it's right here on the player chase.

255
00:10:08,690 --> 00:10:11,380
So the whole problem is with the player Chase.

256
00:10:11,375 --> 00:10:13,625
Obviously we will be fixing that also,

257
00:10:13,625 --> 00:10:15,155
the camera will be fixed.

258
00:10:15,155 --> 00:10:17,965
And finally, this one problem is also here and

259
00:10:17,960 --> 00:10:21,110
the player game object active and hierarchy.

260
00:10:21,110 --> 00:10:24,170
So all of this will be fixed and just a bit.

261
00:10:24,170 --> 00:10:26,210
So I'm going to unpause this and let's

262
00:10:26,210 --> 00:10:28,630
handle the problems one at a time.

263
00:10:28,625 --> 00:10:33,065
So stop this, open this up, the game start.

264
00:10:33,065 --> 00:10:35,035
So open the scripts up.

265
00:10:35,030 --> 00:10:36,200
And what shall we do?

266
00:10:36,200 --> 00:10:38,030
First of all, first I think let's

267
00:10:38,030 --> 00:10:40,000
handle the problem of the main camera.

268
00:10:39,995 --> 00:10:46,195
So if we go to the player controller where we use the

269
00:10:46,190 --> 00:10:47,990
Main camera. Let's double-click

270
00:10:47,990 --> 00:10:49,760
on Main Camera and scroll down,

271
00:10:49,760 --> 00:10:51,730
see where we use it.

272
00:10:51,725 --> 00:10:52,955
We use it.

273
00:10:52,955 --> 00:10:55,445
There we go, in the pointing the gun.

274
00:10:55,445 --> 00:10:58,555
So in here I'm simply moment to check if

275
00:10:58,550 --> 00:11:02,650
the main camera is equal to null.

276
00:11:02,645 --> 00:11:04,405
That means we don't have a camera.

277
00:11:04,400 --> 00:11:07,190
I'm going to say that main camera is

278
00:11:07,190 --> 00:11:10,450
equal to camera dot main.

279
00:11:10,445 --> 00:11:12,295
And this will only happen once we

280
00:11:12,290 --> 00:11:14,330
check is the camera null.

281
00:11:14,330 --> 00:11:15,920
That means we don't have a reference.

282
00:11:15,920 --> 00:11:18,020
If it is, then we will set the main camera.

283
00:11:18,020 --> 00:11:20,800
Again. If not, we will just continue on regular,

284
00:11:20,795 --> 00:11:23,995
regularly with the pointing a gun at most.

285
00:11:23,990 --> 00:11:25,910
So back in our game will run.

286
00:11:25,910 --> 00:11:29,600
The game will take up this one, opening. Anything else?

287
00:11:29,600 --> 00:11:31,580
We will go through this.

288
00:11:31,580 --> 00:11:33,590
And on the other side,

289
00:11:33,590 --> 00:11:35,230
we shall see in the scene.

290
00:11:35,225 --> 00:11:38,155
There we go. You can see that I'm able to point this out,

291
00:11:38,150 --> 00:11:39,740
but obviously there is,

292
00:11:39,740 --> 00:11:41,650
the camera in the game world

293
00:11:41,645 --> 00:11:43,325
isn't focusing on the player.

294
00:11:43,325 --> 00:11:44,785
And this is something that we will

295
00:11:44,780 --> 00:11:46,910
obviously need to flex.

296
00:11:46,910 --> 00:11:48,670
So let's get to fixing.

297
00:11:48,665 --> 00:11:51,175
I'm going to go into my scripts.

298
00:11:51,170 --> 00:11:54,500
And in here while we're shall we call this,

299
00:11:54,500 --> 00:11:57,140
Where should we call this?

300
00:11:57,140 --> 00:12:00,380
Let's see, where did we put the shape camera?

301
00:12:00,380 --> 00:12:02,170
Can we know?

302
00:12:02,165 --> 00:12:04,525
We don't know what?

303
00:12:04,520 --> 00:12:05,540
Let's just go into

304
00:12:05,540 --> 00:12:09,360
the scripts and put it inside of the managers.

305
00:12:10,090 --> 00:12:13,760
Managers, managers, managers.

306
00:12:13,760 --> 00:12:15,050
There is no managers.

307
00:12:15,050 --> 00:12:15,410
Okay.

308
00:12:15,410 --> 00:12:17,720
So managers for some reason, our cameras.

309
00:12:17,720 --> 00:12:19,580
There we go. So and the cameras,

310
00:12:19,580 --> 00:12:22,520
I'm going to create a camera control

311
00:12:22,520 --> 00:12:26,540
or camera camp controller.

312
00:12:26,540 --> 00:12:29,930
Hit Enter, wait for it to compile.

313
00:12:29,930 --> 00:12:32,120
Double-click in here, and

314
00:12:32,120 --> 00:12:33,940
I'm going to do a couple of things.

315
00:12:33,935 --> 00:12:36,715
So what? I'm always doing a couple of things.

316
00:12:36,710 --> 00:12:39,010
So what are the things that we are going to do?

317
00:12:39,005 --> 00:12:41,375
First of all, what we want to do in here is

318
00:12:41,375 --> 00:12:44,035
always find the player target automatically.

319
00:12:44,030 --> 00:12:46,970
So previously what we used to do is if we went into

320
00:12:46,970 --> 00:12:50,380
the camera right here and to the virtual camp,

321
00:12:50,375 --> 00:12:52,205
we said the follow player.

322
00:12:52,205 --> 00:12:54,935
And the problem is when we go to a new world,

323
00:12:54,935 --> 00:12:56,695
let me show you in detail,

324
00:12:56,695 --> 00:12:58,075
should have shown you before.

325
00:12:58,070 --> 00:12:59,450
So if I, for example,

326
00:12:59,450 --> 00:13:00,650
here I have the player,

327
00:13:00,650 --> 00:13:02,690
I click on him, he's in the hierarchy.

328
00:13:02,690 --> 00:13:04,100
I opened up the door,

329
00:13:04,100 --> 00:13:06,410
I call to another level.

330
00:13:06,410 --> 00:13:09,830
You'll notice right now if I open up the cameras again,

331
00:13:09,830 --> 00:13:12,440
which are exactly the same virtual camera,

332
00:13:12,440 --> 00:13:14,390
you can see that the follow is missing,

333
00:13:14,390 --> 00:13:15,590
so there is no player.

334
00:13:15,590 --> 00:13:18,470
Why? Because we have set the player before,

335
00:13:18,470 --> 00:13:20,290
but now he was destroyed.

336
00:13:20,285 --> 00:13:23,485
He's missing, so we need to find them all over again.

337
00:13:23,480 --> 00:13:26,720
So in here I'm going to open this up and I'm going

338
00:13:26,720 --> 00:13:30,190
to create a variable which is the player to chase,

339
00:13:30,185 --> 00:13:32,125
which is obviously going to be,

340
00:13:32,120 --> 00:13:36,880
let's see, the transform or no atlas just make him the

341
00:13:36,875 --> 00:13:42,425
layer controller layer to look at.

342
00:13:42,425 --> 00:13:46,235
And he's going to be equal to find an object of type.

343
00:13:46,235 --> 00:13:48,175
The player, oh, no,

344
00:13:48,170 --> 00:13:49,910
no, no, not here.

345
00:13:49,910 --> 00:13:52,010
Costs down and start.

346
00:13:52,010 --> 00:13:54,500
We are going to say that the layer to look

347
00:13:54,500 --> 00:13:57,760
at is equal to find objects of type,

348
00:13:57,755 --> 00:14:00,695
and it's going to be the player controller.

349
00:14:00,695 --> 00:14:02,305
Now we have a reference to

350
00:14:02,300 --> 00:14:03,860
the player controller that's

351
00:14:03,860 --> 00:14:06,160
also create a virtual camera.

352
00:14:06,155 --> 00:14:08,755
So, so cinema machine,

353
00:14:08,750 --> 00:14:11,590
virtual camera also inherit we need to

354
00:14:11,585 --> 00:14:15,005
double and C using cin, a machine.

355
00:14:15,005 --> 00:14:16,585
There we go, Save that.

356
00:14:16,580 --> 00:14:18,920
So cinema machine,

357
00:14:18,920 --> 00:14:24,820
cinema machine virtual camera, thank you.

358
00:14:24,815 --> 00:14:28,565
Is going to be the verb.

359
00:14:30,540 --> 00:14:33,090
Virtual camera.

360
00:14:33,085 --> 00:14:36,345
And we're going to set it in here as soon as we start.

361
00:14:36,340 --> 00:14:40,080
So it's going to be equal to get component.

362
00:14:40,075 --> 00:14:42,115
And where are we engineer?

363
00:14:42,115 --> 00:14:44,775
Where shall we attach the camera controller?

364
00:14:44,770 --> 00:14:47,610
We might want to attach it in the lecture.

365
00:14:47,605 --> 00:14:49,335
We will attach to the virtual cameras

366
00:14:49,330 --> 00:14:50,350
so we can immediately

367
00:14:50,350 --> 00:14:53,880
access the same machine virtual camera in here.

368
00:14:53,875 --> 00:14:57,235
So get component.

369
00:14:57,235 --> 00:15:05,215
And it's going to be the cinema machine virtual camera.

370
00:15:05,215 --> 00:15:08,835
There we go. And what are we going to do in here?

371
00:15:08,830 --> 00:15:12,390
Well, we are going to set the virtual camera up

372
00:15:12,385 --> 00:15:14,755
top follow to be equal to

373
00:15:14,750 --> 00:15:18,520
the layer to look at dot transform.

374
00:15:18,515 --> 00:15:20,495
So this will be at the start,

375
00:15:20,495 --> 00:15:23,635
but an update, we will need to create a while loop.

376
00:15:23,630 --> 00:15:26,030
So we've covered already while loops.

377
00:15:26,030 --> 00:15:29,330
And it's going to be checking for the player

378
00:15:29,330 --> 00:15:32,600
to look at while he is equal to null.

379
00:15:32,600 --> 00:15:35,110
That means we haven't found a player yet.

380
00:15:35,105 --> 00:15:39,055
We are going to keep searching for the player to look at.

381
00:15:39,050 --> 00:15:41,740
Keep finding the player controller

382
00:15:41,735 --> 00:15:43,615
until we finally find them.

383
00:15:43,610 --> 00:15:44,810
Once we find them,

384
00:15:44,810 --> 00:15:48,490
if we do have a virtual camera.

385
00:15:48,485 --> 00:15:52,355
So this is just an extra step to avoid any errors.

386
00:15:52,355 --> 00:15:55,815
The virtual camera not follow.

387
00:15:55,815 --> 00:15:58,935
Will be equal to the player.

388
00:15:58,930 --> 00:16:02,640
We will save that. And let's look again once what we did.

389
00:16:02,635 --> 00:16:03,975
So first of all, we created

390
00:16:03,970 --> 00:16:06,190
the player controller, which will,

391
00:16:06,190 --> 00:16:07,540
which is a variable that we'll

392
00:16:07,540 --> 00:16:09,250
look at players to look at,

393
00:16:09,250 --> 00:16:10,300
which is a variable of

394
00:16:10,300 --> 00:16:12,910
the player controller, a virtual camera.

395
00:16:12,910 --> 00:16:14,560
These are all private.

396
00:16:14,560 --> 00:16:15,690
We can name the private.

397
00:16:15,685 --> 00:16:17,385
I just didn't have the time.

398
00:16:17,380 --> 00:16:19,260
You know what, let's just make sure

399
00:16:19,255 --> 00:16:22,005
that we don't have any confusions in here.

400
00:16:22,000 --> 00:16:23,380
So private, private because we

401
00:16:23,380 --> 00:16:24,940
won't need them in the inspector.

402
00:16:24,940 --> 00:16:26,110
We can set M if we want.

403
00:16:26,110 --> 00:16:28,060
We don't have to. Then start.

404
00:16:28,055 --> 00:16:30,405
We are going to find the player controller.

405
00:16:30,400 --> 00:16:31,470
This is just in case.

406
00:16:31,465 --> 00:16:33,795
We start off and we have the player in the scene,

407
00:16:33,790 --> 00:16:35,310
then we find the camera,

408
00:16:35,305 --> 00:16:38,525
then we set the follow cam to be the player to look at.

409
00:16:38,520 --> 00:16:41,090
Have only one player in any scene,

410
00:16:41,090 --> 00:16:44,660
because we've already made sure of this right here.

411
00:16:44,660 --> 00:16:46,510
And the Singleton pattern.

412
00:16:46,505 --> 00:16:47,305
There we go.

413
00:16:47,300 --> 00:16:50,050
This is called the singleton pattern, keeping in mind,

414
00:16:50,045 --> 00:16:51,655
whenever you tell it to somebody,

415
00:16:51,650 --> 00:16:53,470
they will think that you're very smart.

416
00:16:53,465 --> 00:16:55,285
Trust me, even though it's

417
00:16:55,280 --> 00:16:57,680
singleton means a single object.

418
00:16:57,680 --> 00:17:00,920
So just in case they ask you what is a singleton pattern?

419
00:17:00,920 --> 00:17:03,020
Anyways, the virtual camera dot follow,

420
00:17:03,020 --> 00:17:05,570
we follow the only player that we find and

421
00:17:05,570 --> 00:17:08,540
then an update just in case this is for,

422
00:17:08,540 --> 00:17:11,540
let's say the player which doesn't get

423
00:17:11,540 --> 00:17:15,110
destroyed usually loads before all the other objects.

424
00:17:15,110 --> 00:17:17,690
So just in case we start off the game,

425
00:17:17,690 --> 00:17:20,870
we don't find the player immediately he is missing.

426
00:17:20,870 --> 00:17:22,730
We cannot solve him as follows.

427
00:17:22,730 --> 00:17:25,640
Follow then an update we will keep looping through

428
00:17:25,640 --> 00:17:27,140
until we find the player would check

429
00:17:27,140 --> 00:17:28,880
if we have a virtual camera.

430
00:17:28,880 --> 00:17:30,380
This is just an extra step.

431
00:17:30,380 --> 00:17:32,150
I don't think we will need this even,

432
00:17:32,150 --> 00:17:33,950
not even once because we will be

433
00:17:33,950 --> 00:17:36,680
attaching the script to the virtual camera,

434
00:17:36,680 --> 00:17:40,510
but just in case there isn't a virtual camera,

435
00:17:40,505 --> 00:17:42,475
we will check the condition and then we

436
00:17:42,470 --> 00:17:44,590
will set the virtual camera dot follow,

437
00:17:44,585 --> 00:17:46,435
save that back in here.

438
00:17:46,430 --> 00:17:47,620
Let's go ahead.

439
00:17:47,615 --> 00:17:51,805
And so the cameras,

440
00:17:51,800 --> 00:17:53,510
Let's see, shake and

441
00:17:53,510 --> 00:17:57,250
shake the camera controller through the virtual camera.

442
00:17:57,245 --> 00:18:00,705
And for some reason I can see that this is not a prefab.

443
00:18:00,709 --> 00:18:03,049
Is that on prefab this.

444
00:18:03,050 --> 00:18:07,010
So when the prefabs University,

445
00:18:07,010 --> 00:18:11,510
whereas the weapons you are a poor to level objects here.

446
00:18:11,510 --> 00:18:12,290
Nope.

447
00:18:12,290 --> 00:18:14,960
Okay. So I can see I did not prefer

448
00:18:14,960 --> 00:18:17,750
up the cameras for some reason I thought I did.

449
00:18:17,750 --> 00:18:21,490
But anyways, that's just prefer the camps and here.

450
00:18:21,485 --> 00:18:23,215
And you can see that

451
00:18:23,210 --> 00:18:25,980
the virtual camera will have the camp controller.

452
00:18:25,984 --> 00:18:27,114
So save that.

453
00:18:27,110 --> 00:18:28,870
Let me go to the other level.

454
00:18:28,865 --> 00:18:30,835
I'm not going to do this to all the levels.

455
00:18:30,830 --> 00:18:33,640
I'm just going to do it with the ones that we're testing,

456
00:18:33,635 --> 00:18:36,775
fixed everything and it will probably be the same.

457
00:18:36,770 --> 00:18:39,770
So cameras and here I'm going to delete it.

458
00:18:39,770 --> 00:18:43,400
And I'm going to go back to the prefabs and make

459
00:18:43,400 --> 00:18:47,270
sure I add the cameras in here in the hierarchy.

460
00:18:47,270 --> 00:18:48,430
Save all of that.

461
00:18:48,425 --> 00:18:50,005
Open this up, okay, seems,

462
00:18:50,000 --> 00:18:53,000
everything seems to be good. Back in the scenes.

463
00:18:53,000 --> 00:18:58,450
Back in the skull is save that, run the game.

464
00:18:58,445 --> 00:19:01,375
Go to the door, opened it up,

465
00:19:01,370 --> 00:19:03,050
move to the next level.

466
00:19:03,050 --> 00:19:05,650
And we should see, there we go.

467
00:19:05,645 --> 00:19:07,555
So we again found the player and now

468
00:19:07,550 --> 00:19:10,540
the cameras are working perfectly fine.

469
00:19:10,535 --> 00:19:12,655
Great. So now with that out of the way,

470
00:19:12,650 --> 00:19:14,030
if we look back in the console,

471
00:19:14,030 --> 00:19:16,220
we still have the problem of

472
00:19:16,220 --> 00:19:18,950
the player getting chased, not being fun.

473
00:19:18,950 --> 00:19:20,660
And because I see that

474
00:19:20,660 --> 00:19:23,410
this video has been already going long enough,

475
00:19:23,405 --> 00:19:25,765
I'm going to posit right here and then we're

476
00:19:25,760 --> 00:19:28,180
going to continue on in the next video.

477
00:19:28,175 --> 00:19:29,385
So I hope you enjoyed.

478
00:19:29,380 --> 00:19:31,970
This is a bit of a boring subject, I know,

479
00:19:31,970 --> 00:19:34,790
but at least you learned what a singleton pattern is.

480
00:19:34,790 --> 00:19:36,920
You understood why the camera was not going.

481
00:19:36,920 --> 00:19:38,630
I am trying to explain as much as

482
00:19:38,630 --> 00:19:40,790
possible even though it's very boring.

483
00:19:40,790 --> 00:19:44,180
But I want you to understand how everything is feeling.

484
00:19:44,180 --> 00:19:45,920
At first, I was also confused,

485
00:19:45,920 --> 00:19:47,900
but as I went through everything,

486
00:19:47,900 --> 00:19:49,670
I understood the things and I

487
00:19:49,670 --> 00:19:51,320
wanted to pass them on to you.

488
00:19:51,320 --> 00:19:52,940
So with that out of the way,

489
00:19:52,940 --> 00:19:56,610
thank you for watching and I'll see you in the next one.

