Hello, guys.
Now that you are.
Oh, it's here.
Wait a second.
Uh, let's try this over here.
So now you are a bit more familiar with unity, and you know these buttons and that buttons.
Let's do something more advanced.
All right, So now we have a basically an endless runner with a player who runs to the right, and he
can jump.
But we don't have any way of continuing the game, as you can see.
Right.
If I'll.
And we're in this life run to the end of the platform.
We can see I will full and there is no continue to this game, let's say.
So we need the solution for that and solution for that would be to spawn platforms in front of the player.
I don't want to drop the entire script on your head right now.
I want to make it like step by step.
So let's begin with simple.
First, let's learn how to generate a platform, how to respond and object.
Right?
And I'm going to create empty object here, and I'm going to name it as level generator.
Okay, let's reset position and add component new script and we're going to name it.
Same level generator.
Let's open level generator.
And inside we're going to make serialize field private transform platform.
All right.
And that will allow us to drag an object to a field and use it later so we can drag just this object,
let's say, and we can reuse the object again and again, or we can control exactly this object.
Now, let's go to start function.
And.
We actually need position for this.
So let's make another transform serialize field private transform.
Respond position.
And now we're going to use instantiate, function.
How we do that.
We just go to start or like it can be done anywhere.
I just want to do it in the start.
Or you know what?
Let's not use the start and let's use an update with the input better.
So I'll type if input get key down E Code Mouse zero.
It's a left click of the mouse and if that happens, then let's type this instantiate.
And as you can see in the description over here it says Clones the object original and returns the clone.
Yeah.
So what we're going to do is to set object original our platform.
Then we're going to pass a position for respond and that will be our response position.
Now we need the rotation for the object and usually you don't want to change it so we can just type
transform that rotation.
We have an error here because we need to type.
Dot position because it requires transform position.
And this is just transform over here.
Now in unity, let's make sure we have platform over here and there's a response position.
Let's just create a new empty object.
Respond, boss, and I'm going to give it a gizmo so I can see it.
Something like that.
I guess I'm going to drag it here and now when I click my mouse, boom.
Oh, yeah.
I need to enter a play mode.
Of course.
Let's go to play mode.
And now I click my mouse boom platform.
If I move my response position somewhere here and click it again.
We can see we spy new platform every time.
So this is how are we going to do it?
Let's review that code again quickly and then move a bit further.
So what we have here, we have two variables, one for platform, one for position.
Then when we click our left mouse, we call, instantiate, instantiate, makes a clone of an object,
and we pass our platform as original object.
We give it a position and rotation.
Now I want to make it's bound different platforms.
How we can do that.
First, what we can do is to make an array.
Array is a kind of a list of objects, and we just need to put square brackets for that.
Now this will work as an array and let's just type over here.
Zero.
Well, now I'm going to explain why later.
Let's go back to Unity.
And here you can see our variable changed for list of the platforms.
Right.
So now we can add multiple elements here.
And each of them has an index.
It starts from zero and goes like one, two, three, four, five, and so on.
We can put different objects over here.
I want to duplicate this platform and make it different color and be like that.
And I'll just move it below.
That's, by the way, almost colors of my country.
I'm from Ukraine.
Yeah.
Now, let's take this little generator, drag platform and platform one.
As you seen, we have index of the elements here, and that's why we need a number here, because we
cannot just choose platform now.
We need to say which platform among those that are in the list.
And I don't want to choose zero.
I want to put random value.
I can do that by just typing in random equals to random range from zero to platform lengths.
And platform that lengths will take number of the last element in the list in our case, number one.
Now, instead of the zero, let's just type here random.
If you are curious why I did type integer inside of a function is to make it as a temporary value.
When you see someone creating variables inside of a function, you should know that that variables will
work only inside of that function and I cannot access this from another function.
So I do need to go and create entire variable here.
I can just do it here quickly and it will work only inside of this if statement.
Let's save it.
Go back.
See it in action.
Sorry.
Wrong button spawn.
We already have a new one and I'll not change the position.
I'll just spawn them.
So every time is different.
Cool right now that you know how it works.
I want to write this code a bit differently.
All right?
So I will just delete everything here, and I'll begin with the creating temp value for new platform
that will be sound.
Right?
So that's what I'm going to do.
Transform again into temp value with the name.
Let's just say part like a part of the level.
And actually we're not going to have platforms.
We're going to have level parts.
That includes multiple platforms.
So let's rename this tool level part with the control RR level part.
Okay, now our part now our part will be equals to level part, and we need a number to choose which
one.
So inside of square brackets I'm going to do random range from zero to level part lengths.
So every time we click a mouse, we create new parts that is equals to one of the platforms in the list.
Now let's instantiate new part and we're going to control new part later.
So better we can type transform again.
New part equals to instantiated object.
I want to put space in here so it's easier to read.
So a new part equals to instantiate.
We're going to pass an object and there's going to be part.
Then we're going to pass position and we're going to work with position later and we're going to improve
this in the next video.
Let's just step respond position.
Should be same rotation.
Rotation and we can pass an errand to this object so it won't create mass in the hierarchy.
And as well, we can use it later to delete parts.
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
transform.
Or again, I forgot to type position.
Sorry.
So when I just type transform, it means transform of the script will be chosen and transform of this
script is a level generator.
So now new platforms will be spawned under load generator.
Let's go in just to sign a few of those.
Plus plus one to.
Now back in the play mode.
I found my platforms.
Every new one and they are children of generator.
We done for this video.
I hope it makes sense to you what we did here.
And let's move to the next one.
We're going to do endless generation there.