Make A Game Like Pokemon in Unity | #16 - Switching Pokemons in Battle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 16 of the pokemon game series in unity so in the previous video we looked at how to create a party screen in which we show all the pokemons in our party so in this video we will look at how to switch pokemon when we select a new one in the party screen a special thanks to all my patreons for supporting me in this journey so by becoming a patreon you can support me in the making of the series and you'll get some cool rewards like access to the complete project files so with that let's start the video okay so first of all in party screen we should allow the user to select one of the members just like we select moves and actions so in our battle system script first we need to add a new state for the party screen okay i'll call it party screen and let's set that state in the open party screen function okay and in the handle update function if the state is equal to party screen then we need to recall a new function called handle party screen selection so i'll just create it just below the move selection i call it handle party selection and i'll call it when the state is party screen so the selection logic of the party members is going to be just like the logic for the move selection so first we need a new variable called current member which will store our current selection so and let me just copy paste the selection logic of the move selection and instead of updating the current move we should update the current member so i'll just use ctrl alt and click to add multiple cursors and rename all of this to current number and we should actually be limiting the value of current member between zero and the number of pokemons in the player party minus one so instead of the move count i'll say player party dot pokemons dot count minus one okay so now we're done with the logic of the selection but we need to update that in the ui so for that we have to make a function like this one that we made for the move selection so inside the party screen script i'll create a public function called update member selection and this function will take the index of the selected member so in this function first we need to loop through all the pokemons so first i need to cache the list of pokemons in my party and in the set party data i'll just assign it and in the update number selection we loop through all the pokemons so i want to loop until i is less than pokemons.com and if i is equal to equal to the selected member then we just need to highlight the name of the member in the ui so in my party member ui script i'll create a public function which can be used to highlight remember so i'll call this set selected and i'll take a bool to specify whether i should select it or deselect it and i'll just create a variable to store the highlighted color so if selected then we'll change the color of the name text to the highlighted color and otherwise i'll just change it back to black which is the default color okay so let's call this function inside our party screen so if i is equal to selected member then we'll set the member slot at that index as selected okay and otherwise i'll just say set selected false in order to deselect it and finally let's just call this function inside our handle party selection and i'll just pass my current member okay let's just test this out so i'll go back to unity and we need to set the highlighted color from the inspector so i'll go inside my party member prefab and change the highlighted color to something like blue and also change the alpha to 255 otherwise it's not going to be visible so i'll just go back out of the prefab and all the instance of it should have the highlighted color set so let's just disable the party screen and also disable our battle system and let's test the game let me just start a battle real quick quick so if we go to our party screen you can see that i can select between the two members that i have in my party so next when we select a new party member we should call back our current pokemon and send out our selected pokemon so inside our handle party selection if z key is pressed first i'll grab a reference to the selected member and before switching our current pokemon with selected member we need to check for some conditions so our selected member should not be a faint pokemon right so i'll say selected member dot hp less than or equal to zero then we should not allow the switch so i'll just return and i also want to show a message in the in our message text so yeah we have a message text inside our party screen so let's create a public function through which we can set its text so i'll name the function set message text and it will take a string called message and i'll just set message text dot text equal to message so now in the handle party selection if the player selects a fainted pokemon i'll set a message like you can't send out a fainted pokemon so next i need to check if the selected member is same as the current pokemon so in this case we'll set a message like you can't switch with the same pokemon and we'll just return so now if the player actually selected a valid pokemon then we need to switch it with the current one so i'll create a co routine called switch pokemon and it will take the pokemon to which we should switch in the parameter so in here first we need to call back our current pokemon so i'll set the dialog to something like come back and the name of the current pokemon and then i'll just play the faint animation on the current pokemon so you can actually create a new animation for returning a pokemon but i'll just use the faint animation for now and then we'll just wait for two seconds in order for our animation to complete okay so we called back our current pokemon and next we need to send out our new pokemon so we already have written the code to send out the next pokemon when the current pokemon faints so let's copy that code so this is what we should do to send out a new pokemon so i'll just paste that here and instead of next pokemon we should choose our new pokemon parameter so let me just rename all of that so finally once we switch the pokemon the player's turn is actually over so i'll just call the enemy move core routine instead all right and we need to call the such pokemon when the z key is pressed so in here before switching the pokemon i'll just disable the party screen and i'll just set the state to busy so that nothing has will happen and finally i'll just call the co routine to switch the pokemon and i'll pass the selected member for the new pokemon okay so we are done with the logic for switching the pokemons and next i want to do one more thing so if the x key is pressed then i just want to go back to the player action state okay so now let's just if all this is working so before we test let me also add few more pokemons to my party so that we can test our party screen completely i'll add five pokemons this one is going to be a purpose of level two and x1 is going to be a bulbasaur of level five and the last one is going to be level six okay so now we should see all these pokemons in our party screen okay let's test the game now and let me start a battle all right so if i go to my party screen we can see all our pokemons here and if i try to switch with the current pokemon it'll show the message you can switch with the same pokemon and if i choose another pokemon in the party we'll call back our current pokemon and send the new one okay so enemy gets a turn since the player's turn is over once he switches the pokemon and now i can just fight this pokemon just like usual or i can switch it with another pokemon so awesome you have implemented pokemon switching in battle so one last thing that i want to do is right now if our current pokemon faints we are just sending the next pokemon right but instead what i want to do is i want to open the party screen and let the player choose the next pokemon so in our battle system script inside the enemy move if the player pokemon faints then we're just switching it with the next healthy pokemon in the party okay so and so instead of doing that i'll just open the party screen and let the player choose the next pokemon instead so yeah we can still use this condition since it'll let us know if there are any more healthy pokemons in our party and we can actually stop the battle if there aren't any so right now when if our player pokemon faints we are playing the faint animation and then opening the party screen so that player can select the next pokemon but inside our switch pokemon we are actually calling back the current pokemon but we don't want to do that in case our current pokemon is fainted since it has already played the faint animation so i only want this code to execute if the current pokemon has not painted so i can simply check if playerunit dot pokemon dot hp greater than zero and i'll only execute all this if this condition is true okay so now let's test this all right i'll just switch to another pokemon and once this pokemon faints we should go back to our party screen okay so we are back in our party screen and we won't be able to select this pokemon since it is fainted and if we choose another one we will send that out so that's working just as we wanted so we are done with our pokemon switching feature so i'll stop the video here make sure to leave a like on the video and consider subscribing to my channel if you haven't already and you can also support the series in patreon if you think you have gotten some value out of it and if you can afford it so i'll see you in the next video thanks for watching
Info
Channel: Game Dev Experiments
Views: 4,637
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: TgchdpVOLAE
Channel Id: undefined
Length: 17min 7sec (1027 seconds)
Published: Fri Jul 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.