Creating an Endless Runner in Unity: A Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Rado starting we're going to be creating a new TD project making sure to use uni editor version 2021.3 at least once edit is open we're going to create a new folder called resources within which we only import the asset pack that you can find Down Below in the description once imported I'm going to go into the environment folder selecting the tile set we're going to set the pixels per unit to 48 Sprite mode to multiple filter mode to points and our compression To None before clicking apply now heading into Sprite editor we're going to click slice leave it on automatic and just simply press slice upon which in the bottom left Sprite we're going to modify it so the width is 48 while the height is 38 before clicking apply now renaming our sample scene to demo from the scenes folder we're going to create a new game object called platform before dragging into the resources folder to create a prefab from here we're going to create two child game objects called top and bottom before heading into the tile set Sprite sheet within our environment folder and dragging toll set 20 free into our top game object now we're going to add a box collider 2D slowly editing it so it's just below the top of the Sprite this allows us to have the effect of the player walking on the platform instead of on top of it making sure the selector used by composite Boolean to true I'll show you why in a moment duplicate the tile four more times while incrementing the X position by one once done we're going to drag tile set 21 into the bottom setting its y-axis to negative 1.1 and it scale to two once again just like before incrementing its x-axis by one for every duplicate now heading back into the platform game object we're going to add a new component which will be composite collider 2D setting its body type to kinematic freezing its Z rotation and Y position lastly we're going to set its composite collider 2D geometry type to polygons this is a deal of any future issues we might have now we're going to select the untag tag just below the platform name in the inspector clicking add tag we're going to create a new tag pressing the plus icon called platform with a capital P before setting platforms tag to annually created platform tag now we're going to select the platform in the hierarchy and duplicate it three more times setting the first duplicates transform position x value to negative six the second to six and the third to 12. we're going to set the camera's size to 2.5 its background color to Black and just arrange it how you would like it foreign set up we can now start on creating the background so right clicking in the hierarchy we're going to head over to UI and just left click canvas within which we're going to set the UI scale mode to scale of screen size its x value to 1920 and Y value to 1080 before setting the render mode of the canvas to screens of space camera and setting to run the camera to our main camera within the scene now create a new child game object for the canvas naming IT background set its anchor using alt to stretch which is the bottom right one before giving the background game object its own child image object once you get hanging to UI and selecting image we're going to repeat the process of setting its anchor to stretch using alt before duplicating it two times naming the first Sky the second clouds and the third C we're going to set the source image within the image components of each image object to what they represent the first being Sky of course second being clouds and third C now we can just reposition it slightly I set the C's top position to 555 the Cloud's top position to 52 which gives us a pleasant looking background now we're going to create a new fallen assets called scripts within which we create a new c-sharp script called platform controller as we of course need a script that control our platforms movement within the platform controller script I'm going to remove the both system Imports as well as the start method replacing it for private float called speed with a default value of 5 making sure they give it the serialized build attribute as well as a private rigid body 2D with a variable name rb2d once again making sure to give it a serialized build attribute as we want the platform to always move left we need to make sure the update method is constantly setting the rigid body 2DS velocity to new Vector 2 passing in negative one times our speed variable and zero heading back into the editor and opening the platform prefab we're going to add the platform control script making sure to set the rigid body 2D reference to the platform testing it out everything seems to work fine except we run out of platform which is a bit of an issue so heading back to our scripts folder we're going to create a new script called platform pool where once again we remove both system Imports and add in a private game object which is our platform prefab making sure that add the serialized build attribute a private float called current X spawn position giving it a default of 12 before adding using Unity engine.pool this gives us access to object pulling which helps optimize our projects and lower the burden place on the CPU so adding public eye object pull with the type being game object giving the pool the name m underscore pull as well as a get and set also following the documentation we're going to add both a private bull called collection checks giving a Default true and a private ins called Max pool size for default of 15. removing the update method we're going to initialize M pull within the start method using MP equals new object pool giving it the type of game object adding the method create platform a private method that returns a game object by creating a game object called platform and setting to instantiate platform prefab the parent transform being this script's game object we then disable the platform using platform.setactive passing in false before returning platform our next method's on take from pool a method that's called when a platform's taken from the pool using gets something I'll show a bit later the method itself takes in a game object of platform where it sets the platform's transform position to equal new Vector 3 passing our current X spawn position with Y and Z being zeros we then enable the platform's game object by using platform.gameobject.setactive true next we have on return to Paul a rather simple method that takes in the platform game object and uses an arrow function to disable the platform game object using platform.gameobject dot set active false of course just like untake from pool which only used when a release is called finally our last method is on Destroy pool object which as you can imagine when called destroys the platform it's a pretty handy method considering it allows us more control over what the destroyed Behavior does but in this case it just destroys the platform finally we add in our collection checks we set the capacity of the port 10 before adding our Max pool size with that done save and head back to the editor before selecting all four platforms right clicking and selecting create empty parents we're going to name this new parents to platform holder giving it our platform pool scripts as a components and dragging our platform prefab into its reference now we're going to create a new game object in the hierarchy called platform releaser resetting its transform and setting its exposition to negative 12. as for these platforms it's The Sweet Spot give it a component of box collider 2D and edit its collider so its height is increased so just below the bottom of the platform we're finally seeing there is trigger bullying to true now heading back into the scripts folder we're going to create a new script called platform releaser dragging it onto the platform release a game object right clicking it and selecting edit script once again removing both system Imports we're going to add a new private variable platform pool called platform pool giving it a serialized build attribute moving both the start and update methods and adding a private void on trigger answer 2D which takes in a collider 2D Collision which wins parameters and giving it an if statement with its condition being Collision dot compare tag platform but there's not much we can do without anything to call so heading back into our platform pool script we're going to create two new public void methods with the first being spawn platform which uses an arrow function are pull variables.get method and second being a release platform which takes in a game object of platform and once again using an arrow function before using our pull variables.release method passing in the platform if you want to know more about the get and release methods feel free to browse the documentation link down below going back to the platform releases script we add our two new methods before heading back to the editor set the platform pool reference to platform holder within our platform release again object and test it out and it works perfectly for slight hiccup that's okay we can fix that just going to set the first platform to zero the second platform to negative six the third platform to six and the last one to twelve testing again and we have no gaps excellent with our platforms working we're going to head back into our resources folder into our asset pack this time going into the player Sprites selecting both idle and run using shift we're going to set the pixels per unit to 48. the Sprite mode to multiple filter motor points and compression To None before clicking apply now just selecting idle we're going to click Sprite editor slice we're going to set the grid by cell size to 48 by 48 click slice and apply doing the same to the other now from the assets folder we're going to create a new folder called animations before when the hierarchy we're going to create a new Square game object we're going to 2D object Sprites and clicking Square rename the game logic to player and position the player where would you like it to be once position we're going to set the Sprite image to idle zero before giving our player game object a new sorting layer by clicking defaults add sorting layer clicking the plus icon and they mean the new layer player making sure to set the Sorting layer on the player game object to player a little bit of repositioning and making sure play is selected we're going to click window animation animation before clicking create we're going to name our new file idle making sure it's within the animations folder and clicking save now left click idle 0 within the player Sprites folder and shift-clicked idle 3. this allows you to drag every single Sprite into the animation window doing the same for run we're going to left-click the idle drop down within the animation window select create new clip and do the same thing but for the Run Sprite sheet with that done we're going to head into the animator once again making sure the select player we're going to right click idle select make transition and click run heading into our parameters we're going to create a new ball called is running setting the transition condition by pressing the plus icon which should give you the condition set the true just make sure the disable has exit time as we don't need it make sure is running is set to true and we can test out the game it's that simple let me know if you have any thoughts on the tutorial if I should continue the series like the video If you learned something and subscribe it really helps the channel out see you in the next one
Info
Channel: The Sleepy Koala
Views: 10,538
Rating: undefined out of 5
Keywords: unity, gamedev, madewithunity, tutorial, unity3d, unity tutorial, unity game development, endless, 2d, runner, how to make a 2d endless runner in unity, unity 2d endless runner tutorial, 2d endless runner unity, 2d endless runner unity tutorial, how to make an endless runner in unity, unity 2d endless runner, endless runner unity, endless runner unity 2d, unity endless runner tutorial, endless runner, 2d endless runner, how to make an endless runner, endless runner tutorial
Id: CcTj8es83zA
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.