‫If there's one thing that is the one most important thing for an infinite runner, then that it's infinite.
‫And in order to create an infinite world, we need an procedural creation.
‫We need to create it procedurally, which means we need to do it via code, via code.
‫So we don't want to create a huge world of, I don't know, 1 billion different pieces of our road elements
‫or road parts.
‫We want to do that programmatically, and we can do that with a procedural way.
‫So what I want to do now is, first of all, I want to set up my crystal and I want to change the crystal,
‫not to be somewhere in the world or anywhere that I positioned it.
‫I want you to be on a road path.
‫So let's position our crystal, this one here.
‫Let's position it onto our road paths.
‫So let's get, by the way, rid of all the other crystals.
‫Delete.
‫Also, let's get rid of our road parts delete as well.
‫And now let's drag that crystal onto our game.
‫So let's say roughly here is a good position.
‫Let's see if it's just on top.
‫Just on top.
‫Yeah, it's fine.
‫So that's good.
‫And I want to put the crystal onto the road part, and now I'm going to deactivate the crystal.
‫So now, as you can see, it's not available anymore.
‫We would have to activate it.
‫And now that road part will be a new prefab.
‫So let's drag that road part into our prefabs folder and we want to create multiple of those road parts
‫procedurally later on.
‫All right.
‫Before we do that, there is a basic piece of our game that will always be the same.
‫So that's going to be our road in a certain way.
‫So let's get rid of the crystals, by the way, as well.
‫And now we can duplicate the crystal and let's move it accordingly.
‫So the first several steps are always the same, and then we get moving to the side.
‫So this piece will always be the same.
‫So let's have a look at the game view.
‫Yeah, it's always the same.
‫They're always moving like that.
‫And the next one.
‫All right.
‫So now that's the start of our map.
‫And now we want to go ahead and make it procedural so that the pieces, they are created automatically
‫and infinitely.
‫And in order to do that, let's create a new script for our road.
‫So let's go to our road, add component and new script, and let's call that one road.
‫Now let's open it one up and here we will need our repair prefab so public game object road prefab let's
‫assign that real quick in unity so our road script will need a road prefab let's drag the script, by
‫the way into our script folder.
‫Now let's go to our.
‫Wrote and now needs the road script or road part game object.
‫All right, there we are.
‫Or the prefab?
‫Pretty much.
‫All right.
‫Next, we will need to know from which point onwards do we want to have our world created?
‫So I have an error here.
‫The role part 11 is not required, so let's delete that.
‫So.
‫So from road ten on, I want to go on and create new road parts.
‫So from this point on.
‫So I need the position from when I want to create new road parts.
‫The position is the position or the transform position of our road part.
‫And if we have a look at it, it's at the position 007.09
‫So let's check that position.
‫I'm going to drag it back in our road script.
‫I want to have that position.
‫So I'm going to create a new public vector three last post, so I'm going to call it and I'm going to
‫add it in here.
‫So it was 007.9 or oh nine.
‫That was the position of our last road part.
‫Now what we need to know is by how much do we need to move an object.
‫So this road part, let's position it outside and now let's move that or duplicate that road power and
‫move it with control and shift.
‫And then we can see it's moving by 0.707.
‫So that's pretty much the width of or the change of the position of our block, and it should be the
‫width as well.
‫All right.
‫So let's go back to our road script and here we will need to know that offset.
‫So let's create a new public float offset and it's 0.707 as a float.
‫And now let's create a method.
‫I'm going to get rid of the start method.
‫I'm going to create a new method, public void, create new road part.
‫And I'm going to for now just debug debug log, create new road part.
‫And within the update method, I'm going to check if the user presses a button.
‫So input dot get key down and which key code do I want.
‫Key code dot space.
‫So whenever the player press the space, I want to create a new road part.
‫Let's go with that now.
‫So let's create that method even further.
‫So create new road part.
‫What do we need here?
‫We need to spawn position, which is a vector three.
‫So let's call it spawn post vector 3.0, which is the position 000.
‫So that's the spawn position from where we start, then we want to have a chance, which is a random
‫range variable between zero and 100.
‫And if chance is lower than 50, then I want to spawn to the left and otherwise I want to spawn to the
‫right.
‫So if chance is lower than 50, I want to have a positive offset and otherwise I want to have a negative
‫offset.
‫So spawn position is going to be new vector three, which is the last position dot x plus the offset
‫and the last Y position.
‫So last post at Y.
‫And then finally last post, but z plus the offset.
‫So that means it will move our or it will create a spawn position which is a vector three or it will
‫assign the spawn position, which is a vector three, and we will use that later on to spawn our game
‫object, which is our road piece or road part.
‫So else I want it to be the spawn position equal to new vector three last post x minus the offset.
‫Last Post Y and last post z plus the.
‫Now let's go ahead and create the game object.
‫I'm going to call it G, instantiate it so we create it within our unity world road prefab and at which
‫position while at the spawn position and which criterion do we want to have.
‫We use criterion dot ular, which means we rotate it with 0.45 and zero because our game objects are
‫rotated as well and we want our roads are rotated and we want them to be rotated here as well.
‫And now we need the last position or need to set the last position, which is g to transform the position.
‫Because that's the new position, right?
‫We need to know what the last position is of our new game object.
‫So we started off with the last position that we set up manually and now we always set it up with the
‫one that is created.
‫So the new road part has a specific position and we save that in our last post variable.
‫All right, so let's try that.
‫I went back to Unity and I created the split screen.
‫Makes sense here because what we want to see is what's happening in the scene.
‫So let me get rid of the sound real quick, even though it's great.
‫Now let's go into the game view and now let's press the space key.
‫And as you can see here in the scene view, our world is created now based on our tabs, but of course
‫we want that to happen, not based on us clicking all the time.
‫We want it to happen based on the position that we have.
‫Now let's go back to scripting.
‫So let's create this automatically.
‫Let's use the private void wake method here and let's just invoke repeating or let's call the method,
‫revoke repeating.
‫And what do we want to invoke repeat?
‫Well the create new road part method and we want to do that after 1/2 and we want to do that every second.
‫So let's just try that.
‫Let's save the game and see what happens now.
‫So let's try that.
‫And we can see every second a new road part is created.
‫All right.
‫But that's not exactly what we want.
‫We don't want it to be created whenever we are on awake, because now what it could what could happen
‫is we don't start the game and it's just running and we still create a huge world every single second.
‫But what we want to do is only if the game is started, we want to create those.
‫So how do we do that?
‫Well, we don't use the awake method here, but we use a different one, which we will create a different
‫one which we call start start building.
‫And it's not going to be private but public because we want to access it from our game manager.
‫And of course we won't need the update method now anymore with the key down where we create new road
‫parts based on our key press.
‫So let's go into the game manager and go to the start game method, because in here we start the game
‫and that's a perfect point to create those objects.
‫So let's find an object of type and let's find the road object.
‫And there we had a method called Start Building, and that's the one that we want to call.
‫All right, let's go ahead and test that.
‫So let's go to our game and I get a null reference exception.
‫And that has to do with me not having a game manager anymore.
‫So let's have a look how we can change that.
‫Let's create a new game manager.
‫If that's the case for you as well, I'm going to create a game manager here and I'm going to drag or
‫I'm going to reset that first here and drag my game manager in here because for whatever reason, it
‫disappeared.
‫So I'm going to save the scene now.
‫And before we test that, of course, we need to assign the score text and the high score text.
‫So let's do that.
‫And now let's try it again.
‫Now, we shouldn't get the error.
‫All right, so now we're back on track.
‫I somehow have must have deleted it.
‫So now let's try it out.
‫Oh, let's try it.
‫Let's start the game.
‫And as you can see, the game is procedurally created there at the back and it's a little bit too slow.
‫So we are faster than our game creates those pieces.
‫So let's create new road parts a little quicker.
‫By the way, by for example, we could create them every 0.5 seconds, so let's do that 0.5 F Now that
‫should solve that problem.
‫I think the interval is fine.
‫Let's have a look.
‫Let's start the game and let's see if it creates enough.
‫So what I don't like, however, is that I can still see how it produces those pieces.
‫Well, now I fell down, so that means how I can solve it is pretty simple.
‫I could simply.
‫Let's get rid of this road path here and that one.
‫Well, I need that one.
‫Let's bring it back to our road.
‫I could simply duplicate that piece here and move it further.
‫Actually, let's move it here.
‫Duplicate it again.
‫So I could simply make the world a little bigger.
‫So now let's save that.
‫And now I shouldn't see where it's created anymore.
‫So let's start a game.
‫And now, of course, we have the problem here that we don't have the correct end point anymore because
‫where was it?
‫Our road needs to have the last position.
‫So the offset of the last position and it should be this one here now.
‫So our road part 14 now is the last one.
‫So let's change the values here and take the Z value as well from our road and enter it here.
‫Okay, so now let's try it again.
‫Let's see where the pieces are created.
‫And as you can see, they are created at the right position.
‫Perfect.
‫And now I don't see where my world is created.
‫And I think 0.5 seconds should be fine because now it's creating faster than I can walk.
‫Okay.
‫That's good.
‫Now let's add the crystals again, because we don't have crystals.
‫Now, what?
‫We do want to have our crystals.
‫So let's go to our road script.
‫And in here in the create new part, we want to have a crystal every fifth step or every fifth road.
‫So let's create a private int road count, which is zero, and let's increase the road count for every
‫road that we create.
‫So road count plus plus.
‫And then we check if road count is divided by five, so modulo five is equal zero, then we want to
‫activate our crystal.
‫So g transform dot get child at the zero position so add ourselves, get the game object and set it
‫to active.
‫True.
‫So that will do is every fifth road will have an active crystal now.
‫All right.
‫And now let's save the script.
‫Let's go back to Unity and see if that works.
‫So the start pieces will not have any crystals.
‫So the ones that we have created manually will not have any crystals.
‫So the player has to get through a certain part here before he gets any crystals.
‫Oh, now we do have a little problem here, though.
‫As you can see, the animation is not working correctly anymore, and maybe you can guess why that is.
‫Well, that has to do with the fact that at one point our player was in the air or has touched the air
‫for just a split second.
‫And that's when he started to not move correctly anymore or to well, not animate correctly anymore.
‫So let's fix that.
‫We can fix that in two ways.
‫One could be that at one point there is some space in between so that the blocks are not perfectly in
‫line with each other.
‫So that could be one of the reasons another one could be, or another way to solve it could be to go
‫to our character animation and add a new transition, make transition from falling to run, and that
‫one will be based of a condition which we haven't created.
‫So let's create a new parameter and let's call that trigger not falling anymore.
‫That would not be the perfect solution though.
‫So the better solution would really be to check if there are any gaps in between my pieces here.
‫But that would be one option to do that.
‫Not falling anymore.
‫So let's do that.
‫Let's go back to our script here and we can go here else in our chart controller in the update method,
‫we could say if the red cast is not at this position anymore, we could go ahead and set the trigger
‫to is not falling anymore.
‫I think that's how I call it.
‫So let's check it out.
‫Let's change that to not falling anymore.
‫That was the correct term.
‫So let's go back to unity and change that to not falling anymore.
‫And now let's see if that problem is solved.
‫So let's play the game here.
‫We can go to our scene and maybe find a better view on things like that.
‫And now let's go ahead and start the game.
‫So now let's collect some.
‫And as you could see, he was falling there.
‫There was a little gap and it was a gap from our start.
‫Well, the point where I created the field manually and where we started to do it procedurally.
‫So there must have been a little gap in between.
‫So one or the solution that I've just shown you is just a patch, a little patch.
‫You shouldn't really use those ones when going live.
‫So you should find the little gap that we have there.
‫But I think it's too boring to show you that in detail.
‫So now let's have a look at the next chapter.
‫Oh, I missed that one.
‫So as you can see, our game does what it should.
‫We have a finished game?
‫I'd say we have the sound.
‫We can collect those points.
‫We well we have a high score and so forth.
‫And I think the most important thing is our procedural world.
‫Now, the challenge could be a bit higher.
‫Of course, you could change the speed of the player step by step.
‫Then, of course, you would also have to change the tempo at which we create our procedural map.
‫And there we are.
‫Great.
‫So I think it's enough for you to have seen me connect 26 points and the next chapter we are going to
‫create the next game.
‫So I hope you're hyped and I hope you took this game and made it even better.
‫So you make it even better.
‫So please go ahead and add features that you think would make this game look better or what make this
‫game just amazing.
‫So please go ahead and do that and share it with us.
‫All right.
‫So see you in the next chapter.