1
00:00:00,000 --> 00:00:00,000
Hello, guys.

2
00:00:00,000 --> 00:00:01,000
Now that you are.

3
00:00:01,000 --> 00:00:02,000
Oh, it's here.

4
00:00:02,000 --> 00:00:03,000
Wait a second.

5
00:00:04,000 --> 00:00:06,000
Uh, let's try this over here.

6
00:00:07,000 --> 00:00:12,000
So now you are a bit more familiar with unity, and you know these buttons and that buttons.

7
00:00:12,000 --> 00:00:14,000
Let's do something more advanced.

8
00:00:14,000 --> 00:00:18,000
All right, So now we have a basically an endless runner with a player who runs to the right, and he

9
00:00:18,000 --> 00:00:19,000
can jump.

10
00:00:19,000 --> 00:00:24,000
But we don't have any way of continuing the game, as you can see.

11
00:00:24,000 --> 00:00:25,000
Right.

12
00:00:25,000 --> 00:00:26,000
If I'll.

13
00:00:27,000 --> 00:00:30,000
And we're in this life run to the end of the platform.

14
00:00:30,000 --> 00:00:36,000
We can see I will full and there is no continue to this game, let's say.

15
00:00:36,000 --> 00:00:43,000
So we need the solution for that and solution for that would be to spawn platforms in front of the player.

16
00:00:43,000 --> 00:00:47,000
I don't want to drop the entire script on your head right now.

17
00:00:47,000 --> 00:00:50,000
I want to make it like step by step.

18
00:00:50,000 --> 00:00:51,000
So let's begin with simple.

19
00:00:51,000 --> 00:00:56,000
First, let's learn how to generate a platform, how to respond and object.

20
00:00:56,000 --> 00:00:57,000
Right?

21
00:00:57,000 --> 00:01:03,000
And I'm going to create empty object here, and I'm going to name it as level generator.

22
00:01:03,000 --> 00:01:08,000
Okay, let's reset position and add component new script and we're going to name it.

23
00:01:09,000 --> 00:01:11,000
Same level generator.

24
00:01:11,000 --> 00:01:13,000
Let's open level generator.

25
00:01:13,000 --> 00:01:19,000
And inside we're going to make serialize field private transform platform.

26
00:01:20,000 --> 00:01:21,000
All right.

27
00:01:21,000 --> 00:01:29,000
And that will allow us to drag an object to a field and use it later so we can drag just this object,

28
00:01:29,000 --> 00:01:34,000
let's say, and we can reuse the object again and again, or we can control exactly this object.

29
00:01:34,000 --> 00:01:37,000
Now, let's go to start function.

30
00:01:37,000 --> 00:01:38,000
And.

31
00:01:40,000 --> 00:01:41,000
We actually need position for this.

32
00:01:41,000 --> 00:01:46,000
So let's make another transform serialize field private transform.

33
00:01:47,000 --> 00:01:49,000
Respond position.

34
00:01:50,000 --> 00:01:52,000
And now we're going to use instantiate, function.

35
00:01:52,000 --> 00:01:53,000
How we do that.

36
00:01:53,000 --> 00:01:56,000
We just go to start or like it can be done anywhere.

37
00:01:56,000 --> 00:01:58,000
I just want to do it in the start.

38
00:01:58,000 --> 00:01:59,000
Or you know what?

39
00:01:59,000 --> 00:02:04,000
Let's not use the start and let's use an update with the input better.

40
00:02:04,000 --> 00:02:13,000
So I'll type if input get key down E Code Mouse zero.

41
00:02:13,000 --> 00:02:19,000
It's a left click of the mouse and if that happens, then let's type this instantiate.

42
00:02:19,000 --> 00:02:25,000
And as you can see in the description over here it says Clones the object original and returns the clone.

43
00:02:25,000 --> 00:02:25,000
Yeah.

44
00:02:25,000 --> 00:02:30,000
So what we're going to do is to set object original our platform.

45
00:02:30,000 --> 00:02:36,000
Then we're going to pass a position for respond and that will be our response position.

46
00:02:36,000 --> 00:02:41,000
Now we need the rotation for the object and usually you don't want to change it so we can just type

47
00:02:41,000 --> 00:02:42,000
transform that rotation.

48
00:02:43,000 --> 00:02:46,000
We have an error here because we need to type.

49
00:02:47,000 --> 00:02:51,000
Dot position because it requires transform position.

50
00:02:51,000 --> 00:02:54,000
And this is just transform over here.

51
00:02:54,000 --> 00:03:00,000
Now in unity, let's make sure we have platform over here and there's a response position.

52
00:03:00,000 --> 00:03:02,000
Let's just create a new empty object.

53
00:03:03,000 --> 00:03:08,000
Respond, boss, and I'm going to give it a gizmo so I can see it.

54
00:03:14,000 --> 00:03:15,000
Something like that.

55
00:03:15,000 --> 00:03:20,000
I guess I'm going to drag it here and now when I click my mouse, boom.

56
00:03:20,000 --> 00:03:20,000
Oh, yeah.

57
00:03:20,000 --> 00:03:21,000
I need to enter a play mode.

58
00:03:21,000 --> 00:03:22,000
Of course.

59
00:03:22,000 --> 00:03:24,000
Let's go to play mode.

60
00:03:24,000 --> 00:03:28,000
And now I click my mouse boom platform.

61
00:03:28,000 --> 00:03:31,000
If I move my response position somewhere here and click it again.

62
00:03:32,000 --> 00:03:35,000
We can see we spy new platform every time.

63
00:03:35,000 --> 00:03:37,000
So this is how are we going to do it?

64
00:03:37,000 --> 00:03:40,000
Let's review that code again quickly and then move a bit further.

65
00:03:40,000 --> 00:03:45,000
So what we have here, we have two variables, one for platform, one for position.

66
00:03:45,000 --> 00:03:51,000
Then when we click our left mouse, we call, instantiate, instantiate, makes a clone of an object,

67
00:03:51,000 --> 00:03:54,000
and we pass our platform as original object.

68
00:03:54,000 --> 00:03:56,000
We give it a position and rotation.

69
00:03:56,000 --> 00:03:59,000
Now I want to make it's bound different platforms.

70
00:03:59,000 --> 00:04:01,000
How we can do that.

71
00:04:01,000 --> 00:04:03,000
First, what we can do is to make an array.

72
00:04:04,000 --> 00:04:08,000
Array is a kind of a list of objects, and we just need to put square brackets for that.

73
00:04:08,000 --> 00:04:12,000
Now this will work as an array and let's just type over here.

74
00:04:14,000 --> 00:04:14,000
Zero.

75
00:04:14,000 --> 00:04:17,000
Well, now I'm going to explain why later.

76
00:04:17,000 --> 00:04:18,000
Let's go back to Unity.

77
00:04:18,000 --> 00:04:23,000
And here you can see our variable changed for list of the platforms.

78
00:04:23,000 --> 00:04:24,000
Right.

79
00:04:24,000 --> 00:04:27,000
So now we can add multiple elements here.

80
00:04:27,000 --> 00:04:29,000
And each of them has an index.

81
00:04:29,000 --> 00:04:33,000
It starts from zero and goes like one, two, three, four, five, and so on.

82
00:04:34,000 --> 00:04:36,000
We can put different objects over here.

83
00:04:36,000 --> 00:04:42,000
I want to duplicate this platform and make it different color and be like that.

84
00:04:43,000 --> 00:04:45,000
And I'll just move it below.

85
00:04:45,000 --> 00:04:47,000
That's, by the way, almost colors of my country.

86
00:04:47,000 --> 00:04:49,000
I'm from Ukraine.

87
00:04:49,000 --> 00:04:49,000
Yeah.

88
00:04:49,000 --> 00:04:54,000
Now, let's take this little generator, drag platform and platform one.

89
00:04:54,000 --> 00:04:59,000
As you seen, we have index of the elements here, and that's why we need a number here, because we

90
00:04:59,000 --> 00:05:01,000
cannot just choose platform now.

91
00:05:01,000 --> 00:05:05,000
We need to say which platform among those that are in the list.

92
00:05:06,000 --> 00:05:07,000
And I don't want to choose zero.

93
00:05:07,000 --> 00:05:09,000
I want to put random value.

94
00:05:09,000 --> 00:05:20,000
I can do that by just typing in random equals to random range from zero to platform lengths.

95
00:05:22,000 --> 00:05:29,000
And platform that lengths will take number of the last element in the list in our case, number one.

96
00:05:30,000 --> 00:05:33,000
Now, instead of the zero, let's just type here random.

97
00:05:33,000 --> 00:05:40,000
If you are curious why I did type integer inside of a function is to make it as a temporary value.

98
00:05:40,000 --> 00:05:45,000
When you see someone creating variables inside of a function, you should know that that variables will

99
00:05:45,000 --> 00:05:49,000
work only inside of that function and I cannot access this from another function.

100
00:05:49,000 --> 00:05:53,000
So I do need to go and create entire variable here.

101
00:05:53,000 --> 00:05:57,000
I can just do it here quickly and it will work only inside of this if statement.

102
00:05:57,000 --> 00:05:58,000
Let's save it.

103
00:05:58,000 --> 00:05:58,000
Go back.

104
00:05:58,000 --> 00:05:59,000
See it in action.

105
00:06:03,000 --> 00:06:03,000
Sorry.

106
00:06:03,000 --> 00:06:05,000
Wrong button spawn.

107
00:06:06,000 --> 00:06:09,000
We already have a new one and I'll not change the position.

108
00:06:09,000 --> 00:06:10,000
I'll just spawn them.

109
00:06:11,000 --> 00:06:13,000
So every time is different.

110
00:06:14,000 --> 00:06:16,000
Cool right now that you know how it works.

111
00:06:16,000 --> 00:06:18,000
I want to write this code a bit differently.

112
00:06:18,000 --> 00:06:19,000
All right?

113
00:06:19,000 --> 00:06:26,000
So I will just delete everything here, and I'll begin with the creating temp value for new platform

114
00:06:26,000 --> 00:06:28,000
that will be sound.

115
00:06:28,000 --> 00:06:28,000
Right?

116
00:06:28,000 --> 00:06:29,000
So that's what I'm going to do.

117
00:06:30,000 --> 00:06:33,000
Transform again into temp value with the name.

118
00:06:33,000 --> 00:06:36,000
Let's just say part like a part of the level.

119
00:06:36,000 --> 00:06:38,000
And actually we're not going to have platforms.

120
00:06:38,000 --> 00:06:39,000
We're going to have level parts.

121
00:06:39,000 --> 00:06:42,000
That includes multiple platforms.

122
00:06:42,000 --> 00:06:46,000
So let's rename this tool level part with the control RR level part.

123
00:06:46,000 --> 00:06:55,000
Okay, now our part now our part will be equals to level part, and we need a number to choose which

124
00:06:55,000 --> 00:06:55,000
one.

125
00:06:56,000 --> 00:07:03,000
So inside of square brackets I'm going to do random range from zero to level part lengths.

126
00:07:03,000 --> 00:07:12,000
So every time we click a mouse, we create new parts that is equals to one of the platforms in the list.

127
00:07:12,000 --> 00:07:16,000
Now let's instantiate new part and we're going to control new part later.

128
00:07:16,000 --> 00:07:20,000
So better we can type transform again.

129
00:07:20,000 --> 00:07:25,000
New part equals to instantiated object.

130
00:07:25,000 --> 00:07:27,000
I want to put space in here so it's easier to read.

131
00:07:27,000 --> 00:07:30,000
So a new part equals to instantiate.

132
00:07:30,000 --> 00:07:32,000
We're going to pass an object and there's going to be part.

133
00:07:32,000 --> 00:07:38,000
Then we're going to pass position and we're going to work with position later and we're going to improve

134
00:07:38,000 --> 00:07:38,000
this in the next video.

135
00:07:38,000 --> 00:07:40,000
Let's just step respond position.

136
00:07:42,000 --> 00:07:43,000
Should be same rotation.

137
00:07:44,000 --> 00:07:50,000
Rotation and we can pass an errand to this object so it won't create mass in the hierarchy.

138
00:07:50,000 --> 00:07:53,000
And as well, we can use it later to delete parts.

139
00:07:53,000 --> 00:08:01,000
So what I'm going to do here is I'm just going to type transform as a parent and it will take my current

140
00:08:01,000 --> 00:08:01,000
transform.

141
00:08:01,000 --> 00:08:03,000
Or again, I forgot to type position.

142
00:08:03,000 --> 00:08:04,000
Sorry.

143
00:08:05,000 --> 00:08:10,000
So when I just type transform, it means transform of the script will be chosen and transform of this

144
00:08:10,000 --> 00:08:12,000
script is a level generator.

145
00:08:12,000 --> 00:08:16,000
So now new platforms will be spawned under load generator.

146
00:08:18,000 --> 00:08:20,000
Let's go in just to sign a few of those.

147
00:08:22,000 --> 00:08:26,000
Plus plus one to.

148
00:08:29,000 --> 00:08:31,000
Now back in the play mode.

149
00:08:32,000 --> 00:08:34,000
I found my platforms.

150
00:08:35,000 --> 00:08:38,000
Every new one and they are children of generator.

151
00:08:39,000 --> 00:08:40,000
We done for this video.

152
00:08:40,000 --> 00:08:43,000
I hope it makes sense to you what we did here.

153
00:08:43,000 --> 00:08:44,000
And let's move to the next one.

154
00:08:44,000 --> 00:08:46,000
We're going to do endless generation there.

