Hello, guys.
So in the last video we created prefab of a coin, right?
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
level paths.
We can just place a generator and it's going to respond 5 to 10 coins for us.
Right?
So let's do this.
Let's create an empty object that's going to be named as coin generator.
I'm going to reset the position for now, and I want that new script coin generator.
All right.
Let's go inside.
Now.
We're going to use instantiate function.
You probably know it.
We did use it when we created the level parts.
Check this out.
The generator we have here, instantiate right this one.
So we're going to use it in a kind of same way.
First, let's set amount of coins, so I'm going to do serialize field private int.
Amount of coins.
Right.
And in the start, what I want to do.
Let me delete the update.
And what I want to do in the start is for loop.
So how far loop works?
And if you didn't see it before, it might feel confusing at first, but actually it's simple and it's
very useful.
So I'm going to just type four and click top twice, just like that instead of length.
So here I'm going to type.
Let's say, amount of coins.
Let's just use the amount of coins.
Now, how this works for loop runs in a cycle for a certain amount of steps, how you define how many
amount of steps.
So what it does, it takes an AI and set it to zero here.
So AI is a first step and our first step starts from zero.
Then it's going to run in cycle until I is less than amount of coins or any number you put here.
In our case, it's going to be the number we choose later in the inspector.
Right?
So let's say we chose five over here for example.
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
to do something and it's going to do a plus plus.
And on the next cycle I is going to be equals to one because we need plus plus.
Then it's going to check.
If I is less than five, then it's going to do again.
Then it's going to do a plus plus.
So it's going to run for certain amount of times.
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
I is less than 501, two, three, four.
It's a five steps from 0 to 4.
Let's try and do the back of the I first.
And instead of five, I'm going to choose amount of coins here.
Let's go back to Unity.
And look, I'm going to set five.
Open the console and hit play mode.
As I said, 0 to 4.
Now, let's try to put number one here.
How many steps is going to do?
That's right.
Only one, because zero is less than one and no more.
If you want to adjust it, you can.
You can do if less, ie less or equals the amount of coins, for example.
And you can start your AI not from zero, but from one.
All right.
So you start from one.
And until it's less or equals the amount of coins and I'm going to choose number of steps five.
Let's go.
One, two, three, four, five.
Cool.
Right.
Now, instead of debug line and let's set it to zero and here is backwards to less.
So now instead of debug, we're going to do instantiate instantiate a coin.
So let's go here and just make serialize field private game object.
Coin prefab.
Now, again, as I said, instead of debug, we're going to do instantiate.
And you see here we need an object.
So coin prefab, then we need a transformed parent.
But actually we can put some more in for here, such as transform position, then rotation.
And I'm going to do an identity.
And let's skip this one for now.
Right.
Let's just keep this one.
Because I want to approach a problem one by one and show a solution to you.
Now I'm going to go to Project Prefab, choose my coin generator and drag coin over here.
And let's say amount of coins is going to be five.
So first what I want to fix is a.
Where COIN was generated and I mean the parent of it, because at the moment I have five coins in the
hierarchy and it was just one coin generator.
But imagine I have like 50 level parts on the level and there are 50 coin generators or let's not say
50, let's say just for one, two, three, four.
Check this out.
My hierarchy is a mess.
So many coins here.
So what I can do is assign a parent, let's do this.
And as a parent, I'm going to assign current transform.
Transform.
Let's go back and check this again.
Now it's much better.
You see, it is pretty.
It is inside.
Now I'm going to delete the extra coin generators and go back.
Now, next problem we have here is a position of the coin.
So currently you can see that.
When my coins are spawned, they are all at the same place.
Check this out.
There is one coin.
There is another coin behind that and another one and so on.
So what we can do is to use AI to set the offset of the coin.
How are we going to do that?
Basically, we're going to create vector three that I'm going to call offset.
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
to pass zero because I don't want to change anything on the Y.
Now let's just add this offset to transform position plus offset.
Not.
Check this out.
I have my coin generator, and let's give it the gizmo so we can see it.
Something like this?
Yeah.
Now, let's go to play mode.
And we can see Cohen was bound with some offset.
Zero one, two, three, four.
It is good, but I want to do a bit of a change here.
I don't want to place my first coin at the zero.
I want them place my first coin on the left.
So basically my coins as pound kind of in this way.
So the amount of coins is even from the left side and right side.
It's nothing special.
It does not affect the gameplay at all.
It's just something for our convenience that I want to see in the game and I want to show you how to
do it, of course.
So what we could do is just offset not only by I, but by amount of coins as well.
So in the start I'm going to do additional offset is equals to amount of coins divided by two and I'm
just going to do here I minus additional offset.
Let's see the result, what we created here.
And say, I'm going to put 11 coins.
So now five coins on the left, five on the right, and one in the middle.
Now, basically this is done, but I want to add a bit of the randomness to this one.
So how I want to make it random, There are two options we have.
First option is to randomize a amount of coins that will be spawned.
And let's try to do that first.
Just going to do serialize field private private int mean coins.
Enter Alyse field private int max coins.
Now we're going inside and saying that amount of coins equals to random range between mean coins and
max coins.
Let's go back to unity.
And what I want to do here is to set a minimum of three coins and maximum of nine.
Let's say Let's go and check this.
Amount of coins.
Seven.
It was placed equally.
Let's try again.
Three.
Four and so on.
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
one more option.
We can create a chance to spawn.
Serialize field.
Private float.
Chance to spawn.
And I'm going to comment out this one just for time being.
And inside I'm going to say bool can spawn will be true if chance to spawn is bigger than random range
between zero and 100.
So it's going to roll a number from 0 to 100.
Then we're going to check if chance to spawn is bigger than that number.
Then confound is true.
And if it's like that then we're going to do.
If can spawn, then instantiate back in infinity.
I'm going to set a chance to spawn 50.
So here it is.
Some of the coins was found, some were not.
But I don't really like it this way.
You can keep any option you like.
What I'm going to do here is I will revert it to how it was.
I'll just delete this one and I'll delete this one.
I'll keep minimum two maximum coins.
I'll make it private so I cannot see it in the inspector anymore.
And I'll delete this one.
Now, the next step would be is to reset position to zero zero open prefab and drag it here to prefab
folder.
Now, whenever we want to place a coins on the level instead of placing a coin, we can place coin generator.
And if we want to adjust it, we can just select coin generator and choose minimum coins and maximum
coins.
So here it will be from 10 to 50 and here is going to be from 1 to 10 is just an example.
Yeah.
Boom.
So many coins we have.
Look at this.
So let's save it and go to next video.