5 Minute ENDLESS RUNNER Game UNITY Tutorial

preview_player
Показать описание
In this Unity Tutorial, learn how to make an Endless Runner or Infinite Runner game in 5 minutes! Or... at least the foundation for one.

There are many different approaches when it comes to the Endless Runner genre. Sometimes the player is stuck on a track and can only move in one axis, sometimes the player can freely move wherever they want while the camera scrolls. It depends on how you want to design it!

For this example, the camera will be stationary, the backgrounds will scroll across the camera and reset, and the player will only be able to move vertically within the confines of the camera to avoid incoming obstacles.

✨Want to support the channel?

➤LIKE the video if you enjoyed, it really helps the channel!

We have channels to help you with your problems!

0:00 Repeating Background

The first thing we need to do is setup our repeatable scrolling background. I used an outer space theme as it's just an easy way to accomplish this, but there are plenty of resources out there on how to make your own "seamless pattern" images.

My image was 2000x1000 pixels, which in Unity world space units is about 20 x, 10 y in our default Main Camera. This means that with my first image set to the origin position (0,0,0), a second image could be placed at (20,0,0) to be perfectly lined up.

Once you have a seamless pattern created and you've tested it by aligning 2 of them, we then want to add a BoxCollider2D, a Rigidbody2D, as well as a script to handle the scrolling & resetting position logic.

The BoxCollider2D will be used to store the width of the image, as adding a Collider2D defaults to auto-sizing the image (which is desireable in this case).
The Rigidbody2D will be used to move the background images across the screen.
The BackgroundScroller script will make all of that happen.

2:50 Player Controls

For my example, I'm restricting the player to only move up and down vertically. I used a 64x64 pixel square as the Player, set its sprite color to Cyan, added a Rigidbody2D component, as well as a new C# script PlayerController.

In PlayerController we get the Vertical Input axis value in our update, and setting our Rigidbody2D velocity we multiply the input axis by our movespeed.

Here are some other tutorials where I've covered player movement in more detail:

3:45 Bounds Detection

After setting up our player movement, the player is able to move outside of the bounds of the Main Camera. This is obviously not desirable (or is it?... you be the judge!), so we need to confine our player to stay within the camera's view.

We could have the camera follow the player, but that breaks my design. Instead, I just added 2 new empty GameObjects, gave them BoxCollider2D objects, and using the tool in the Inspector, dragged the bounding box to be the length of the camera on the top and bottom edges.

4:02 Spawn Obstacles

Before we talk about spawning or instantiating any obstacles, we need to first make an obstacle to use! Reusing the same 64x64 pixel square as the Player, I scaled it up 5 in the x, and 5 in the y. I also colored it Red, and added a BoxCollider2D component with Is Trigger checked to True.

It's important to add a tag to these Obstacles, so I created a new Tag called... well, "Obstacles" and assigned it. I then made sure there was an Obstacle GameObject nested under each Background GameObject.

To spawn these Obstacles, at the start of the game as well as every time a background image resets its position, I fetch the Obstacle GameObject by using the background's transform's GetChild method. I then simply set the Obstacle localPosition (important when working with nested GameObjects) to a new Y position between -3 and 3. I determined these values by dragging the obstacle in the editor to the top and bottom edges of the camera and rounding to the cleanest number.

I wanted to give a short demonstration of how it could be done, but I think here is where you will need to expand things in your own project to really make it... interesting.

4:44 Obstacles destroy Player

The last thing we need to do is modify our PlayerController script to make it so if a Player collides with one of these Obstacles, then the game ends. Using the tag setup in the previous step, within our OnTriggerEnter2D event, we can check the other gameObject's tag if it's "Obstacle" and if it is, Destroy the player.

4:54 You did it!

Way to go Lil Noober! If you struggled to get to here and have questions, leave a comment or join our discord and post in our problem-solving channel!

#bmo #unity #tutorial
Рекомендации по теме
Комментарии
Автор

People do speedruns while playing games, this dude does a speedrun making games. A True LEGEND

terabyte
Автор

I love your tutorials so much I watch them when I'm not even trying to make a game

notbotpot
Автор

Didn’t think this video could get more helpful until I clicked on the description box! You are the real tutorial MVP!!

amandacollins
Автор

Man these 5 minute videos are what’s up! Straight to the point and informative. Can’t wait to see more my friend.

deadbroadcastpc
Автор

I got it to scroll vertically by changing this stuff.
public new BoxCollider2D collider;
private float height;
private float scrollSpeed = 0f;
height = collider.size.y;
(scrollSpeed, -2);
If (transform.position.y < -height) Vector2 resetPosition = new vector2(height * 0f, 40;

NobodyAtAll
Автор

This is a super concise and easy way of doing this. I've never actually tried this but I totally should. Great video!

neenaw
Автор

Glad to be one of the first to catch it.
I'm actually learning alot from these videos.
Cool👍

eileeng
Автор

I really love this tutorial, cause it covers the basics in a short amount of time, is also easy and good explained, espesially when u just take a look into the description :D thank u

Wotan
Автор

the repeat background part is just what i needed!

Daniel_He
Автор

I just did it! It took me 30 minutes though, I was a bit picky with my background a player character ^^ Great job, its great to let people do something in the engine that works!

BennysRadio
Автор

Doctor: Sir, you have 5 minutes to live.

BMo: Open up Unity, and get this project started.

JSCR
Автор

Super fun 5 min tutorial! Really sets the groundwork for a really fun game!

Irishpierre
Автор

I'm working on a game called "I have ADD". I'm learning Unity 5 minutes at a time, max. Thanks!

billkasperdotcom
Автор

if your background does not fit into the camera, reduce the camera size to 4 and it should work 👍
thanks for the video!

definarte
Автор

Awesome tutorial bro, applied the same concept to 3d objects and came up with a similar result.

slmusic
Автор

Thanks for this, it helped me out a lot

carsonpaige
Автор

I don't know if someone has the same problem, I followed the tutorial and everything works. Then I added a new method to spawn obstacles in different pattern. The problem is that everything start to wobble / shake, even thought I setted a constant speed like in the video

megaminx
Автор

the script of backgrounds didnt work, i follow same steps and make everything the same way, an (expected) error dont let me play the demo of the game some help pls

mikekonstadinidis
Автор

Love your vids it's simple yet very informative

RandomGuy-dfus
Автор

my obstacle resets before it goes out of the screen, im using 3 backgrounds and i did width*3 too but still obstacle gets resets early
please help

arpityadav