Make an Endless Runner in Unity 2020 / 2021 - part 1 of 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome to another video i'm infensia and in this video i'm going to show you how to do a little endless runner in unity [Music] i think an endless runner is a really good starting point it's actually a lot simpler than a platformer because uh with the automatic running you don't really have to run into problems like climbing or figuring out some intricate wall jumps and things like that and it's actually quite fun to play as well to my kids the youngest ones they had a pretty good time when playing this prototype [Laughter] if you're new to unity brand new that is then you should check out my learn unity tutorial first because this video has a little bit of a higher pace i'd say and it probably requires you to know your whereabouts a little bit in unity so i recommend to check that video out first before you get started with this one i created a new project in unity 20 20.2 and with a universal render pipeline we can use shader graph and we don't need the complexity of the high definition render pipeline the example assets are no longer needed so let's get rid of those this is a side scroller so the camera can be reset to position and rotation 0 0 0 and then just move it back a little bit i created a new empty game object and i called it environment this is going to act as a parent folder for level objects to keep it tidy and then i create a primitive cube and that will act as a tile block the light is rotated so it lights up the side that we can actually see i decided to scale the blocks to 2 by 2 by 2 to suit a normal character size you can use ctrl d to duplicate the tile objects you can also change the grid snapping and press ctrl while you move objects to snap them into place this makes it a lot easier when you build a level i created a new game object that i named hero and this is going to become our player as a child object a simple capsule will act as the player for a brief moment until we get the animated character in make the pivot point at the feet of the character or in this case at the base of the capsule i decided to go for the unity character controller for this game in my tests it offers a little bit smoother movement compared to a rigid body gravity will have to add manually but we don't really need any other physics forces to be affecting the player by creating a layer named ground and assigning it to all child objects under the environment it'll be a lot easier to detect when the hero is touching the ground through a layer mask i'm creating a dedicated hero folder under assets for all the hero related assets so i can easily export the hero if needed then i create a new c-sharp script that i named hero character controller this script will be controlling the player hero character i drag the new script onto the hero game object and then i double click on it to open it in visual studio since we're using the character controller we have to apply gravity manually we also need to do a reference to the character controller so let's do that in the start method now let's add gravity to the velocity variable on every frame and move the character controller by the velocity remember time.deltatime to make it frame rate independent time to test it out and we'll erase the hero parent object and press play gravity is added even when touching the ground so we'll have to fix that let's expose a new layer mask and we can designate which layers should act as the ground and then we assign the ground layer to the layer mask physics.checksphere is a good way to check if a fictional sphere is touching any collider i'm using that to check if the feet are touching the ground layers layer mask remember the transform position is at the bottom of the character let's only apply gravity if the hero is not grounded if the character is moved off the platform it looks like gravity is now working time to add movement horizontal movement will always be one since it's an automatic endless runner we'll also make the character face in the direction that it's running and we'll move the character controller horizontally okay now that's the slowest running capsule i've ever seen i added a multiplier just to increase the speed better but we've got no jumping yet so let's fix that one the hero should jump if he's grounded and if the jump button is pressed we can calculate the necessary velocity of a given jump height you can press ctrl period to create the variable and will expose it in the inspector too in edit project settings input manager you can see that the jump button is defined already and it's mapped to spacebar let's test it out in play mode okay in full screen it's an amazing game already maybe not let's continue i created a new script that i called camera follow this script will make the camera follow a designated target and in our case that's going to be the hero at a given distance the offset is calculated at the start and the camera movement should also be done in late update the lerp will make the movement smooth i assign the hero to be the target of the camera to follow the start position of the camera is relative to the player and that will be the offset at which the camera is following the object okay this video will be too long if i include the modeling so i'll fast forward the modeling and animation process out my channel because i have many videos about how to do low polar character modeling rigging and keyframe animation i also made a two hour in-depth low poly modelling tutorial that covers pretty much all the important stuff including how i colorize objects and all the keyboard shortcuts and tips and tricks how to model these type of things for this character i made a simple armature with inverse kinematics for the legs to make the run cycle more platformer like i made it fast and a little bit over animated i don't know if that's a word but usually i search for a run cycle image search on google when i animate and then i try to mimic those keyframe poses if you want me to release this modeling and animation process in a separate video don't hesitate to give me a shout in the comments there we go the head could do a little bit better movement but overall it feels a bit like a platformer movement snappy and rapid after modelling and animating the character i modeled some basic tile style dirt rock blocks and a wood crate as well a bit overboard with vertices possibly but get away with it i use a gradient texture and then i project the uvs from the side view to get the objects to fade towards the darker color at the bottom i also model a coin a dragon and a turtle enemy but i don't plan to put them in this game so i've got those spared to uh use for something else the cloud and tree however that made it into this prototype later in the video i exported the environment and the hero character from blender and i export them as fbx files and then i dragged to import them into unity starting with the environment objects i also imported the gradient texture that i used in blender to colorize the object you can find a link in the description to that texture if you want to download it usually i create a single new material that i try to apply to as many objects as possible unity can automatically batch objects that share the same material for better performance under lighting environment i disabled the fog i thought it was just washing out the level quite a bit the old cubes that acted as placeholders could now be removed so i removed them the environment objects were exported in the same fbx file so i just did some housekeeping and then i started to implement them the grass was modeled as a separate object so that could be rotated independently from the block for some variation it's best to create a prefab of the environment objects as soon as possible that way you can change the prefab to update all the instances of that block in one go i now add a box collider to the block and i manually forced it to be two by two by two in size notice how the pivot point is at the top of the block it makes a little bit easier to position and remember to make a prefab out of it i bet you forget that because i do that all the time i used ctrl d again to duplicate the tile block prefab to start building a level you can rotate some of the blocks by 90 degrees just to vary the look and feel of the blocks a little bit when i had a few dirt blocks i added the rock block that i had disabled previously i had to add the box collider again like before and it's also two by two by two meters in size okay we've got somewhat of a prettier level to look at now and time to test it out the gravity looks way too strong so uh that's because we need to set the layer to ground again for all the children objects of the environment so we can do that check properly that looks a lot better next it's time to import the hero character and replace the capsule i'm putting all the hero assets in the same folder to make it easier to export to another game or prototype if needed i'm dragging the hero fbx object into the scene and then i unpack the prefab and move it into the hero game object the capsule can be deleted now it lived a short but purposeful life okay the character animation is not set up yet so he's outperforming michael jackson's moonwalk although he's just sliding backwards so first thing to fix is the orientation so i select the mesh object in the project window and under the model tab i check the bake access conversion this is a new feature that will fix the export rotation differences it's not flawless it's got some contortions problems so it's time to add the animator control and the correct post will be fixed after we apply some animations i add the animator component to the hero object i created a new animator controller in the projects folder called hero animator controller the animator controller is assigned to the hero's animator components double click on the animator controller to open the animator window most of the animations are designed to loop so under the animator tab of the hero mesh object i set which animations i animated to loop seamlessly you can set multiple animations to loop before you hit the apply button because apply process takes a few moments so saves you from waiting if you apply it every time i expand the hero mesh object and i drag the idle animation into the animator window that creates the default state that the character will be in when play is pressed the character orientation is correct and the broken post is fixed by the idle animation to fix the contortion problem i disable the hero character controller script that changes the orientation while we're still in play mode i pause and then the idle animation has the hero in a non-contorted position so i create a new prefab out of the hero while that was post and then i stop and then i delete the original and i recreate the hero in the scene by dragging the new hero prefab that was created in play mode and now it has the correct pose a little bit of a trick to do so in play mode it looks the same but we removed the twisted post now while we're in editor mode and then i re-enable the hero character controller script again the camera follow script lost the reference to the old hero that was deleted so i have to reassign that i rotate the hero to face in the direction of the game so it's just starting in the default direction as well the character controller collider capsule is too big so i fixed that to make it fit the character a bit better time to add the run animation note in the animator window i drag the run animation into the canvas i right click on the idle state and i make a transition to the run state and i add another transition back again i add a new float parameter that i named speed and then i click on the transition arrow from idle to run and i add the condition to set speed to greater than 0.01 and then i do the opposite for the transition back so speed is less than 0.01 nothing happens yet because we need to set the value of speed parameter in the script so in the hero character controller script i add a reference to the animator component and set the speed parameter and we set that every frame and nothing happens at first but when the hero is selected in the hierarchy we can see that the animator plays the complete animation of the idle pose first so in the transition settings i untick has exit time to fix that the hero skidded at first so we'll reduce the transition duration as well it's looking better but the hero is running while he's still in the air so that needs to be improved as well in the animator window i create a new blend tree this blend tree will blend between a jump pose when going up and a fall pose when falling down i make the transition from any state because you could jump or fall both from idle and the run states i added a new is grounded bool parameter and then i added the condition is grounded equals false to transition to this state then i added the transition from the jump fall blend tree to the idle state when is grounded is true again in the script we have to set the is grounded parameter for the animator component we get stuck in the state and the blend tree is empty so we need to fix a few things still first of all let's increase the transition speed to the jump fall blend tree we need to disable has exit time and reduce the transition duration out of the jump fall blend tree as well it's a bit better now but the transition out of the jump fall blend tree should not first have to go to the idle state if you're jumping while you're running that can be fixed by adding the speed as an additional condition to either the run or idle state so we select which one we should be returning to problem again i often forget to disable the has exit time checkbox so get used to detecting and fixing that issue now the transitions are working and time to fix the jump ball blend tree itself in the blend tree i add two motion fields one for the jump pose and one for the fall pose i disable automate threshold and i manually set the jump threshold to 2 and the fall threshold to negative 2. one more parameter is needed so i add this float that i call vertical speed and i use that for the blend tree in the script we set the animator's vertical speed to the y component of the velocity variable now the jump fall blend tree blends between the jump pose and the fall pose based on the vertical speed of the character you can go to edit project settings time and lower the time scale setting to analyze the animation transitions in more detail when you have the object selected in the hierarchy you can also see the animator components state and values in the animator window here we see that the character is sliding a bit so we need to increase the speed of the run animation to match the movement speed still some sliding so let's make it run twice the speed of the original speed foot placement is good enough now i usually prefer this over root motion for platformers i just like to keep it simple the is grounded parameter only checks right under the hero's feet so it's not very forgiving when you try to jump when you're close to the edge we can add some more points to check for the ground i create a new child object of the hero that i call ground checks under that i create two more child objects that will act as position placeholders where i want to check if there is ground i position one towards the front of the hero and the other one slightly behind it's the last one that will help to jump when you are close to leaving an edge i move czech spear into a for each loop that loops through and checks any of the assigned positions if the ground is touched the reason why i'm not just increasing the radius of a single check sphere is because that would check too far below the character i demonstrate that with a simple temporary collider just so you can see we also have to assign check 1 and check 2 to the exposed ground checks variable i noticed that there is a little slide when the hero lands let's lower the time scale even more so we can see what's going on here's a problem the jump state keeps transitioning to itself which has a slight transition time in settings for the jump fall transition i uncheck can transition to self now there's no sliding on landing since it can transition to the run animation straight away i reset the time scale back to 1 and test it out in real time the run animation feels too fast so i'll accept some sliding and reduce the animation speed now we need to check what happens if the hero runs into an obstacle he gets stuck when jumping it's a little quirk of the character controller when a constant movement is applied when you hit something so to fix that i use a similar approach to the ground checks i just copy the position checks this time we need to check a little bit higher up so i moved the check objects a little bit higher in the script i copy and paste the checksphere behavior and i use the same logic when checking for walls this time now we'll only move the hero if it doesn't have a wall in front of him and we have to assign the check objects in the inspector good next we have to check if there's a small fall or a step down okay he falls off the tiniest edges so oh my god we i'll add another condition for the jump fault transition vertical speed has to be greater than zero so it only applies when it's jumping pretty good but uh he's still gonna need to fall at some point when he falls off the ledge as well i add another transition to the same state where is grounded is still true but vertical speed must be less than negative 20. remember to also set the transition duration and disable the count transition to self like i said i always forget that negative 20 was too much or should i say too little so maybe negative 10 is better yeah he transitions pretty fast now after falling down one more thing uh timing is a problematic so sometimes you press jump right before you land and the hero won't jump then so it feels a bit awkward like he's skipping the jump not responding to your jump command so to improve that i changed the script to cache the jump key being pressed for about 0.2 of a second so 200 milliseconds you can hit the jump button just before he lands and then he'll jump as soon as he lands instead that feels much better and i think there'll be a few less bad reviews so don't skip out on the polish i'm adding a sound effect to be played while jumping check out my universal sound effects package that i sell on my website it contains thousands of sound effects for any game genre so you can support me by buying that one i made my own sound effects by the way i record them as well in the field you should check that out i also modeled a few decorations earlier if you can remember so let's add those into the scene here i'm putting the clouds and put background objects at different distances if you want a parallax scrolling effect different distances will make them pan at different speeds the post-processing effects are already available in the universal render pipeline so i increase the vignetting a bit that makes the surrounding edges a little bit darker and then i add a color adjustment override and i increase exposure contrast and saturation e i'm going to add one more thing that's speed trails i create a child object of the hero that i call trails and that will be the parent of a few child objects i add the trail renderer component to the child object i reduce the trail time to one second and i make the trail taper off at the end and then i make it thinner to begin with as well i set the material to the default line material that's included in the default project and first i'll test it out with a single trail and that looks a bit dodgy but at least we know it's working for the color i set the alpha opacity to zero at the end and i also lower it at the beginning i make four copies of the trail object so we can have some multiple trails it looks a bit too static and boring with the solid trails so let's come up with something i could randomize them a bit so i create a new c-sharp script that i call something magic like random trails emit in the script i add some code to get the trail renderer component and randomly start and stop emitting the trail at different time intervals i think that looks a lot better than the static trails looks a bit cartoony and suits the graphics pretty good [Music] [Music] i hope you enjoyed this video and that it gives some insight how you could create this type of a game and remember i didn't actually do the endless part in this so it's a bit of a scam title i think because it's called an endless runner but that's more the genre where it auto runs and you have to just press one button to jump but in another video now i'm gonna be taking this exact project and we'll turn the level spawning into a procedural level spawning so it can basically keep you running forever and maybe gradually increase the difficulty but i'd say that that video is pretty much the same duration as this one possibly so i wanted to put that in a separate video for you hit the like button if you liked the video and subscribe if you haven't already so you don't miss that part 2 video maybe head over to patreon.com infensia as well because here you can download this project for the if you're in the tutorial tier so uh patreon.com for that one and until next time have a great one and i'll see you then bye bye for now [Music] you
Info
Channel: Imphenzia
Views: 89,715
Rating: undefined out of 5
Keywords: unity 2020, unity 2021, endless runner, low poly, endless runner tutorial, platformer, platformer tutorial, character controller, platform physics, run animation, jumping in unity, imphenzia
Id: N8QMuFPK4v0
Channel Id: undefined
Length: 32min 38sec (1958 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.