1
00:00:00,000 --> 00:00:00,000
Hello, guys.

2
00:00:00,000 --> 00:00:04,000
So in the last video we created prefab of a coin, right?

3
00:00:04,000 --> 00:00:10,000
And in this video I want to create a coin generator so we don't have to place a coin one by one on the

4
00:00:10,000 --> 00:00:10,000
level paths.

5
00:00:10,000 --> 00:00:14,000
We can just place a generator and it's going to respond 5 to 10 coins for us.

6
00:00:14,000 --> 00:00:14,000
Right?

7
00:00:14,000 --> 00:00:15,000
So let's do this.

8
00:00:15,000 --> 00:00:19,000
Let's create an empty object that's going to be named as coin generator.

9
00:00:20,000 --> 00:00:27,000
I'm going to reset the position for now, and I want that new script coin generator.

10
00:00:27,000 --> 00:00:28,000
All right.

11
00:00:28,000 --> 00:00:29,000
Let's go inside.

12
00:00:29,000 --> 00:00:29,000
Now.

13
00:00:29,000 --> 00:00:31,000
We're going to use instantiate function.

14
00:00:31,000 --> 00:00:32,000
You probably know it.

15
00:00:32,000 --> 00:00:35,000
We did use it when we created the level parts.

16
00:00:36,000 --> 00:00:37,000
Check this out.

17
00:00:37,000 --> 00:00:42,000
The generator we have here, instantiate right this one.

18
00:00:42,000 --> 00:00:44,000
So we're going to use it in a kind of same way.

19
00:00:44,000 --> 00:00:51,000
First, let's set amount of coins, so I'm going to do serialize field private int.

20
00:00:52,000 --> 00:00:54,000
Amount of coins.

21
00:00:54,000 --> 00:00:55,000
Right.

22
00:00:55,000 --> 00:00:58,000
And in the start, what I want to do.

23
00:00:58,000 --> 00:01:00,000
Let me delete the update.

24
00:01:01,000 --> 00:01:03,000
And what I want to do in the start is for loop.

25
00:01:03,000 --> 00:01:05,000
So how far loop works?

26
00:01:05,000 --> 00:01:12,000
And if you didn't see it before, it might feel confusing at first, but actually it's simple and it's

27
00:01:12,000 --> 00:01:13,000
very useful.

28
00:01:13,000 --> 00:01:19,000
So I'm going to just type four and click top twice, just like that instead of length.

29
00:01:19,000 --> 00:01:21,000
So here I'm going to type.

30
00:01:22,000 --> 00:01:24,000
Let's say, amount of coins.

31
00:01:24,000 --> 00:01:26,000
Let's just use the amount of coins.

32
00:01:26,000 --> 00:01:34,000
Now, how this works for loop runs in a cycle for a certain amount of steps, how you define how many

33
00:01:34,000 --> 00:01:35,000
amount of steps.

34
00:01:35,000 --> 00:01:39,000
So what it does, it takes an AI and set it to zero here.

35
00:01:39,000 --> 00:01:44,000
So AI is a first step and our first step starts from zero.

36
00:01:44,000 --> 00:01:52,000
Then it's going to run in cycle until I is less than amount of coins or any number you put here.

37
00:01:52,000 --> 00:01:57,000
In our case, it's going to be the number we choose later in the inspector.

38
00:01:57,000 --> 00:01:58,000
Right?

39
00:01:58,000 --> 00:02:01,000
So let's say we chose five over here for example.

40
00:02:01,000 --> 00:02:07,000
So it's going to do I equals to zero, then it's going to check if I is less than five, it's going

41
00:02:07,000 --> 00:02:10,000
to do something and it's going to do a plus plus.

42
00:02:10,000 --> 00:02:15,000
And on the next cycle I is going to be equals to one because we need plus plus.

43
00:02:15,000 --> 00:02:16,000
Then it's going to check.

44
00:02:16,000 --> 00:02:19,000
If I is less than five, then it's going to do again.

45
00:02:19,000 --> 00:02:20,000
Then it's going to do a plus plus.

46
00:02:20,000 --> 00:02:23,000
So it's going to run for certain amount of times.

47
00:02:23,000 --> 00:02:29,000
In our case, it's going to do five steps because I is equal to zero and it's going to do as long as

48
00:02:29,000 --> 00:02:35,000
I is less than 501, two, three, four.

49
00:02:35,000 --> 00:02:37,000
It's a five steps from 0 to 4.

50
00:02:37,000 --> 00:02:39,000
Let's try and do the back of the I first.

51
00:02:41,000 --> 00:02:44,000
And instead of five, I'm going to choose amount of coins here.

52
00:02:44,000 --> 00:02:46,000
Let's go back to Unity.

53
00:02:46,000 --> 00:02:48,000
And look, I'm going to set five.

54
00:02:48,000 --> 00:02:50,000
Open the console and hit play mode.

55
00:02:53,000 --> 00:02:55,000
As I said, 0 to 4.

56
00:02:55,000 --> 00:02:59,000
Now, let's try to put number one here.

57
00:02:59,000 --> 00:03:00,000
How many steps is going to do?

58
00:03:02,000 --> 00:03:02,000
That's right.

59
00:03:02,000 --> 00:03:06,000
Only one, because zero is less than one and no more.

60
00:03:06,000 --> 00:03:08,000
If you want to adjust it, you can.

61
00:03:08,000 --> 00:03:14,000
You can do if less, ie less or equals the amount of coins, for example.

62
00:03:14,000 --> 00:03:18,000
And you can start your AI not from zero, but from one.

63
00:03:18,000 --> 00:03:18,000
All right.

64
00:03:18,000 --> 00:03:19,000
So you start from one.

65
00:03:20,000 --> 00:03:25,000
And until it's less or equals the amount of coins and I'm going to choose number of steps five.

66
00:03:25,000 --> 00:03:26,000
Let's go.

67
00:03:28,000 --> 00:03:30,000
One, two, three, four, five.

68
00:03:30,000 --> 00:03:30,000
Cool.

69
00:03:30,000 --> 00:03:31,000
Right.

70
00:03:31,000 --> 00:03:36,000
Now, instead of debug line and let's set it to zero and here is backwards to less.

71
00:03:36,000 --> 00:03:40,000
So now instead of debug, we're going to do instantiate instantiate a coin.

72
00:03:40,000 --> 00:03:46,000
So let's go here and just make serialize field private game object.

73
00:03:47,000 --> 00:03:48,000
Coin prefab.

74
00:03:49,000 --> 00:03:54,000
Now, again, as I said, instead of debug, we're going to do instantiate.

75
00:03:55,000 --> 00:03:57,000
And you see here we need an object.

76
00:03:57,000 --> 00:04:01,000
So coin prefab, then we need a transformed parent.

77
00:04:01,000 --> 00:04:07,000
But actually we can put some more in for here, such as transform position, then rotation.

78
00:04:07,000 --> 00:04:09,000
And I'm going to do an identity.

79
00:04:11,000 --> 00:04:14,000
And let's skip this one for now.

80
00:04:14,000 --> 00:04:14,000
Right.

81
00:04:14,000 --> 00:04:15,000
Let's just keep this one.

82
00:04:17,000 --> 00:04:20,000
Because I want to approach a problem one by one and show a solution to you.

83
00:04:21,000 --> 00:04:26,000
Now I'm going to go to Project Prefab, choose my coin generator and drag coin over here.

84
00:04:26,000 --> 00:04:28,000
And let's say amount of coins is going to be five.

85
00:04:32,000 --> 00:04:35,000
So first what I want to fix is a.

86
00:04:36,000 --> 00:04:41,000
Where COIN was generated and I mean the parent of it, because at the moment I have five coins in the

87
00:04:41,000 --> 00:04:44,000
hierarchy and it was just one coin generator.

88
00:04:44,000 --> 00:04:50,000
But imagine I have like 50 level parts on the level and there are 50 coin generators or let's not say

89
00:04:50,000 --> 00:04:56,000
50, let's say just for one, two, three, four.

90
00:04:57,000 --> 00:04:57,000
Check this out.

91
00:04:57,000 --> 00:04:59,000
My hierarchy is a mess.

92
00:04:59,000 --> 00:05:01,000
So many coins here.

93
00:05:01,000 --> 00:05:04,000
So what I can do is assign a parent, let's do this.

94
00:05:04,000 --> 00:05:08,000
And as a parent, I'm going to assign current transform.

95
00:05:09,000 --> 00:05:09,000
Transform.

96
00:05:14,000 --> 00:05:15,000
Let's go back and check this again.

97
00:05:18,000 --> 00:05:19,000
Now it's much better.

98
00:05:19,000 --> 00:05:20,000
You see, it is pretty.

99
00:05:20,000 --> 00:05:21,000
It is inside.

100
00:05:22,000 --> 00:05:25,000
Now I'm going to delete the extra coin generators and go back.

101
00:05:25,000 --> 00:05:28,000
Now, next problem we have here is a position of the coin.

102
00:05:28,000 --> 00:05:30,000
So currently you can see that.

103
00:05:31,000 --> 00:05:35,000
When my coins are spawned, they are all at the same place.

104
00:05:38,000 --> 00:05:39,000
Check this out.

105
00:05:39,000 --> 00:05:41,000
There is one coin.

106
00:05:41,000 --> 00:05:46,000
There is another coin behind that and another one and so on.

107
00:05:46,000 --> 00:05:51,000
So what we can do is to use AI to set the offset of the coin.

108
00:05:51,000 --> 00:05:52,000
How are we going to do that?

109
00:05:52,000 --> 00:05:57,000
Basically, we're going to create vector three that I'm going to call offset.

110
00:05:58,000 --> 00:06:04,000
We shall be equals to a new vector to and for the x, I'm going to pass my eye for the Y, I'm going

111
00:06:04,000 --> 00:06:07,000
to pass zero because I don't want to change anything on the Y.

112
00:06:07,000 --> 00:06:13,000
Now let's just add this offset to transform position plus offset.

113
00:06:16,000 --> 00:06:16,000
Not.

114
00:06:16,000 --> 00:06:17,000
Check this out.

115
00:06:17,000 --> 00:06:21,000
I have my coin generator, and let's give it the gizmo so we can see it.

116
00:06:22,000 --> 00:06:23,000
Something like this?

117
00:06:23,000 --> 00:06:23,000
Yeah.

118
00:06:24,000 --> 00:06:25,000
Now, let's go to play mode.

119
00:06:29,000 --> 00:06:32,000
And we can see Cohen was bound with some offset.

120
00:06:32,000 --> 00:06:37,000
Zero one, two, three, four.

121
00:06:37,000 --> 00:06:40,000
It is good, but I want to do a bit of a change here.

122
00:06:40,000 --> 00:06:44,000
I don't want to place my first coin at the zero.

123
00:06:44,000 --> 00:06:47,000
I want them place my first coin on the left.

124
00:06:47,000 --> 00:06:53,000
So basically my coins as pound kind of in this way.

125
00:06:53,000 --> 00:06:56,000
So the amount of coins is even from the left side and right side.

126
00:06:56,000 --> 00:06:57,000
It's nothing special.

127
00:06:57,000 --> 00:06:59,000
It does not affect the gameplay at all.

128
00:06:59,000 --> 00:07:04,000
It's just something for our convenience that I want to see in the game and I want to show you how to

129
00:07:04,000 --> 00:07:05,000
do it, of course.

130
00:07:05,000 --> 00:07:11,000
So what we could do is just offset not only by I, but by amount of coins as well.

131
00:07:11,000 --> 00:07:20,000
So in the start I'm going to do additional offset is equals to amount of coins divided by two and I'm

132
00:07:20,000 --> 00:07:24,000
just going to do here I minus additional offset.

133
00:07:27,000 --> 00:07:29,000
Let's see the result, what we created here.

134
00:07:34,000 --> 00:07:36,000
And say, I'm going to put 11 coins.

135
00:07:39,000 --> 00:07:43,000
So now five coins on the left, five on the right, and one in the middle.

136
00:07:43,000 --> 00:07:47,000
Now, basically this is done, but I want to add a bit of the randomness to this one.

137
00:07:47,000 --> 00:07:50,000
So how I want to make it random, There are two options we have.

138
00:07:50,000 --> 00:07:54,000
First option is to randomize a amount of coins that will be spawned.

139
00:07:54,000 --> 00:07:56,000
And let's try to do that first.

140
00:07:56,000 --> 00:08:02,000
Just going to do serialize field private private int mean coins.

141
00:08:03,000 --> 00:08:07,000
Enter Alyse field private int max coins.

142
00:08:08,000 --> 00:08:18,000
Now we're going inside and saying that amount of coins equals to random range between mean coins and

143
00:08:18,000 --> 00:08:19,000
max coins.

144
00:08:19,000 --> 00:08:20,000
Let's go back to unity.

145
00:08:21,000 --> 00:08:27,000
And what I want to do here is to set a minimum of three coins and maximum of nine.

146
00:08:27,000 --> 00:08:30,000
Let's say Let's go and check this.

147
00:08:31,000 --> 00:08:32,000
Amount of coins.

148
00:08:32,000 --> 00:08:33,000
Seven.

149
00:08:33,000 --> 00:08:35,000
It was placed equally.

150
00:08:35,000 --> 00:08:36,000
Let's try again.

151
00:08:39,000 --> 00:08:40,000
Three.

152
00:08:44,000 --> 00:08:45,000
Four and so on.

153
00:08:45,000 --> 00:08:50,000
And I kind of like it this way, but if you want to make it in a different way, I'm going to show you

154
00:08:50,000 --> 00:08:51,000
one more option.

155
00:08:51,000 --> 00:08:54,000
We can create a chance to spawn.

156
00:08:55,000 --> 00:08:56,000
Serialize field.

157
00:08:56,000 --> 00:08:57,000
Private float.

158
00:08:57,000 --> 00:08:59,000
Chance to spawn.

159
00:08:59,000 --> 00:09:03,000
And I'm going to comment out this one just for time being.

160
00:09:03,000 --> 00:09:11,000
And inside I'm going to say bool can spawn will be true if chance to spawn is bigger than random range

161
00:09:11,000 --> 00:09:13,000
between zero and 100.

162
00:09:13,000 --> 00:09:15,000
So it's going to roll a number from 0 to 100.

163
00:09:15,000 --> 00:09:19,000
Then we're going to check if chance to spawn is bigger than that number.

164
00:09:19,000 --> 00:09:20,000
Then confound is true.

165
00:09:20,000 --> 00:09:22,000
And if it's like that then we're going to do.

166
00:09:22,000 --> 00:09:26,000
If can spawn, then instantiate back in infinity.

167
00:09:26,000 --> 00:09:27,000
I'm going to set a chance to spawn 50.

168
00:09:30,000 --> 00:09:31,000
So here it is.

169
00:09:32,000 --> 00:09:34,000
Some of the coins was found, some were not.

170
00:09:34,000 --> 00:09:36,000
But I don't really like it this way.

171
00:09:36,000 --> 00:09:37,000
You can keep any option you like.

172
00:09:37,000 --> 00:09:41,000
What I'm going to do here is I will revert it to how it was.

173
00:09:42,000 --> 00:09:46,000
I'll just delete this one and I'll delete this one.

174
00:09:48,000 --> 00:09:51,000
I'll keep minimum two maximum coins.

175
00:09:51,000 --> 00:09:54,000
I'll make it private so I cannot see it in the inspector anymore.

176
00:09:55,000 --> 00:09:56,000
And I'll delete this one.

177
00:09:58,000 --> 00:10:07,000
Now, the next step would be is to reset position to zero zero open prefab and drag it here to prefab

178
00:10:07,000 --> 00:10:07,000
folder.

179
00:10:08,000 --> 00:10:14,000
Now, whenever we want to place a coins on the level instead of placing a coin, we can place coin generator.

180
00:10:16,000 --> 00:10:22,000
And if we want to adjust it, we can just select coin generator and choose minimum coins and maximum

181
00:10:22,000 --> 00:10:22,000
coins.

182
00:10:22,000 --> 00:10:31,000
So here it will be from 10 to 50 and here is going to be from 1 to 10 is just an example.

183
00:10:31,000 --> 00:10:32,000
Yeah.

184
00:10:38,000 --> 00:10:39,000
Boom.

185
00:10:39,000 --> 00:10:40,000
So many coins we have.

186
00:10:40,000 --> 00:10:41,000
Look at this.

187
00:10:43,000 --> 00:10:45,000
So let's save it and go to next video.

