5 Minute ENDLESS RUNNER Game UNITY Tutorial
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?
Buy me a coffee ☕: ko-fi.com/bmoli
➤LIKE the video if you enjoyed, it really helps the channel!
➤Join our DISCORD SERVER: / discord
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.
Here is the 1.5 minute Photoshop tutorial I followed to make my space background: • How to Photoshop Space...
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:
2D SideScroller Movement Tutorial: • 2D Side Scroller MOVEM...
2D Top Down Movement Tutorial: • 2D Top Down Movement U...
6 Minute Pong Tutorial: • Making PONG in 6 Minut...
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
Пікірлер: 224
People do speedruns while playing games, this dude does a speedrun making games. A True LEGEND
@Billman07
2 жыл бұрын
Build different...
I love your tutorials so much I watch them when I'm not even trying to make a game
@BMoDev
3 жыл бұрын
You're the best!
@notbotpot7559
3 жыл бұрын
@@BMoDev no you are also thank you so much your discord is really helpful
@belalmansour1450
3 жыл бұрын
Yh I'm trying to make a completely different game but I thought oh well I might as well watch it
Didn’t think this video could get more helpful until I clicked on the description box! You are the real tutorial MVP!!
@BMoDev
3 жыл бұрын
:D
@IceCream-sb7lc
3 жыл бұрын
BMO! I never knew that you had such detailed descriptions....I too believe that you are the real MVP
Man these 5 minute videos are what’s up! Straight to the point and informative. Can’t wait to see more my friend.
@BMoDev
3 жыл бұрын
Thank you, appreciate you 🙏
@LaurisG_
2 жыл бұрын
God bless "Pause" button! :-D
This is a super concise and easy way of doing this. I've never actually tried this but I totally should. Great video!
@BMoDev
3 жыл бұрын
Appreciate it! Yeah give it a shot, once you get the foundation in place you get to solely focus on content - quick to prototype compared to some other genres.
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
the repeat background part is just what i needed!
Glad to be one of the first to catch it. I'm actually learning alot from these videos. Cool👍
@BMoDev
3 жыл бұрын
Thanks!
Super fun 5 min tutorial! Really sets the groundwork for a really fun game!
@syedmoin7939
3 жыл бұрын
kzread.info/dash/bejne/f2h5xtNmZKioodI.html
Bro This was BRILLIANT
Doctor: Sir, you have 5 minutes to live. BMo: Open up Unity, and get this project started.
this is amazing!!!
Great tutorial really helpful
Great video! It was really helpfull!
Awesome tutorial bro, applied the same concept to 3d objects and came up with a similar result.
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;
Great video man :]
Great Tutorial!
@BMoDev
3 жыл бұрын
Thanks!
Thanks for this, it helped me out a lot
@BMoDev
2 жыл бұрын
Awesome
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!
@BMoDev
3 жыл бұрын
Nice job! Feel free to share on the discord (only if you want to), I'd like to see how yours came out 👀
@dhruvdornal6381
3 жыл бұрын
Bruh did you made the same game or changed the character and obstacles ? And from where did you get the objects
@BennysRadio
3 жыл бұрын
Dhruv Dornal 8D Songs you can get free sprites at sites like opengameart.org or kenney.nl to let your prototypes look nicer. I made basically the same game and to get back to the engine after some breaks and changed just some minor things. Never shared it though.
@dhruvdornal6381
3 жыл бұрын
@@BennysRadio ok so we can add character and obstacles like subway surfer games for free ?? Right
@BennysRadio
3 жыл бұрын
Dhruv Dornal 8D Songs depends on the license the creator posted it to the sites. On opengameart you can see the licensing model on the left side. If somebody posted a subway surfer like set and give it away for free with for example the public domain license, than yes. But some licenses require things to do. Like mention them in the credits or linking to them. It’s all explained in the license mode they choosed.
this man is just built different
You sound like brackets jaja, love your tuts. Keep the great work bro 💪
@BMoDev
3 жыл бұрын
Really?? But I'm not Danish 😁 Thats certainly a compliment to me - appreciate it!
this worked thanks
Thank you :)
Hi, love the tutorial. I have a quick question, how would you go about adding more background images linking next to each other rather than just one stagnant image that loops? I want to have about 3 scenes of different backgrounds, it can be something like background 1, 2, 3 then loop.
Thanks for tutorial! I have question. After adding backgroundscroller script did you do something in inspector? Like adding value to collider and rb or it just none?
Love your vids it's simple yet very informative
@BMoDev
3 жыл бұрын
:)
Subbed!
hiya, thanks for the tutorial! although i think i did something wrong, the player just phases through the obstacle, is there a way to either show a game over screen or to make the player reset/disappear?
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
I'm working on a game called "I have ADD". I'm learning Unity 5 minutes at a time, max. Thanks!
@jason.arcuri
2 жыл бұрын
I’m learning the same way lmao; 5-10min Unity videos are now built into my daily school routine
thankss :D
Thats good
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
@egeozz
3 жыл бұрын
Yes Same
hey can I ask you what theme color you're using in the VSC IDE ? thanks
if your background does not fit into the camera, reduce the camera size to 4 and it should work 👍 thanks for the video!
when i do the repeating background it functions properly but when it resets like the border tears and you can see the background
@syedmoin7939
3 жыл бұрын
kzread.info/dash/bejne/f2h5xtNmZKioodI.html
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
I tried the background code but i got the error: Embedded statement cannot be a declaration or labeled statement. Does someone have any idea how i can fix this because i have no clue i just started programming.
Help pls, if i go to unity there is a "the name scrollspeed does not exist in the current context" at the bottom left. Someone explain pls. Thanks
@BMoDev
3 жыл бұрын
You have a typo most likely. Can you share your script?
@BMo what do you use to edit the script
Nice vid man i subbed But tbh thats a very sly and crapy substitute for an obstacle spawner
For anyone who has a problem with it not scrolling right and they are using a sprite, try setting width to the length of your object
How do I make it so the Obstacle also spawns on the middle?
I keep on getting error about background scroller I followed ur step by step but I keep on getting them
I got on the end an error of the word "other" how can I fix this?
one word for u GOD
What if wanted to move my player in the vertical axis. What do i need to change in the script to make it work??
@NobodyAtAll420
2 жыл бұрын
Either change the get axis to Horizontal or add another line of code the same as how you have it moving vertically but use the word Horizontal if there is an x change it to a y.
The Movement and the wall is working but the sprites go underneath the background
How the hell do you make VisualStudio show the references above the variables? Been searching my ass off but can't find anything of use on the subject. Also... why aren't my variables turning green like yours? They just stay white... I'm working in the latest version of VisualStudio...
@nchtblut
3 жыл бұрын
SAME F
@Azazel.x
3 жыл бұрын
You have to download extensions for that to work. Also He is just using another theme. The colours and such varies accordingly
Hey @BMo i think i have followed every step correctly but when I press play my obstacles always dissapears behind the background and I don’t know what to do???
@BMoDev
3 жыл бұрын
You probably have the obstacles on the same sorting layer. Check in the Sprite Renderer in the inspector
@purjolokgaming8482
3 жыл бұрын
@@BMoDev thank you, i looken around a bit and finally i got it. Now everything works fine👍👍
HI i follow step by step but when i played my background it is still in static form what should i do?
@beccabigcca
3 жыл бұрын
its ok now but the image scroll incontinuously
Thank you ! I learned much from this tutorial. The only problem is i can't manage when i run into obstacle my rocket just pass it.
@BMoDev
3 жыл бұрын
Setting "IsTrigger" to true lets you pass through objects instead of collide, however if you are setting up the OnTriggerEnter2D method in your PlayerController to Destroy your player GameObject, it will have the same effect. If you don't want the obstacles to kill your player, but rather just collide with them, you may have to change the approach a bit.
@cheecha3
3 жыл бұрын
@@BMoDev Thanks, i have managed it. Now i'm struggling to add shooting to the player so i can also destroy obstacles. Have you got any clue what script to use? Thanks!
Im use your game and share itch io ?
DAYUM!
What is your VSCode theme?
how did you code the scroller itself? like what app do you use?
@DoB7709
Жыл бұрын
From the logo on the top left of the coding screen i think he uses microsoft visual studio
semih jsuis first avec ce projet
Please help i am stuck it says: warning CS0108: 'backgroundscroller.collider' hides inherited member 'Component.collider'. Use the new keyword if hiding was intended. Please need help as soon as possible thanks.
@samlion7308
3 жыл бұрын
Change the name of your BoxCollider2D from "collider" to "bxcollider"
@JSCR1
3 жыл бұрын
@@samlion7308 What do I edit? Do I make BoxCollider2D into Boxbxcollider2D?
@samlion7308
3 жыл бұрын
@@JSCR1 they create a box collider 2d named collider, that's not a good name because "collider" is a reserved word
I need help. The BackgroundScroller script doesnt show up in the place where you select a component.
@NobodyAtAll420
2 жыл бұрын
Drag and drop it onto the object and it should add it to the object automatically.
@NobodyAtAll420
2 жыл бұрын
Also you have to hit ctrl+S to save it in your code editor then go back to unity and unity should reload then it should be available if you copied it exactly and remember to save before switching back to unity.
question as to why my obstacles are sticking to the right side am i doing something wrong
@pimonsolak3692
3 жыл бұрын
Try to check the code with ResetObstacle and check thu numbers you have written there.
More videossss
it so good i won my school best game
hey my player passes from enemy without getting out please help!!!!🤔
@ammar_arain
3 жыл бұрын
I have the same problem...that and my background glitches
My player dont die what can i do
Idk why you have 2k subs you should have 2m
@BMoDev
3 жыл бұрын
Appreciate that 🙏
Btw I wanted to ask from where can I get a character like a human for my game ?? Can someone help me out
@fukaze2021
3 жыл бұрын
there's a lot of freebies in itch.io
Can i get these scripts please ? Please tell where to find
Ok so I follow the script word by word but when I start it the 2nd image resets exactly where my first image is. How do I fix that?
@kingcreatez4827
2 жыл бұрын
that happens to me aswell, have you managed to fix it?
@N473
Жыл бұрын
@@kingcreatez4827 Ended up fixing mine a while ago by just copying another background over and shuffling them around.
how do i make the player game over when touching the obstacle ?
@ChupaChups2626
3 жыл бұрын
in the video, after the player hit the obstacle, the player is destroyed. but the game is not over. how to fix this ?
@pimonsolak3692
3 жыл бұрын
@@ChupaChups2626 im asking the same question. The only thing i managed to do is when the player hits the obsatcle the game stops and nothing happens next. But still dont know how to make the game repeat.
@Reddit-stories404
2 жыл бұрын
@Yuvaraja M I would recommend making a new scene and writing that when he collides it changes scene to a scene that says game over
Amoung is background?
I was looking for a tutorial for an autoscrolling game and found one, but the man was speaking really slow, So I took advantage of the ability to change playback speed and turned it to 1.5. I didn't like the length of the video, so I found this one. My first thought was "Wow, this man is really going at it." I'm an idiot.
Hello I'm trying to make a 2D endless runner game for a school project and I've did everything you did but my background scroller doesn't work and the player does not get destroyed when it touches an obstacle. I also need to make it for a phone so I'd also need a reset button, if anyone knows of any tutorials that can help me out please let me know.
@kingcreatez4827
2 жыл бұрын
same with me, my background resets its position on top of eachother, did you manage to fix yours? if so please tell me how
@ammar_arain
2 жыл бұрын
@@kingcreatez4827 Hope you find the solution though, best of luck!
How to Fix this 'are you missing a using directive'
This is awesome!!! But what if i want to increase the difficulty of the game as time goes on? How do i generate more obstacles the longer the player stays alive? Thanks! :)
@NobodyAtAll420
2 жыл бұрын
You need an object pooling script to randomly call different objects.. At least that's what I've figured out so far. Hopefully this info helps lead you to find more info.
Bro are you a game developer Could u say. Your qualification As i need that for my future
Wtf none of my code doesn’t work it’s exactly the same I write it, I go back to unity and press play and it doesn’t do anything please help me I beg
Im gonna make a video about it.
What the program code? 🥺
I Love your tutorials but how do u make it into a mobile game?
Hey Can you make serie to make Game look like HOTLINE MIAMI
Which Version???
why in my unity the hero doesn't destroy if hit the obstacle?
@13daffaafaalihbimap.d9
3 жыл бұрын
please help me
this is really a beginner friendly tutorial, thanks! its straight forward that I cant follow along, like wtf is wrong with my script its different than yours I have something going on like // start is called before the first frame update can u help me like wtf this is so hard to make and u only did it in 5 fukin minutes and I wanna die I followed your codes but it didnt worked help me, heelp, me
@fndusty4542
3 жыл бұрын
you can delete the stuff like //start is called... and such, they don't do anything due to the double forward slash infront of the text. I am assuming that is what this guy in the tutorial did. You might need to go and find another tutorial on youtube on how to make the character move, as this guy only shows what to put into the script and not how to make the input (up arrow on keyboard to go up, vice versa for down arrow, etc.) hopefully that helps you.
@09SkyFrost
3 жыл бұрын
@@fndusty4542 Thanks dude, But im kinda confuse in these visual studio codes like first of all the // then he has something at the top of his code called reference? idk its so different than mine, im stressed out yesterday its like 1:00AM when I wrote that comment and he's like doing a speedrun instead of a tutorial
@fndusty4542
3 жыл бұрын
@@09SkyFrost I can agree- this tutorial does feel like a speed run with some cuts. If your visual studio code looks different, don’t worry. The reference thing wasn’t at the top of mine and it had worked, just type in the codes he types in the brackets. Yours should still have “void start” and “void update” as a default, and just go from there with the video. When it gets to the player controller though, I recommend watching one of the videos he has linked in the description for that section. They should help with inputs and movement, as you are a beginner you may not have that set up.
@09SkyFrost
3 жыл бұрын
@@fndusty4542 Alright, Thanks!!! appreciate it 😁
@ammar_arain
3 жыл бұрын
@@fndusty4542 I can't find any tutorials and I really need to do it for a school project, do you know any good tutorials that tells you how to make a good endless runner for android 2D??
It doesn't work:(( please help me
scripct correct but cant move the player
How to move it on Andriod?
I keep getting CS0245 with 'Rigidbody2d' I have no idea what to do, 'New to coding' using System.Collections; using System.Collections.Generic; using UnityEngine; public class backgroundScroller : MonoBehaviour { public BoxCollider2D collider; public Rigidbody2D rb; private float width; private float scrollSpeed = -2f; // Start is called before the first frame update void Start() { collider = GetComponent(); rb = GetComponent(); width = collider.size.x; collider.enabled = false; rb.velocity = new Vector2(scrollSpeed, 0); } // Update is called once per frame void Update() { if (transform.position.x { Vector2 resetPosition = new Vector2(width * 2f, 0); transform.position = (Vector2)transform.position + resetPosition; } } }
@thesauce5517
3 жыл бұрын
if you haven't figured it out yet on "rb = GetComponent();" you need to capitalize "2D" Code should be rb = GetComponent();
Mines says other does not exist in current context can someone help?
@BMoDev
2 жыл бұрын
Sure can you share your script?
@Wolfnoir60
2 жыл бұрын
@@BMoDev never mind I’m just very blind and need to wear my glasses thank you
Great tutorial! But i would like to ask that how do i make th game repeat after dying?
You sound like brackeys
is anybody else posting comments in case he makes a 10000 player battle royal ?
@BMoDev
3 жыл бұрын
lmaooo I just might one day
0:37 how can i sellect 2
@BMoDev
3 жыл бұрын
Ctrl + Click
@januuuu2900
3 жыл бұрын
@@BMoDev k
my words don't have color what do i need to do
@Maximiliano7670
3 жыл бұрын
Its fine they dont need color mine dont as well but it still works
Assets\PlayerController.cs(25,7): error CS0103: The name 'other' does not exist in the current context how do i fix this void OnTriggerEnter2D(Collider2D col) { if(other.gameObject.CompareTag("Obstacle")) { Destroy(gameObject); }
does this work for mobile?
what means f, in "-2f"?
@jellyhotdog4955
2 жыл бұрын
might be a bit late to reply but it means float
i need the scroll script
sir is this a mobile game or a pc game???
can u pls make a how to move stickman with arrow keys and ad
@BMoDev
3 жыл бұрын
Yeah I've gotten a lot of asks to do so, not sure if it will be my next video but I'll try to get to it.
@monsterboyx7076
3 жыл бұрын
@@BMoDev thnks sooo much