Creating Infinite Level
71K views
Oct 30, 2023
Creating Infinite Level
View Video Transcript
0:00
Hello guys, this is Vivek Sharma and welcome to the seventh episode of this Game Doggment
0:04
series on Csharponan.com. In our previous video, we saw that our dinosaur fall off the ground whenever it ended
0:11
So in this tutorial, we will be creating an infinite ground for our dinosaur using infinite background
0:17
looping. So without wasting any further time, let's get started. So to achieve an infinite ground, we are going to create three small ground
0:30
and we are going to change position of the last ground every time it hits a looper game object attached to the main camera which is always constantly moving so to do that first of all I will click on main ground I will change its transform values to make it a little uniform we'll create two more copies of it now I have three grounds now we have got a little mess in the hierarchy panel so to clean that up I'm going to create a new game object call it grounds gonna reset its transform and I'm going to make it the
0:59
parent of our three grounds. Similarly, I will create a new game object, we'll reset its values
1:05
and will make it parent of our enemies Now our hierarchy panel has started to look much cleaner
1:25
Now I will be creating a looper which is going to shift the position of our three grounds
1:29
To do that, let's go to Sprite's folder, drag it to the hierarchy panel, rename it and
1:35
make its size big enough so that it never misses the. the ground. Now let's add a box collider 2D and a rigid body 2D to our looper by clicking
1:42
the add component button. Let's change the body type to kinematic inside the rigid body 2D and check
1:47
the is triggered field in the box collider 2D. Next let's make the looper the child object of our
1:53
main camera so that it always moves with our camera. Now let's go to scripts folder, create a new
1:59
script and rename it as looper script. Do we click on it to open it up? Now first of all I'm
2:05
going to remove start and update method and create a on trigger enter 2d method now over
2:11
here I'm going to check a collision dot tag equal to ground or collision dot tag equal to enemy
2:16
now we will check if the object triggered to our looper is ground enemy or something else if it ground or enemy then we are going to shift its position to get an infinite level To do that first of all I will create a public variable of type float and call it distance changer
2:33
Now I'm going to calculate the length of the object which has got triggered
2:37
To do that, let's write float, length of ground, box collider 2D, collision
2:43
dot size, dot x. This is going to give the size of the box collider in the X direction of the box collider in the X direction
2:49
of the game object which has got triggered. Next, we want to get the current position of the object triggered
2:55
To get that, create a variable vector 3, call it pose, equal to collision. .transform
3:01
This will give the current position of the object which has got triggered
3:06
Now let's alter the x value of the position to make a shift. To do that, let's write pose
3:12
.x plus equal to length of ground into distance changer. This will increase the position in the X direct
3:18
in the X direction by the length into the distance changer we have just created
3:23
Now let's assign this new value of position to the game object which got triggered
3:27
To do that, write collision. .transform. Position equal to pose And that it We have completed our looper script Now let get back to Unity click on Looper click on Add component and search for the Looper script add the Looper script and over here we again have an exposed field for distance changer
3:45
Let's give it a value 30 because we have three grounds and each ground's length is 10
3:50
So 10 into 3 is 30. That's the value which is going to get here and let's hit play
3:56
And now as you can see, the ground game objects are changing their positions whenever they get triggered
4:00
they get triggered by the loop object. But currently the enemy game objects are not getting looped because we haven't added the tag enemy
4:08
to them. So to do that, click on tag, add tag, click on the plus sign, create a new tag, call it enemy
4:17
Again, select all of the enemy game objects, go to tag and select the enemy tag from this list
4:24
Now again, if we hits play, as we can see, our grounds and enemies are getting looped
4:30
And now we have a completely infinite ground and a completely infinite level
4:35
So that's it for this video. In the next video we will learn about Unity's Ui components
4:40
So till then, goodbye, have fun and don't forget to follow and subscribe because the best
4:44
is here to come
#Action & Platform Games
#Video Game Development