Make A Game Like Pokemon in Unity | #45 - Additive Scene Loading 2 & Bug Fixes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 45 of my pokemon game series in unity so in this video we look at how to switch to indoor scenes while using additive scene loading we'll also fix some issues caused by additive scene loading and some other bugs that we have right now so let's look at how to do all that 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 all right so switching to an indoor scene is going to be pretty similar to any other scenes so we'll have to place it in the map and add it as a connected theme just like we do for other scenes the only difference will be in the case of indoor scenes the actual switching will be done by using portals so house one is our window scene so first let's place it in the map and connect it so i'll open the home town and the house one scene additively all right so i'll move the grid of the house one scene to the left of the hometown i'll put it at somewhere like -25 and by the way when placing a scene in the map we have to make sure that there is no other scene in this area so for example we have a root one scene over here so we can't place the house one scene above the hometown right so just be careful about that when placing scenes in the map so we have our house one scene so next we should create a scene trigger for it so i'll just duplicate the scene trigger for hometown i'll name it as house one and i'll place it on top of the grid so since i know the grid of house one is at minus 25 i can also place the trigger at -25 so next let's change the size of the box glider to fit the scene so i know the size of the scene is 12 by 14 so i'll set it as the size of the box collider all right and let me just change the x offset to zero so yeah it fits perfectly so next we need to define its connected themes so only home town is connected to house one so in the connected scenes we can add the hometown all right so next in the hometown scene trigger we should also had house one as a connected team so let's do that all right so now home town and house one scenes are connected so let's go to the gameplay scene try playing it and see if they are loading correctly so i'll minimize the game so yeah here you can see when loading the home town house one is also loaded so if i leave hometown you can see that house1 is unloaded and if i come back it's loaded again so these two scenes are connected properly so next let's look at how to transfer the player to the indoor scene so in the case of an indoor scene we'll have to transfer to it using a portal right so we already have a portal for loading a new scene so here this is a portal for loading scene two but we can't use this portal in the case of additive scene loading right because in that case the scene will already be loaded and the only thing the portal should do is change the position of the player to the position of the destination portal right it shouldn't load a new scene so let's create a separate portal for doing this so what i'll do is i'll duplicate my portal prefab and i'll name this one as location portal since it will be used for changing the location of the player so let's open this prefab and we don't want this portal script in it so let's remove that and i'll create a new script for it called location portal all right so let me open that in unity so i'll write a comment here so that we don't confuse between portals and location portals so location portals are going to be pretty similar to the portal so i'll just copy all the code from [Music] portal and place it inside the location portal class okay so the difference is in case location portal we don't need a scene to load variable so let's remove that and then inside this function we don't actually have to load a new scene so i'll remove this line and since we are not loading a new scene we don't have to worry about things like don't destroy on load so i'll remove that too so the reason why we have this error is because we have to import link so let me do that and while finding the destination portal we have to make sure to find an object of type location portal instead of a normal model right so this should solve any error we have here all right so finally one more thing i want to do is this function is named as switch scene right so let me change that to something like teleport in order to avoid confusion and by the way location portal should also implement the i player triggerable interface just like our normal portal if we don't do that then on player to good will not be called automatically so let me implement the iplayer triggerable interface so yeah that's all we have to do in the location portal so in the case of location portal all we are doing is changing the position of the player to the position of the destination portal right so this portal doesn't have anything to do with scene switching we can even use these portals inside the same scene itself so let's try creating these so in my location portal prefab i have to assign the location portal script and let me assign the spawn point okay so next let's place this prefab in the scene so i'll drag it here and i'll change its size to one and place it on top of the dough okay let me align it perfectly all right so we have the location portal on top of the door so we don't need the other portal anymore so let me delete that and now if i duplicate this location portal and place it somewhere over here then when the player steps on this portal he will be teleported to this one right so let's try testing that so if i step on this portal i get transferred over here right so the location portals are working as we expected so now what we have to do is place this location portal in our indoor scene so let me go to the scenes and let me open the house one scene additively so it's over here and i'll just drag this location portal into the house one scene so since we have the location portal we don't need the normal portal so i'll just remove that and i'll place a location portal at the entrance of house one okay so its size is going to be two and i want to paste place it right below the entrance right so let me just align it perfectly and then the spawn point of this portal should be above it so i'll change the white to one so yeah that's all we need let me just fix the name of it so now the player should be teleported here when he steps on this portal so let's go to gameplay scene and test if this is working and yeah we can actually minimize this to see how it's working so yeah you can see this is automatically loaded when we are in hometown and when we step on the location portal it will transfer us here okay so that works perfectly and we can also go back so yeah things are working perfectly and if i go to root one then the house one scene is being unloaded so yeah we found a way to make indoor scenes work while using additive scene loading so next i want to fix some issues that we have so the first issue is after we switch to a digital scene loading the while pokemon encounters won't work properly so let me explain the issue so let's look at what are the pokemons assigned in each scenes so in this scene we have bulbasaur assigned in the map area and in route 1 scene here also we have bulbasaur so let's change that to some other pokemon like pidgey all right so now when we walk through the grass of route 1 we should get pidgey and when we walk through hometown we should get bulbasaur right but it won't work because of an issue that we have so let me show you so if i walk through hometown that's working correctly we are getting bulbasaur but if i walk through root one we should get pg right but we are still getting bulbasaur so we have to fix this issue so let me first explain why we are getting this issue so if you open your game controller script inside the start battle function what we are doing is we are finding a game object of type map area and then we are getting a random pokemon from that list right it will return the first map area game object from the currently opened scenes okay so earlier we only had one scene so it will correctly return the map area of that scene but now we have multiple scenes open so this function might return a map area from any of the open scenes all right it might not be the scene in which the player is walking so yeah this is some of the problems with additive scene loading methods like find game objective type will return game objects from all the currently open scenes so let's look at how to fix this so an easy solution that i found for this is to move the map area from the grid of the game object to its scene trigger all right so right now the map area is inside the grid so we'll move it to the scene trigger of that scene so in the game controller since we already have the scene trigger of the current scene it will be easy for us to get the map area if it is in the scene trigger itself okay so let's move it on to the scene trigger so i'll copy this and i'll remove it from here and then i'll paste it in its integral all right so let's also do that for all the other scenes so let me first do that for down two and finally let me do it for root one all right so we have moved the map area to the scene trigger for all the scenes and we don't need a map area for house one since it's an indoor scene for all the other scenes you'll have to assign it so now in the game controller script in start battle what we can do is instead of using find objective type we can say current scene dot get component map area all right so this will get the map area of the current scene so the issue should be fixed now let's test so here i'm getting bulbasaur since only bulbasaur is assigned so that's working and oh come on and if i go to root one you can see that i'm getting a pg just like we assigned in the map area so that issue is fixed so next let's fix another issue that we have so right now when we try to test the scene independently like without loading the gameplay scene we will get this so what's happening here if you pass the game you can see that the player is being spawned over here right so this is because the essential objects loader will always spawn the object at zero zero zero position right so that was not a problem earlier because all the scenes were placed at the center but now that won't work since the scenes are placed at different positions in the map so we can easily fix this so what we can do is in the essential object spawner instead of always spawning it at zero we'll check if there is a grid and if there is one then we will use the center of the grid as the spawn position so if there is a grid then we should spawn at center so i'll create a variable car spawn position and i'll set it to 0 by default so next i'll check if we have a grid object in the scene and if we have one then i'll set it position to spawn position all right and finally when instantiating the prefab i'll use spawn position all right so let's try testing this so if we test the house one scene the player should be spawned at the center of the scene so let's also try testing some other scene so yeah it's also working for root one so now we can test all the scenes independently and in case if the player lands in a position that you don't want you can always pass the game find the player and then move it to a position that you want we are just placing it in the center for the convenience of testing all right so that issue is fixed so next let's look at another problem that we have so in indo scenes right now we don't have anything blocking the player to more of it so if i try to walk here i can go out of the indoor scene right so we need to fix this so to fix this what you can do is we can create a tile map like solid objects and place some solid objects around the scene and then we can make that tile map invisible right so first let me duplicate solid objects tile map and i'll name this as false okay so since we created a duplicate volts will have all the tiles from the solid objects so we don't want that so if we try disabling the solid objects you can still see objects like cupboard and bed since it's there in the vaults tile map so let's keep the salt objects disabled and remove these objects from the wall style map so i'll just take the eraser and remove it perfect so next we can place any tiles around the edge of the scenes in the volatile map so you can actually place any tile like this any tile will be fine since we'll make this invisible anyway but i don't want to use the styles instead i'll import a tile set with few basic colors so let me import that so this is my tile set it will be useful to mark different areas in our tile map so i'll first split these into individual tiles just like we do for all our tile sheets all right so i have split this and by the way i'll add a link to download this image in the description in case you want to use the same image so next let's actually turn this into a tile palette so i'll add a new palette and i'll drag this to the new palette so i'll be using this red tile to paint the walls so let me do that so we don't want to place the walls on top of the portal then we won't be able to go out of the house so i'll just place it like this okay so now if we try playing the game you can see that i can't go out of the house these styles are actually blocking me so next we don't want to see these styles right we want them to be invisible so for that we can go to a wall style map and then we can just disable the tile map renderer okay so this will make it invisible but what i actually want is i want to be able to see these styles when i edit the scene but when i play the game it should be made invisible so that's how i want it so we can turn it on by default and we can create a simple script that'll disable it on epic so inside scripts inside core i'll create a new script called invisible tile map so inside the script in the start function i'll grab a reference to the tile map renderer so in order to use tilenaprendro we have to import unityengine.tilemaps all right and then i'll set enabled equal to false okay so let's add the script to our wall style map and this should automatically disable it when we play the game so let's test it so yeah now you can see we don't have the walls and we are not able to go out of this house the walls are actually blocking us so let's also try testing it from the gameplay scene instead of just testing it directly so yeah even now the balls are blocking the player so that issue is fixed so i want to fix one more bug before we stop the video so the bug is when we walk through the grass you can see that it's playing a weird animation right so yeah you can see it's not actually playing the animation and for some reason it's getting messed up so let's look at what's causing this issue so inside the player controller script when the player steps on an iplayer triggerable object we are setting its animator dot is moving to false right so the reason why we are doing that is because let's say when the player steps on a portal we want the player to stop playing the animation right so if we come in this line and try to test it you can see that looks weird right so that's the reason why we are stopping the animation when the player steps on a triggerable object so the problem here is that long grass is also a triggerable object but it will not always trigger the action so let me explain what i mean if we go to the longer script you can see that it's a triggerable object but it'll only start the battle if this condition is also true right only one in 10 times it'll start the battle so in the case of long grass this should only be done inside this if condition okay only when we are starting the battle so we don't have access to character directly so let's say player dot character dot animator and then set is moving to false so this is how it needs to be done for long grass so the problem now is we won't be able to put this line over here right we will have to move this inside the on player triggered function so let's actually right click on this and select go to implementations so this will give us all the classes that are implementing the on player triggered function so long grass is one of them so let's go to each one of these classes and put the animator dot is moving inside it okay so let's also do this for the location portal and portal all right so all we did was we moved the line to stop the animation inside the on player triggered functions right and in the case of long grass we won't always do it we'll only do it if this condition is also true so yeah let's try testing this so yeah now you can see the animation is playing correctly and let's also try going through the portal to see if animation is being stopped so yeah that's working correctly the animations are being stopped alright so that issue is fixed so i'll stop the video here and in the next video we will start implementing our saving system so before you leave please make sure to leave a like and consider subscribing to my channel that'll really help me out and you can also support the cds by becoming a patreon so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 1,799
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 additive scene loading, unity multi scene editing
Id: 3csh9CMV0vU
Channel Id: undefined
Length: 29min 19sec (1759 seconds)
Published: Sun Apr 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.