1
00:00:00,000 --> 00:00:00,000
Hello, guys.

2
00:00:00,000 --> 00:00:03,000
In this video, we're going to make endless generation of the level parts.

3
00:00:03,000 --> 00:00:07,000
But before we do that, let me explain how it's going to work.

4
00:00:07,000 --> 00:00:13,000
So what I'm going to do now is I'm going to have a next spawn position somewhere here.

5
00:00:13,000 --> 00:00:21,000
Let me draw the x axis so we have a spawn position and we have the player and also we have the platform.

6
00:00:22,000 --> 00:00:29,000
And this platform will have the start point, start point and the end point.

7
00:00:30,000 --> 00:00:36,000
Now, whenever players are getting closer to this point, position will generate a platform and this

8
00:00:36,000 --> 00:00:40,000
position, the spawn position with it start point.

9
00:00:41,000 --> 00:00:47,000
So the platform will be generated like this and here is the start point and end point.

10
00:00:47,000 --> 00:00:53,000
And whenever this happens, we're going to take our spawn position and move it to the point of new platform.

11
00:00:53,000 --> 00:01:00,000
So in this case, whenever platform is generated, the spawn position will be moved to the end point,

12
00:01:00,000 --> 00:01:06,000
and whenever player gets closer to this spawn position, we're going to generate a new platform over

13
00:01:06,000 --> 00:01:11,000
here and move the spawn position to the end point of the next platform.

14
00:01:11,000 --> 00:01:17,000
And whenever a player getting closer to a spawn point again, we're going to generate new platform and

15
00:01:17,000 --> 00:01:23,000
dead point and again move the spawn position to the end of the platform.

16
00:01:23,000 --> 00:01:26,000
So in this case we're going to have endless generation as we want to.

17
00:01:26,000 --> 00:01:31,000
And because parts will be different, it will do the job we need to do.

18
00:01:32,000 --> 00:01:34,000
So let me demonstrate one more time.

19
00:01:34,000 --> 00:01:42,000
So we have the platform with the start point and the end point x axis, and we have spawn position right

20
00:01:42,000 --> 00:01:43,000
here.

21
00:01:44,000 --> 00:01:49,000
Whenever player gets closer to the spawn point, we're going to take the platform and spawn it over

22
00:01:49,000 --> 00:01:50,000
here.

23
00:01:51,000 --> 00:01:55,000
And then we take the spawn position and move it here.

24
00:01:56,000 --> 00:02:03,000
And when our player gets closer again to the spawn point, we're going to take another platform and

25
00:02:03,000 --> 00:02:05,000
generate it right here.

26
00:02:06,000 --> 00:02:10,000
And then we're going to move the second point somewhere there.

27
00:02:11,000 --> 00:02:13,000
So this is the purpose of this video.

28
00:02:13,000 --> 00:02:14,000
I hope that makes sense.

29
00:02:14,000 --> 00:02:19,000
If it's not, just stay with me, you'll see how it works and very simple to understand.

30
00:02:19,000 --> 00:02:21,000
So now you know how that's going to work.

31
00:02:21,000 --> 00:02:22,000
Let's prepare the platform.

32
00:02:22,000 --> 00:02:24,000
First, we're going to delete this background in a coin.

33
00:02:24,000 --> 00:02:25,000
We don't need it.

34
00:02:25,000 --> 00:02:26,000
And this we don't need it.

35
00:02:26,000 --> 00:02:27,000
We need on the platform.

36
00:02:27,000 --> 00:02:33,000
Let's create empty parent for this and name it as level part.

37
00:02:34,000 --> 00:02:36,000
We're going to reset position to zero zero.

38
00:02:36,000 --> 00:02:37,000
Make sure you do that.

39
00:02:38,000 --> 00:02:44,000
Then we're going to create empty object under it and I'm going to name it as start point.

40
00:02:45,000 --> 00:02:46,000
Let's give it the gizmos.

41
00:02:47,000 --> 00:02:50,000
Some colorful like that is.

42
00:02:50,000 --> 00:02:53,000
Okay, then let's duplicate this and make it end point.

43
00:02:56,000 --> 00:03:01,000
Now, whenever you make a level part like this, we're going to use start point, We're going to place

44
00:03:01,000 --> 00:03:03,000
it on the left and endpoint.

45
00:03:03,000 --> 00:03:09,000
We're going to place it on the right, and that's going to be our left and right borders that we use

46
00:03:09,000 --> 00:03:11,000
to know how long is the little part?

47
00:03:11,000 --> 00:03:12,000
All right.

48
00:03:12,000 --> 00:03:16,000
And we need to make sure both of them at the zero level, you'll know why very soon.

49
00:03:16,000 --> 00:03:20,000
And basically you can make your platforms inside of this area.

50
00:03:20,000 --> 00:03:25,000
You can resize them, you can duplicate place another one like that, and then you would need to jump

51
00:03:25,000 --> 00:03:27,000
from here to here to here to here.

52
00:03:28,000 --> 00:03:29,000
Always like that.

53
00:03:29,000 --> 00:03:31,000
Let's change the pivot.

54
00:03:31,000 --> 00:03:34,000
Let's keep one platform for now.

55
00:03:34,000 --> 00:03:36,000
Just like that, it can be anywhere.

56
00:03:36,000 --> 00:03:42,000
You just need to know that this is your, let's say, zero level of your level part.

57
00:03:42,000 --> 00:03:48,000
And if you place some platforms below is going to be like below zero level, but it will be easier to

58
00:03:48,000 --> 00:03:52,000
understand when we're going to actually create level parts entirely.

59
00:03:52,000 --> 00:03:54,000
For now, let's just prepare the system, okay?

60
00:03:54,000 --> 00:03:56,000
I want to make prefab out of this.

61
00:03:56,000 --> 00:03:59,000
And if you don't know what is prefab, don't worry about it.

62
00:03:59,000 --> 00:04:01,000
We're going to talk about this later in the course.

63
00:04:01,000 --> 00:04:04,000
For now, let's just create a folder name.

64
00:04:04,000 --> 00:04:08,000
It is a prefab and drag this level part inside.

65
00:04:09,000 --> 00:04:15,000
Now we can delete it from hierarchy and we have this part, very same part over here, and we can use

66
00:04:15,000 --> 00:04:20,000
it as a template to spawn same objects whenever we want to.

67
00:04:20,000 --> 00:04:27,000
So let's drag it here to the level generator like that and go inside of the script.

68
00:04:28,000 --> 00:04:32,000
We can change this to vector three.

69
00:04:32,000 --> 00:04:35,000
Next part position.

70
00:04:37,000 --> 00:04:38,000
I'm going to save it.

71
00:04:38,000 --> 00:04:40,000
I have a habit of saving everything all the time.

72
00:04:40,000 --> 00:04:42,000
It's a good habit, actually, I guess.

73
00:04:42,000 --> 00:04:46,000
Now, let's change this position to the next part position.

74
00:04:46,000 --> 00:04:51,000
So we're going to use this next power position to spawn our platform for the first time.

75
00:04:51,000 --> 00:04:56,000
And then we're going to assign next third position equals to the end point of each level part.

76
00:04:56,000 --> 00:05:04,000
And let's do that right now, just here below, I'm going to type next part position equals to new part

77
00:05:04,000 --> 00:05:10,000
dot find and find is used, as you can see, finds a child by name and returns it.

78
00:05:10,000 --> 00:05:15,000
So basically we can use it to find an object by its name.

79
00:05:16,000 --> 00:05:20,000
Let's do that and point dot position.

80
00:05:20,000 --> 00:05:26,000
So as I said, so as I said, we're going to spawn platforms at point every time and every time it's

81
00:05:26,000 --> 00:05:27,000
going to be a new endpoint.

82
00:05:27,000 --> 00:05:32,000
Let's go back to Unity and test this and click some mouse.

83
00:05:32,000 --> 00:05:33,000
Click, click.

84
00:05:33,000 --> 00:05:35,000
Okay, we're doing it.

85
00:05:36,000 --> 00:05:38,000
It is there now.

86
00:05:38,000 --> 00:05:43,000
As you can see, this is my first platform, and my second platform is bound at the endpoint.

87
00:05:43,000 --> 00:05:48,000
But it was found right at the center, like this is the center part of the platform and the endpoint

88
00:05:48,000 --> 00:05:51,000
and we need to spawn it like this.

89
00:05:51,000 --> 00:05:57,000
To fix that, we're going to go back to script and we're going to type next part position.

90
00:05:59,000 --> 00:06:00,000
Minus.

91
00:06:01,000 --> 00:06:04,000
Part dot find.

92
00:06:05,000 --> 00:06:07,000
Start point.

93
00:06:08,000 --> 00:06:09,000
Start point.

94
00:06:10,000 --> 00:06:11,000
Position.

95
00:06:12,000 --> 00:06:14,000
So we have an x axis.

96
00:06:14,000 --> 00:06:17,000
Yeah, this is the X and we have start point.

97
00:06:17,000 --> 00:06:21,000
And this one, let's say somewhere at minus five.

98
00:06:21,000 --> 00:06:24,000
And next per position we have at zero.

99
00:06:25,000 --> 00:06:29,000
So this is third point and this is the next part position.

100
00:06:29,000 --> 00:06:36,000
So what we're going to do here is we're doing next third position minus five, and that will be equals

101
00:06:36,000 --> 00:06:38,000
to minus five.

102
00:06:38,000 --> 00:06:44,000
So in the end, this value will be equal to minus five, which is start position.

103
00:06:44,000 --> 00:06:46,000
And basically this is what we're doing every time.

104
00:06:47,000 --> 00:06:50,000
Let's go and see this in action.

105
00:06:50,000 --> 00:06:51,000
I'm going to save it.

106
00:06:51,000 --> 00:06:51,000
Boom.

107
00:06:51,000 --> 00:06:53,000
Now I'm going to spawn it again.

108
00:06:53,000 --> 00:06:54,000
Boom.

109
00:06:54,000 --> 00:06:56,000
And we can see that now.

110
00:06:57,000 --> 00:07:05,000
Endpoint and start point and point and start point of the new one are at the same position.

111
00:07:05,000 --> 00:07:06,000
Very cool.

112
00:07:06,000 --> 00:07:06,000
Right.

113
00:07:06,000 --> 00:07:12,000
Now, what is important here not to mess up is the why of the platforms.

114
00:07:12,000 --> 00:07:18,000
So at the moment our platform has start point y zero and endpoint Y zero.

115
00:07:18,000 --> 00:07:24,000
This position is very important because if you place one of them higher or lower, you're going to have

116
00:07:24,000 --> 00:07:25,000
this effect.

117
00:07:25,000 --> 00:07:31,000
And I know this because I released this course before and now, as you might know, I don't know if

118
00:07:31,000 --> 00:07:34,000
I told you before, this is a rework of the course.

119
00:07:34,000 --> 00:07:34,000
All right.

120
00:07:34,000 --> 00:07:38,000
So basically, we had eight months of beta testing with other students.

121
00:07:38,000 --> 00:07:42,000
And I know that some of them had difficulties just like by the accident.

122
00:07:42,000 --> 00:07:48,000
They move this endpoint and then there is this kind of a behavior which is not okay, but I want to

123
00:07:48,000 --> 00:07:50,000
prevent it just in case.

124
00:07:50,000 --> 00:07:56,000
So let's go back to code and for this position we're going to use only X, So no matter how high or

125
00:07:56,000 --> 00:08:01,000
low, you're going to place your endpoint and start point, they're going to be spawned at the same

126
00:08:01,000 --> 00:08:02,000
level all the time.

127
00:08:02,000 --> 00:08:03,000
Let's do that.

128
00:08:04,000 --> 00:08:13,000
We can just type here vector to new position equals to new vector to and for the x, we're going to

129
00:08:13,000 --> 00:08:18,000
use this value over here and we're going to do this dot x.

130
00:08:19,000 --> 00:08:21,000
And that ex.

131
00:08:21,000 --> 00:08:25,000
And now after a comma, we need to put y and that's going to be zero.

132
00:08:25,000 --> 00:08:28,000
And then we're going to go here and type new position.

133
00:08:30,000 --> 00:08:37,000
So it's going to always have zero Y, so Y is always going to be equal to zero and there will be no

134
00:08:37,000 --> 00:08:38,000
offset.

135
00:08:38,000 --> 00:08:40,000
Even you accidentally move it.

136
00:08:40,000 --> 00:08:41,000
Yeah, let's go check that.

137
00:08:42,000 --> 00:08:44,000
So they are at the different level.

138
00:08:47,000 --> 00:08:48,000
But if I spot them.

139
00:08:49,000 --> 00:08:50,000
They're at the same level.

140
00:08:51,000 --> 00:08:51,000
Cool.

141
00:08:52,000 --> 00:08:53,000
I like it.

142
00:08:53,000 --> 00:08:55,000
Now, how do we make it spawn when we need to?

143
00:08:55,000 --> 00:09:01,000
First, we could spawn like one thousands of platforms at the beginning and just see how it works.

144
00:09:01,000 --> 00:09:02,000
But I want to make something better.

145
00:09:02,000 --> 00:09:07,000
I want to spawn the platforms when player get in close to the spawn position.

146
00:09:07,000 --> 00:09:08,000
So let's.

147
00:09:09,000 --> 00:09:10,000
Select this.

148
00:09:10,000 --> 00:09:11,000
Click and enter.

149
00:09:13,000 --> 00:09:17,000
Extract method and we're going to name it as generate.

150
00:09:18,000 --> 00:09:19,000
Platform.

151
00:09:20,000 --> 00:09:27,000
Yeah and I want to change condition here is going to be not if but while and in the parentheses let's

152
00:09:27,000 --> 00:09:29,000
check the distance right.

153
00:09:29,000 --> 00:09:31,000
What kind of distance do we need?

154
00:09:31,000 --> 00:09:35,000
Look here we have a span position somewhere, right?

155
00:09:35,000 --> 00:09:39,000
This one we have a strong position and player is getting close.

156
00:09:39,000 --> 00:09:45,000
So we need to know how close should be player before we spawn next platform.

157
00:09:45,000 --> 00:09:49,000
So basically we need this distance.

158
00:09:50,000 --> 00:09:59,000
So let's make that first serialize field private float distance to spawn.

159
00:09:59,000 --> 00:10:05,000
And while we are here, let's create serialize field, private float distance to delete.

160
00:10:06,000 --> 00:10:09,000
And of course we need reference to the player.

161
00:10:09,000 --> 00:10:14,000
So let's do this serialized field private transform player.

162
00:10:15,000 --> 00:10:16,000
Okay.

163
00:10:16,000 --> 00:10:24,000
Now, back in the parentheses, we're going to do vector two, the distance between player transform

164
00:10:24,000 --> 00:10:25,000
position.

165
00:10:26,000 --> 00:10:27,000
And.

166
00:10:28,000 --> 00:10:31,000
Next part position.

167
00:10:31,000 --> 00:10:36,000
And we're going to check if it is less than distance to spawn.

168
00:10:38,000 --> 00:10:40,000
Okay, that's good enough.

169
00:10:40,000 --> 00:10:41,000
Now, let's go back to Unity.

170
00:10:41,000 --> 00:10:47,000
We're going to select a level generator and drag our player over here.

171
00:10:48,000 --> 00:10:53,000
Then we need to make distance to spawn maybe 50 and distance to delete.

172
00:10:53,000 --> 00:10:54,000
Let's put 50.

173
00:10:54,000 --> 00:10:56,000
Now, the first part position is 000.

174
00:10:56,000 --> 00:10:59,000
So I want to place my player above that.

175
00:10:59,000 --> 00:11:01,000
Let me see somewhere.

176
00:11:02,000 --> 00:11:04,000
Okay, So zero here.

177
00:11:04,000 --> 00:11:07,000
Maybe a bit to the right and a bit to.

178
00:11:07,000 --> 00:11:08,000
Okay.

179
00:11:08,000 --> 00:11:13,000
He's not going to run fast enough, so I will give him a speed of 20.

180
00:11:13,000 --> 00:11:14,000
And let's go.

181
00:11:17,000 --> 00:11:19,000
Okay, so he spans the platforms.

182
00:11:22,000 --> 00:11:24,000
But that's because we don't have function for that yet.

183
00:11:25,000 --> 00:11:27,000
Okay, so the platforms are there.

184
00:11:27,000 --> 00:11:28,000
Let's delete them now.

185
00:11:28,000 --> 00:11:30,000
Let's go back.

186
00:11:31,000 --> 00:11:32,000
And how are we going to do that?

187
00:11:33,000 --> 00:11:41,000
First, we're going to create a function, of course, private void, delete, platform and position

188
00:11:41,000 --> 00:11:45,000
before our platforms respond as a child of a level generator.

189
00:11:45,000 --> 00:11:46,000
So we can use that.

190
00:11:47,000 --> 00:11:54,000
We can check if this level generator has any child and if it has some, then we can delete it.

191
00:11:54,000 --> 00:11:55,000
So that would be the first check.

192
00:11:55,000 --> 00:11:56,000
So let's do this.

193
00:11:56,000 --> 00:12:04,000
If transform child count bigger than zero, then if it is, we're going to grab the first child of the

194
00:12:04,000 --> 00:12:09,000
object like an angry parent who first comes to his eyes.

195
00:12:09,000 --> 00:12:10,000
We'll get some.

196
00:12:10,000 --> 00:12:11,000
So let's do that.

197
00:12:12,000 --> 00:12:13,000
Um.

198
00:12:14,000 --> 00:12:15,000
Transform.

199
00:12:16,000 --> 00:12:20,000
BART to delete equals to transform.

200
00:12:20,000 --> 00:12:23,000
Get child zero, which is the first one.

201
00:12:24,000 --> 00:12:30,000
And then we're going to check if player is far enough from this platform.

202
00:12:31,000 --> 00:12:37,000
So let's check if Victor to the distance between player transform position.

203
00:12:38,000 --> 00:12:41,000
And BART to delete.

204
00:12:42,000 --> 00:12:46,000
Sorry Bart to delete transform position.

205
00:12:46,000 --> 00:12:47,000
Then.

206
00:12:50,000 --> 00:12:50,000
Oh, wait.

207
00:12:50,000 --> 00:12:51,000
Yeah.

208
00:12:51,000 --> 00:12:54,000
Less than distance to delete.

209
00:12:55,000 --> 00:12:57,000
Then we're going to do this, Troy.

210
00:12:58,000 --> 00:13:01,000
Bar to delete game object.

211
00:13:02,000 --> 00:13:08,000
And let's do this check just before the generating delete delete platform.

212
00:13:10,000 --> 00:13:16,000
Very cool rate, very simple short script, but so effective.

213
00:13:16,000 --> 00:13:17,000
Let's go back.

214
00:13:18,000 --> 00:13:27,000
And I want to up on my platform and just start point here and end point somewhere here.

215
00:13:27,000 --> 00:13:32,000
And remember, we don't need to worry about why because we set it to zero.

216
00:13:36,000 --> 00:13:36,000
Okay.

217
00:13:36,000 --> 00:13:41,000
We have an issue with that because this sense is the same is getting deleted before it gets generated.

218
00:13:41,000 --> 00:13:45,000
So let's make distance to delete 100.

219
00:13:49,000 --> 00:13:51,000
I did some kind of a mistake there.

220
00:13:51,000 --> 00:13:53,000
I will see what is wrong.

221
00:13:54,000 --> 00:13:55,000
Oh, yeah.

222
00:13:55,000 --> 00:13:56,000
I am so stupid.

223
00:13:57,000 --> 00:13:59,000
Should be not less, but bigger, right?

224
00:13:59,000 --> 00:14:01,000
If distance is bigger, then we delete.

225
00:14:01,000 --> 00:14:03,000
Like if he is far enough.

226
00:14:03,000 --> 00:14:03,000
I don't know why.

227
00:14:05,000 --> 00:14:06,000
If bigger.

228
00:14:08,000 --> 00:14:10,000
Make sure you put here bigger.

229
00:14:12,000 --> 00:14:13,000
Sorry, I like the stools.

230
00:14:13,000 --> 00:14:14,000
Let's go back.

231
00:14:14,000 --> 00:14:17,000
Now it can be 50 again and doesn't matter.

232
00:14:17,000 --> 00:14:18,000
So respond.

233
00:14:18,000 --> 00:14:22,000
And let me change speed to 25 and let's go.

234
00:14:25,000 --> 00:14:30,000
So now parts behind the deleted and parts in front of generated.

235
00:14:30,000 --> 00:14:31,000
Very cool.

236
00:14:31,000 --> 00:14:32,000
I like it.

237
00:14:33,000 --> 00:14:35,000
And basically we have it and let's run her.

238
00:14:36,000 --> 00:14:39,000
We can place main camera under the player.

239
00:14:41,000 --> 00:14:44,000
And make it zero or zero.

240
00:14:45,000 --> 00:14:46,000
And now if you play the game.

241
00:14:54,000 --> 00:14:55,000
And let's run there.

242
00:14:55,000 --> 00:15:00,000
Then we're going to improve it and we're going to do that in the next video.

243
00:15:00,000 --> 00:15:02,000
For now, it's enough.

244
00:15:03,000 --> 00:15:04,000
Good job today, man.

