Hello, guys.
In this video, we're going to make endless generation of the level parts.
But before we do that, let me explain how it's going to work.
So what I'm going to do now is I'm going to have a next spawn position somewhere here.
Let me draw the x axis so we have a spawn position and we have the player and also we have the platform.
And this platform will have the start point, start point and the end point.
Now, whenever players are getting closer to this point, position will generate a platform and this
position, the spawn position with it start point.
So the platform will be generated like this and here is the start point and end point.
And whenever this happens, we're going to take our spawn position and move it to the point of new platform.
So in this case, whenever platform is generated, the spawn position will be moved to the end point,
and whenever player gets closer to this spawn position, we're going to generate a new platform over
here and move the spawn position to the end point of the next platform.
And whenever a player getting closer to a spawn point again, we're going to generate new platform and
dead point and again move the spawn position to the end of the platform.
So in this case we're going to have endless generation as we want to.
And because parts will be different, it will do the job we need to do.
So let me demonstrate one more time.
So we have the platform with the start point and the end point x axis, and we have spawn position right
here.
Whenever player gets closer to the spawn point, we're going to take the platform and spawn it over
here.
And then we take the spawn position and move it here.
And when our player gets closer again to the spawn point, we're going to take another platform and
generate it right here.
And then we're going to move the second point somewhere there.
So this is the purpose of this video.
I hope that makes sense.
If it's not, just stay with me, you'll see how it works and very simple to understand.
So now you know how that's going to work.
Let's prepare the platform.
First, we're going to delete this background in a coin.
We don't need it.
And this we don't need it.
We need on the platform.
Let's create empty parent for this and name it as level part.
We're going to reset position to zero zero.
Make sure you do that.
Then we're going to create empty object under it and I'm going to name it as start point.
Let's give it the gizmos.
Some colorful like that is.
Okay, then let's duplicate this and make it end point.
Now, whenever you make a level part like this, we're going to use start point, We're going to place
it on the left and endpoint.
We're going to place it on the right, and that's going to be our left and right borders that we use
to know how long is the little part?
All right.
And we need to make sure both of them at the zero level, you'll know why very soon.
And basically you can make your platforms inside of this area.
You can resize them, you can duplicate place another one like that, and then you would need to jump
from here to here to here to here.
Always like that.
Let's change the pivot.
Let's keep one platform for now.
Just like that, it can be anywhere.
You just need to know that this is your, let's say, zero level of your level part.
And if you place some platforms below is going to be like below zero level, but it will be easier to
understand when we're going to actually create level parts entirely.
For now, let's just prepare the system, okay?
I want to make prefab out of this.
And if you don't know what is prefab, don't worry about it.
We're going to talk about this later in the course.
For now, let's just create a folder name.
It is a prefab and drag this level part inside.
Now we can delete it from hierarchy and we have this part, very same part over here, and we can use
it as a template to spawn same objects whenever we want to.
So let's drag it here to the level generator like that and go inside of the script.
We can change this to vector three.
Next part position.
I'm going to save it.
I have a habit of saving everything all the time.
It's a good habit, actually, I guess.
Now, let's change this position to the next part position.
So we're going to use this next power position to spawn our platform for the first time.
And then we're going to assign next third position equals to the end point of each level part.
And let's do that right now, just here below, I'm going to type next part position equals to new part
dot find and find is used, as you can see, finds a child by name and returns it.
So basically we can use it to find an object by its name.
Let's do that and point dot position.
So as I said, so as I said, we're going to spawn platforms at point every time and every time it's
going to be a new endpoint.
Let's go back to Unity and test this and click some mouse.
Click, click.
Okay, we're doing it.
It is there now.
As you can see, this is my first platform, and my second platform is bound at the endpoint.
But it was found right at the center, like this is the center part of the platform and the endpoint
and we need to spawn it like this.
To fix that, we're going to go back to script and we're going to type next part position.
Minus.
Part dot find.
Start point.
Position.
So we have an x axis.
Yeah, this is the X and we have start point.
And this one, let's say somewhere at minus five.
And next per position we have at zero.
So this is third point and this is the next part position.
So what we're going to do here is we're doing next third position minus five, and that will be equals
to minus five.
So in the end, this value will be equal to minus five, which is start position.
And basically this is what we're doing every time.
Let's go and see this in action.
I'm going to save it.
Boom.
Now I'm going to spawn it again.
Boom.
And we can see that now.
Endpoint and start point and point and start point of the new one are at the same position.
Very cool.
Right.
Now, what is important here not to mess up is the why of the platforms.
So at the moment our platform has start point y zero and endpoint Y zero.
This position is very important because if you place one of them higher or lower, you're going to have
this effect.
And I know this because I released this course before and now, as you might know, I don't know if
I told you before, this is a rework of the course.
All right.
So basically, we had eight months of beta testing with other students.
And I know that some of them had difficulties just like by the accident.
They move this endpoint and then there is this kind of a behavior which is not okay, but I want to
prevent it just in case.
So let's go back to code and for this position we're going to use only X, So no matter how high or
low, you're going to place your endpoint and start point, they're going to be spawned at the same
level all the time.
Let's do that.
We can just type here vector to new position equals to new vector to and for the x, we're going to
use this value over here and we're going to do this dot x.
And that ex.
And now after a comma, we need to put y and that's going to be zero.
And then we're going to go here and type new position.
So it's going to always have zero Y, so Y is always going to be equal to zero and there will be no
offset.
Even you accidentally move it.
Yeah, let's go check that.
So they are at the different level.
But if I spot them.
They're at the same level.
Cool.
I like it.
Now, how do we make it spawn when we need to?
First, we could spawn like one thousands of platforms at the beginning and just see how it works.
But I want to make something better.
I want to spawn the platforms when player get in close to the spawn position.
So let's.
Select this.
Click and enter.
Extract method and we're going to name it as generate.
Platform.
Yeah and I want to change condition here is going to be not if but while and in the parentheses let's
check the distance right.
What kind of distance do we need?
Look here we have a span position somewhere, right?
This one we have a strong position and player is getting close.
So we need to know how close should be player before we spawn next platform.
So basically we need this distance.
So let's make that first serialize field private float distance to spawn.
And while we are here, let's create serialize field, private float distance to delete.
And of course we need reference to the player.
So let's do this serialized field private transform player.
Okay.
Now, back in the parentheses, we're going to do vector two, the distance between player transform
position.
And.
Next part position.
And we're going to check if it is less than distance to spawn.
Okay, that's good enough.
Now, let's go back to Unity.
We're going to select a level generator and drag our player over here.
Then we need to make distance to spawn maybe 50 and distance to delete.
Let's put 50.
Now, the first part position is 000.
So I want to place my player above that.
Let me see somewhere.
Okay, So zero here.
Maybe a bit to the right and a bit to.
Okay.
He's not going to run fast enough, so I will give him a speed of 20.
And let's go.
Okay, so he spans the platforms.
But that's because we don't have function for that yet.
Okay, so the platforms are there.
Let's delete them now.
Let's go back.
And how are we going to do that?
First, we're going to create a function, of course, private void, delete, platform and position
before our platforms respond as a child of a level generator.
So we can use that.
We can check if this level generator has any child and if it has some, then we can delete it.
So that would be the first check.
So let's do this.
If transform child count bigger than zero, then if it is, we're going to grab the first child of the
object like an angry parent who first comes to his eyes.
We'll get some.
So let's do that.
Um.
Transform.
BART to delete equals to transform.
Get child zero, which is the first one.
And then we're going to check if player is far enough from this platform.
So let's check if Victor to the distance between player transform position.
And BART to delete.
Sorry Bart to delete transform position.
Then.
Oh, wait.
Yeah.
Less than distance to delete.
Then we're going to do this, Troy.
Bar to delete game object.
And let's do this check just before the generating delete delete platform.
Very cool rate, very simple short script, but so effective.
Let's go back.
And I want to up on my platform and just start point here and end point somewhere here.
And remember, we don't need to worry about why because we set it to zero.
Okay.
We have an issue with that because this sense is the same is getting deleted before it gets generated.
So let's make distance to delete 100.
I did some kind of a mistake there.
I will see what is wrong.
Oh, yeah.
I am so stupid.
Should be not less, but bigger, right?
If distance is bigger, then we delete.
Like if he is far enough.
I don't know why.
If bigger.
Make sure you put here bigger.
Sorry, I like the stools.
Let's go back.
Now it can be 50 again and doesn't matter.
So respond.
And let me change speed to 25 and let's go.
So now parts behind the deleted and parts in front of generated.
Very cool.
I like it.
And basically we have it and let's run her.
We can place main camera under the player.
And make it zero or zero.
And now if you play the game.
And let's run there.
Then we're going to improve it and we're going to do that in the next video.
For now, it's enough.
Good job today, man.