Make A Game Like Pokemon in Unity | #41 Scene Switching 2 - Changing Scenes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 41 of my Pokemon Game tutorial series in unity. In this video, we'll look at how to switch scenes when the player steps on a portal tile. We'll also look at how to snap the characters to the center of the tile automatically using script.

If you're new to it, this a series where we'll create a Turn-Based RPG like Pokemon in unity using Design Patterns and Good Game Development Practices. I'd love hear what you think about it :)

Here is the entire playlist.

👍︎︎ 1 👤︎︎ u/GameDevExperiments 📅︎︎ Mar 14 2021 🗫︎ replies
Captions
hey everyone welcome to part 41 of my pokemon game series unity so this is the second video on scene management and in this video we'll look at how to switch scenes when the player steps on the portal alright so this is a new scene here special thanks to all my patreons for making the series possible by becoming a patreon you can support me in the making of the series and get access to some cool rewards like the complete project files of the series which also contains some advanced features that are not covered here so let's start the video so in the previous video we created the portal and right now when the player enters the portal we're just printing a log all right so instead of this we need to switch the scene when the player enters the portal so first let's create a second scene so that we can switch to that so inside scenes folder i'll create a new scene called root one all right and i'll rename our current scene to something like hometown all right so if we go to the root one scene right now we don't have anything it's just empty so here we have to create this scene using tile maps just like we did for our first scene so to save time what i'm going to do is i'll go to hometown and i'll copy all our title maps so i'll copy the grid game object and i'll paste it into the root one scene okay so in this scene let me go ahead and remove the houses and long grass and all that we have you can go ahead and create your second scene properly but i just need a blank scene for now to make the scene switching work so i can go to tile palette select the eraser and and erase the houses and grass so i can erase this one tile at a time or i can select multiple tiles from here and then select eraser so now our eraser will be big and we can easily erase this okay so next let me go to the long grass layer and erase that too and then i'll just go back and select a single tile select eraser again and i'll erase two fences over here oops i have to select the solid objects layer and i'll erase two fences over here so that the player has a way to enter the scene so what would happen is when the player go through here this portal he should come out of here all right so now we have the second scene so let's write the code to switch the scene when the player enters the portal so inside of a portal script instead of writing this log i'll create a new function called switch scene and this function is going to be a co routine okay so in this function in order to switch scene we can use scene manager all right to use scene manager we have to import unity engine dot scene management all right and to switch scene we can use scene manager dot load scene sync so basically we have load scene and load scene sync so the difference between these is load senior sync is like a core routine it'll let us wait until the scene is completely loaded so we want to use load cnsync and to this function we have to pass the index of the scene that we want to load all right so what's the index of the scene so we have these two scenes right here but in order to switch the scenes we should first go to the build settings it's inside file and build settings and in here we have to add our scenes into the scenes in build list okay so we can simply drag and drop these scenes into this list and it'll appear here so once you do that you can see a number over here so home town is zero and root one is one right so this is the index of the scene and this is what we have to pass for switching the scene so let's go back to our script and here we can pass the index of the scene so what i'll do is i'll create a serialized field called scene to load so this will take the index of the scene that we want to load all right so by default i'll set it to something like -1 just so that we'll get an error if we forget to set this from from unity so now we can pass this variable to our load scene sync function and since this function is an async function like a core routine we can use heal to return in order to wait for its completion all right so that's all we need so let's call the switch swing core routine from here okay so let's go to unity and in the portal i'll set the scene to load to one since that's the index of the second scene and let's try testing this okay so if i step on the portal the scene switches and we are now in the second scene so if you pause the game you can confirm that this is the second scene but the problem is we don't have the player in the second scene so we can't move around so what we should do is things like player game controller battle system and all that shouldn't be destroyed when we switch the scene right right now when we load the second scene everything gets destroyed and we don't have anything in the second scene so we have to make sure that essential objects like player game controller and all that will not be destroyed so to achieve this what i'll do is i'll create a new game object called essential objects alright and i'll reset the position of this game object to zero so this is very important this game object is going to hold all our essential game objects so it's important that the parent game object is placed at zero otherwise it will get hard to manage things that are under this game object okay so next we should put all our essential game objects under this game object okay so i'll select the player the battle system and even system is part of the canvas so i'll also select that and i'll select the game controller and i'll select the canvas so inside the canvas is where we have our dialog box so we need to select the canvas also and i'll drag these into the essential objects so now all these game objects are a child object of the essential objects game object okay so now what we have to do is we have to make sure that the essential objects doesn't get destroyed when we load a new scene right so for that what i'll do is i create a new script so i'll create it inside a folder called core i just named it code because this is something that we can reuse in multiple games it's not just specific to pokemon games so here i'll create a new script called essential objects okay and i'll assign it to our game object so let me open this up so in here what we have to do is we have to make sure that the game object that is holding the script doesn't get destroyed when loading a new scene right so unity has a function called don't destroy on load in order to do this so inside the awake function i'll call don't destroy on load and for the game object i'll pass the game object of this component okay so now whenever we switch scenes the essential objects should not be destroyed so let's try this out okay let me see the scenes oops looks like we have a problem so let me try to pause the game so yeah here you can see don't destroy load is working properly but the problem is we have two cameras in the scene so let's go to the root one scene and let's remove the main camera right we already have a camera attached to the player so let's test now okay so this is scene two so you can see that scene switching is working and all our essential game objects are transferred to the second scene so next when we switch the scene the player should appear over here right right now he's just appearing in the same position of the previous scene we don't want that instead when the player goes through here he should appear over here all right so what we have to do is after switching the scenes from the portal we should update the position of the player so that he is placed correctly in the new scene so how can we get the position at which we should load the player so for that what i'll do is in our portalgame object i'll create a game object called spawnpoint under the portal okay so let me use a different icon to indicate spawn point i'll just use this diamond so the spawn point will give us the position at which the player should spawn when the player loads to the scene okay so let's say the player is transferred from a portal in another scene to this portal then the player should spawn over here right so what i'll do is i'll select the spawn point and i'll change the y to one actually i'll change it to minus one so that the spawn point is over here so next we need to add a portal in the second scene also so first let me just turn this into a prefab so inside game i'll create a new folder called scene management and i'll drag and drop the portal here in order to turn it into a prefab all right so now let's go to the second scene and i'll place a portal over here okay let me just fix its position all right so in this case when the player is transferred to this portal he should spawn over here right so the spawn point should be at the y position of one instead of minus one all right the player should spawn over here so let's grab a reference to the spawn point from the portal script so here i'll create a variable to store the spawn point okay let's assign it from the inspector so when assigning it you have to make sure to go inside the prefab and assign it from here okay if you don't do that the change will only be reflected in this game object not all the objects of the prefab all right so now we have the spawn point at which we should spawn the player so we can simply update the position of the player to the spawn point right but there is a problem so let me explain so this portal over here is responsible for switching to scene two right now once we load the second scene that is once this function is complete the portal will be destroyed right and if the portal is destroyed this script won't execute any further so right now what will happen is after this line nothing will be executed in the script so let me try to prove it to you by writing a log so i'll just write some log here so this log won't appear in our console since the portal will be destroyed before that so let's test this okay so if i switch the scene you can see that the log we wrote doesn't appear over here so since we have to update the player's position after loading the scene we need to find some way to fix this so what we can do is we can use the don't destroy our load function to make sure that the portal won't be destroyed okay so just like for the essential objects we can use this function to make sure that the portal won't be destroyed when a new scene loads but after we finish executing all our logic we need to make sure to destroy it okay so in the end i'll say destroy game object so the portal won't be destroyed until we execute all our logic and after that we'll destroy it so now let's test if the log is appearing okay let me switch scene real quick all right so now the log is appearing so next instead of just writing this log we should update the position of the player to the spawn point of the destination portal so first let's find the destination portal so we can use find objects of type and the type here is portal so this function will return all the portals in the current scene so we just need the first one so i'll say dot first so we have to import link in order to use first and that should return the first portal but we need to add one more condition here even though this will only return the portals in the current scene remember that the portal through which we switched the scene is not it's not yet destroyed right it's not yet destroyed even though the new scene has been loaded so we just have to add an extra condition over here saying x not equal to this so this line will return the first portal in the current scene which is not the portal we used to transfer all right so let's store this in a variable called destination portal i'll just call it desk portal for short and then we can get the spawn point of the dust portal to get the position at which we should place the player right so first let's create a property to expose the spawn point all right and then we can use that to set the position of the player so first let's cache the reference to the player from this function okay and i'll cache it from here all right and now we should be able to say player dot transform dot position and set it to destination portal dot spawn point dot position all right so this should work right but before we do this let me explain a problem that we have with our current approach of how we position our characters so right now all our characters like player trainer and npc and r needs to be placed at the center of the title actually it's not at the exact center it's slightly above the center to give some perspective so all our characters like trainers and npcs if you look at their positions the exposition needs to be 0.5 and the y position needs to be 0.8 or 0.2 if it's negative in order to place it in the way we want so the problem here is the designer who's going to build the world of this game will have to manually do this for all the characters right and since we created portals right now they'll have to also do this for spawn points all right spawn points should also be placed at the center of the tile to make sure that the player is placed at the center so the point is it'll be a pain for the designers to place all the characters and spawn points and all that exactly in this way so let's try to automate this using code so what i'll do is in my character script i'll create a new public function called set position and snap to tile so this function will take the position which we want to set all right so in here we have to snap this position to the center of the title so for that i'll say position dot x equal to math f dot flow of position plus 0.5 okay so let me explain what this does so let's say the position to set was something like 2.3 okay but that's not at the center of the title we have to snap it to the center so after passing through math.floor the fraction of this decimal will be removed so this will become 2 and after that we'll add a 0.5 and make it 2.5 so now it'll be at the center of the tile okay so that's how it works so let's also do this for the y okay all right so for the y we don't want it to be at the exact center instead we want it to be placed slightly above to give some perspective so here you can see the y is at 0.8 so in our player controller we have an offset here which we can reuse so what i'll do is i'll move this to the character script so i'll make this a property instead okay and i'll set it value to 0.3 so here we can say 0.5 plus offset y so now from the player controller we can remove this and use the property that we created here so here i'll say character dot offset y all right so let's go back to the character script and in the awake function when the character is initialized let's also call this function in order to snap it to the center of the tile so for the position i'll just pass the current position so basically what this will do is this will snap the current position of the character to the center of the tile and by the way we forgot to set the updated position back to transform.position so let's set it so now let's go to unity and test this so let me set the position of the player to some random position and i also do that for the trainer so now they are not at the center of the tile or anything but when we start the game they should automatically snap to the center of the nearest style so let's test this okay so if we pass the game you can see that both the player and the trainer has been snapped to the center correctly all right so we can also test if the perspective and things are working properly so yeah this is how we want it so now let's go back to setting the position of the player after scene switching so in our portal script after switching scenes instead of setting the position directly we can call the new function that we created in character so we don't have access to the character so in player controller let's create a property to expose it and now we can access player.character and call to set position and snap to dial function okay so for the position i'll pass destination portal dot spawn point dot position so now the player's position will be set to the spawn point of the destination portal and we don't have to worry about placing the spawn point at the center of the tile or anything this function will automatically do it for us so let's test this okay so if i go through this portal i'll be placed on the spawn point of the destination portal so that's working correctly so now we are switching the scenes correctly but there are a lot more issues that we need to fix and improvements we need to make so for example if i go back to the starting scene oops i'm not going to the starting scene i'm just loading the same scene again so let's go to root one and for this portal the scene to load should be zero since that's the index of the hometown so let's try that i just wanted to show you a bug that we have right now so if i switch scenes and then come back to the first scene then you can see that the player is placed at its initial starting position so if we try going up you'll see this weird bug where there are two players in the scene so in the next video we will fix issues like these and do a lot more improvements to scene switching so i'll stop the video here if you think this video is helpful please leave a like and consider subscribing to my channel that will really help me out and you can also support the series by becoming a patreon so i hope it was helpful and i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 2,556
Rating: undefined out of 5
Keywords: make a game like pokemon in unity, how to make a game like pokemon in unity, make pokemon in unity, make pokemon game in unity, how to make pokemon game in unity, make a pokemon game in unity, unity pokemon game tutorial, unity scene switching, unity scene management
Id: QN2LoBUlbGE
Channel Id: undefined
Length: 28min 36sec (1716 seconds)
Published: Sun Mar 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.