Unity 2D Platformer Tutorial 21 - Level management and teleporting between scenes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video i'm going to show you how to add levels and teleporting to a 2d platformer game in unity in this project i have two different scenes which are acting as two different levels in the game and i want the player to be able to teleport from one level to the next so to get this to work the first thing you're going to need to do is to create more than one level or more than one scene so uh i have my first level here as a scene and what i've actually done is in my scenes folder here is i've added a second level and there's a couple of ways that you can do this so you can either click on file and then click on new scene and you can build a whole new scene in your project or what you can do is open up a scene that you've been working on that you've been building and then click file save as and then save a copy of that scene uh with a different name and that's actually what i've done so i've worked on level one which i have open here and i built up this scene and then what i did was saved it as level one but then saved another copy and called it level two and then i opened up that level two scene and i just edited the scene just to change it so what that meant was i didn't need to add the player again i didn't need to add every single object to that scene again like platforms and trees and things like that all i needed to do was delete what i didn't want in that second scene and add what what i did want and and just make some changes so it looked like a different scene so now i have level one and level two and what i'm going to do in level one is i'm going to add a portal at the end here so when the player reaches the end of this level they'll teleport through a portal to the start of level two and i also want to have a portal at the start of level two so the player can get back to level one if they want to once you've added your scenes or levels and you have multiple scene files what you'll need to do is go into your build settings for the project so click file and build settings and uh in the build settings you should be able to see the scenes in build right now i only have level one listed so what you need to do is go and open each scene in your project and then click add open scenes so now i can see both level 1 and level 2 are listed as scenes in this build and each scene can be rearranged so you can change the order of the scenes and they each have an index or number okay and that's important because we're going to use that later to be able to cycle through the scene so if we're on level one we can just go to level one has an index of zero so what we can do is just increase uh the basically just go to the next uh index or next level number uh in this bill okay so we'll look at that a little bit later what i'm going to do now is i'm going to add a portal to this scene and i'm actually using the free pixel space platform pack by aiden art which is available on the unity asset store so i'm going to uh i've imported that asset pack and i'm going to find a portal i'm going to use this one and just scale that down because it's a bit too big and just position that on the platform there okay so the player will be able to walk through this platform and then teleport into level two what i'm going to need to do is add a collider so just search for collider and i'm going to choose a box collider 2d make sure that is trigger is checked because i want to detect when the player has passed through this box collider and i'm just going to edit the size of this collider to be the size of the portal and i'm actually just going to edit the left side so it's sort of halfway through this it's in the middle of this sprite what that means is if the players walking along here they're not going to teleport straight away as soon as they hit the edge of this portal they're going to teleport once they kind of get in the portal in the middle of it okay so just edited that collider like that then what you'll need to do is tag this portal to distinguish uh when the player is colliding with this object from when the player is colliding with other objects like spikes or checkpoints or the fall detector okay so i'm going to i've got this portal selected in the inspector panel here i'm going to add a new tag and i'm actually going to add two new tags i'm going to add one called next level and i'm going to add another one called previous level okay so these two tags will be used for two different types of portals one for when you want to get to the next level and one for when you want to get back to the previous level okay i'll select that portal again and i'm going to assign the tag next level okay i'm going to save that scene and i've just realized i'm actually in level two so and i don't have a level three so i'll go back to level one and basically do the same thing so i'm going to add a a portal here drag that in just here and just position that on the top of the platform okay so this is in level one again i'm going to add a box collider and just edit that to the size that i want and uh make sure that i have is trigger checked and i'm going to add that tag of next level okay so basically what i have now and i'll make sure that i save that scene as well is right here in level one i have a portal that is tagged as next level so and it's got a box collider with is triggered ticked so when the player reaches the end of level one i want them to pass through this portal and then it's going to take them to the beginning of level two and they'll spawn over here and then when they reach the end of level two um there's another portal here which is also tagged next level has a box collider with is trigger checked and that will take them to level three i don't actually have level three built yet um so to get that to work and to not have any errors um occur in the game i'll need to now make a level three as well i'm not going to do that now though what i will do is add another portal and in fact i can just duplicate this portal in level two so i'm just going to click on that portal and press command d and to duplicate or i could just right click and select duplicate so now i've got this portal which is going to be at the start of level two not where the player spawns though we don't want the player to be touching the portal when this uh scene loads otherwise they're gonna teleport back to level one again but on this portal at the start of this level i'm going to change its tag to previous level okay it says tag previous level which means when the player goes through this portal they'll teleport back to level one okay so the scene is set up now i've got some portals to go between level one and two and back and forth um i can spend a little bit more time creating another scene for level three and and build that up but i'll do that later what i'm going to do now is write some code to get this teleporting to work so i'll go into my scripts folder and open up the playercontroller script which is responsible for player movement and some collision detection and in the previous uh videos previous two videos i added this code here for detecting uh collisions so on trigger enter 2d this method here is called or runs when the player collides or passes through or enters the collider of another object so we check you know if the player falls through the full detector then they respawn back to the respawn point otherwise if they reach a checkpoint the respawn point is updated so if you're not sure how to do that you can go back and watch the previous two videos but all i need to do here now because i have this method set up for collision detection all i need to do is now just add a couple more if statements that check if i've collided with the an object tagged as next level or if i've collided with an object tagged as previous level and then just either go to the next scene in the list of scenes or go to the previous scene so in here i'm going to say else if and i'm going to say collision dot tag is equal to next level okay and then i'm also going to add another l-sit and i can just copy and paste that one and this one is going to be previous level all right so we check if uh collided with an object that's tagged next level or if we're collided with an object that's tagged previous level um and that's what the the portals are tagged as either next level or previous level all right before we can uh load another scene in the game all we need to do is go to the top of the script and just need to make sure that you've got this line of code there so using unity engine dot scene management and you'll need that line of code to work with the different functions for loading scenes so if you don't have that line of code here yet i added it earlier just make sure that you type that in at the top of your code now what we can do is when the player has passed through a portal that's tagged next level we can say scene manager dot load scene scene manager dot get active scene and then open close parenthesis dot build index plus one okay so what this means is i've just saved that script and if i go back into unity what this means is if we just go back and have a look at the build settings remember that we have this list of scenes in the game so level one and level two and we could add if we create a level three we could also add that here as well but each of these scenes has an index or number okay so if we are in level one then we are in uh scene zero or index zero and if we're in level two then we're up to index one and then level three would be index two uh level four would be index three and so on okay so if we're currently in uh level one which is scene index zero then if we add one to that then we're going to load the next level okay so basically we're just finding out what scene index we what scene we're currently in and what its index is and then we're just increasing that by one to load the next scene okay now to load the previous scene and just copy and paste that line of code and we would just decrease by one all right so that would go back to the previous scene in the list okay one thing you might also want to do is just update the respawn point as well so i'm going to say respawn point equals transform dot position after the player has teleported so if the player teleports to the next level uh immediately after they're teleported the respawn point will be updated to where they spawn in that scene all right so that's important because if the player dies in that scene or falls off one of the platforms uh we don't want the player to respawn to uh the last respawn point that was saved in the previous level we don't want them to go to that position um so copy and paste that we'll add that here as well so that will work for both scenes the respawn point is updated to where they teleport to in that next level okay you'll need to save that code and now we can go and test that out just need to make sure that i load level one first all right and remember this script is attached to the player which is in both of these scenes or both of these levels so it's it's going to work for both levels let's run the game all right so i'm just going to move around this scene and reach the end of level one and as soon as i hit this portal and pass through it i teleport into level two and then i could go along level two and explore that or if i want to go back to level one i can go back to level one okay uh so what will happen so if i just go back to level two again let's see what happens when we get to the end of level two because we don't actually have a level three yet so if i reach the portal here you'll see there's an error message that says popped up we go to the console it says scene with build index two couldn't be loaded because it has not been added to the build settings so you just need to make sure that if you're going to have a portal that in level 2 to take you to level 3 you need to make sure that level 3 exists and it's been added to the um in the build settings that it's been added to the list of scenes there by going into level three and then clicking add open scenes okay so what i'll need to do now is just go and add a third level and to make sure that we can fix that error and um allow the player to teleport to level three one last thing that i'll just mention here is that you might have noticed that when i um go from level two back to level one that i'm actually teleporting back to the very beginning of level one rather than you know maybe just to where the portal was at the end of level one okay so you can change this code you can actually also jump from one specific scene to another specific scene rather than just the next one in the list and if you want to do that you can use this line of code instead so you can say scene manager dot load scene and then between parentheses you can just specify the specific index or scene number that you want to load so you know if i wanted to specifically go to level 1 here and specifically go to level 0 or scene 0 index 0 then i could use that line of code instead so if you had maybe different portals in a scene that would take you to other specific scenes then you could just refer to the specific index number of that that scene okay and then what you could also do is you could just have different tags for each portal and then instead of um specifying the respawn point uh to whatever the position is of the player you could specify a very a specific respawn point and then move the player to that position so that each scene has its own specific respawn point when you're teleporting back and forth between scenes okay anyway i'm gonna undo that code because the existing code worked fine for the way that i've set this up that's it for this video in the next few videos what we're going to start looking at is adding collectables and scoring to the game and we'll also look at creating ui to display score we could add health as well and then we'll start looking at some particle system effects as well and sound effects and and how to really um improve this game and make it a polished 2d platformer game anyway that's it for this video thanks for watching
Info
Channel: Daniel Wood
Views: 798
Rating: undefined out of 5
Keywords:
Id: ILUbcxfvEMk
Channel Id: undefined
Length: 18min 37sec (1117 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.