Make A Game Like Pokemon in Unity | #12 - Starting and Ending Battles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hi Everyone, in the tutorial, we'll start Random Battles when the player walks on the grass, and when one of the pokemons faint we'll end the battle and go back to our free roaming state. We'll manage the Free Roam and Battle States by using a Sate Design Pattern and we'll also look at how to use Observer Design Pattern to avoid circular dependency.

If you're new to this series, here is the entire playlist.

Let me know in the comments what other feature you would like me to cover.

👍︎︎ 2 👤︎︎ u/GameDevExperiments 📅︎︎ Jun 29 2020 🗫︎ replies
Captions
hey everyone welcome to part 12 of the Pokemon game series in unity so in this video we will look at how to start a battle when the player walks through the grass and also how to hint the battle and go back to the free roam state once one of the Pokemon faints so before we start the video I just want to let you guys know that I've started a patreon page and you can help me continue making the series by supporting me in patreon and you also get some cool rewards like an exclusive patreon feed where you can ask me any questions you have access to script files which you can use as a reference while following along and I'll also be planning the next videos in the series based on what my patreon want to see so if you think you have got some value from the series and if you can also afford it please consider becoming a patreon and support me in this journey so let's get started so first let's look at a problem that we have right now so if I drag my game window here and on the same window I want to see my player and if we test the game you can see that our players are also moving when I try to select an action so this is because both our playercontroller and battle system has an update function in which they are trying to get the input and do something so in order to fix this we are going to use a state design pattern so we are going to have a game controller inside which will stow the game state and depending upon the state we will give the control to either the player controller or the battle system so both of them can't have the control at the same time so in the scripts I'll create a new script call the game controller and inside the game controller we need a way to show the game state so I'll create an enum for that I'll name the two states as free roam and battle so now inside our game control we can create a reference of the game state and in the update function if the state is free wrong then we'll give the control to the player controller and otherwise if the state is equal to battle then we'll give the control to the battle system so now I need reference to both player controller and vassal system so first I'll create one for the player controller and then for the battle system so if we go inside the player controller you can see that we have in a plate function so I I'll say in the name of sis function from update to handle update so that it won't be called automatically by unity and we can call this from inside the game controller and we also have to make it public so similarly in the battle system I'll change update to handle update and I'll make it public so if if the game state is free Rome I'll call player controller dot handle update and if it's battle I'll call a battle system dot handle update so initially the game is going to be in the free roam state since this is the first state in our innum so in the scene we will disable the battle system and enable our main camera so when the game starts we will be in the free roam state and now when the player walks to the grass we need to start a battle so if you look inside our playercontroller script we have already written the logic that'll create random encounters with while Pokemon we did this in part four of the series so instead of just printing this we should start the battle by going into the battle State when this happens so how do we tell the game controller that we need to go to the battle State so one way is speaking grab reference to the game controller in the player controller and this caller function but the problem with this is we already have a reference of the player controller inside the game controller so if we create a reference back to it then this will create a circular dependency which can cause a lot of problems and it is something that we want to avoid so instead we are going to use the observer design pattern to solve this so inside our player controller we will create an event and the game controller will subscribe to the ribbond and whenever there's an encounter we'll just invoke that event and any of the game objects that are subscribed that event will be notified that it happened so in the player controller I'll create an event called on encountered so in order to use action we need to import the system namespace and I'll name this event on encounter and finally in the check for encounters we need to call our event instead of just printing this so we have an error here in the random this so this is because both here in tianjin and our system namespace has a random class and since we just imported the system namespace now this code doesn't know which random class we are referring to so here I'll just say unity engine dot random all right and now in the game controller in the start function we will subscribe to the event that we created and we'll call a function called start battle when this event is fired so let's create that function now so inside start battle function first we need to set the state to game state dot battle and then we need to enable the battle system game object because initially it is disabled so I'll say battle system dot game object dot set active true so this will enable the game object to which the battle the system script is attached and finally we need to disable the main camera which is the camera of our world so that we don't have two cameras active at the same time so I'll grab a reference to the main camera and I'll just call this world camera and here I'll just disable it so now you go to unity and create a new game object to hold the game controller script and I let's attach the game controller script to it and assign the player controller in the battle system and the main camera now let me just make the main camera a child object of the player so that week it will move along with the player and we'll change the x and y position to zero so freitas the game now and if we walk through the grass you can see that it starts a battle and the player doesn't move when we try to select an action but one thing you can note is that the player is actually being animated when this happens so this is because we aren't calling handle update anymore once the battle starts so the east moving is not set to false so let's set that just before starting the battle and it will solve that issue ok so now we need to end the battle when one of the Pokemon faints and we need to go back to the free roam State so in our battle system please create an event called on battle over I can use a shortcut control dot to quickly import the missing namespaces and I'll call this event on battle over and to this action we will also add a bullying parameter so that they can let the game controller know whether the player born the battle or lost the battle so inside perform player moko routine if the enemy faints then first we wait for two seconds and then we need called the on battle over event and pass true to its because enemy Pokemon is the one that fainted and player won the battle and in case of the enemy move if the player Pokemon fainted I'll do the same thing but I'll pass false to indicate that the player is the one who lost so now in the game controller let's subscribe to this event and we'll call a function called n battle and let's create the end battle function so this function will take a boolean which will specify if the battle was born or lost and inside this function first we'll set the state back to free roam and then we disable the battle system and finally we will enable our world camera so if we test again now so you can see that when the battle starts the player is not in the animated State anymore okay so if I use a move all right so I had change the level of vole pisode 50 earlier just for testing so let me change that back otherwise we won't be able to beat him so I'll change the level back to 5 so let me just place the game back here and I'll sell it maximize on play and now if we test the game I can walk through the grass and start a battle and if I try to kill the enemy Pokemon you can see that once it faints we wait for 2 seconds and then go back to our free roam State but there is one more shoe that we need to address so if we start another battle you can see that it continues our previous battle instead of starting a new one so let's fix that inside the battle system right now we are calling the set of battle cordeen from the start so it will only be called once so we need to be able to call this whenever we start a new battle from the game controller so I'll rename this function as start battle make it public and call this inside our start battle function in the game controller and there is another issue right now in our battle unit at the end of a battle will play the faint animation so this will change the alpha of our image to 0 to make it invisible so in the setup function we need to reset the color of our image so to do that I will set image dot color to the Koller so now if we test the game let me end this battle quickly and we should go back to our free roaming scene and you can see that when I start a new battle we don't have that issue anymore it starts an entirely new battle so I'll stop the video here if you think this video is helpful please consider subscribing to my channel and leave a like on the video that'll really help me a lot and I'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 8,377
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 turn based battle system, turn based battle system in unity
Id: R9XMOEFne7w
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Mon Jun 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.