Make A Game Like Pokemon in Unity | #34 - Catching Pokemons

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 34 of my Pokemon Game tutorial series in unity. we'll implement the most important feature of pokemon game "Catching Pokemons". Once you catch a wild pokemon it will be added to you party, and you can use it future battles.

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.

πŸ‘οΈŽ︎ 8 πŸ‘€οΈŽ︎ u/GameDevExperiments πŸ“…οΈŽ︎ Dec 20 2020 πŸ—«︎ replies

Wait, you're up to 34 parts on this? DAMN. Well done sticking with a series, whereas so many make an intro and disappear into the void

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/jonbrant πŸ“…οΈŽ︎ Dec 21 2020 πŸ—«︎ replies

the tutorial continues!

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/AFugginHedgehog πŸ“…οΈŽ︎ Dec 20 2020 πŸ—«︎ replies

Nice!

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/BimzyCodes πŸ“…οΈŽ︎ Dec 20 2020 πŸ—«︎ replies
Captions
hey everyone welcome to part 34 of my pokemon game series seniority so in this video we will implement the catching mechanic so we haven't implemented the items and inventory system yet so what i've done is if we choose the bag action we'll use the pokeball and try to catch the wild pokemon so we'll change this once we implement the inventory system but for now we'll just use the bag action to throw the pokeball so if i select the back action you can see that i'll draw the pokeball and we'll try to capture so this time i wasn't successful and the pokemon broke out of the pokeball but if we reduce the health of the pokemon then we'll have more chance of catching it alright so now if i try to catch the pokemon you can see that i was able to cast the pokemon successfully and the pokemon will be added to our party so let's look at how to implement this 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 first we need to create a game object for the pokeball so for that first we need to import a sprite for it so inside my art folder i'll create another folder car items and i'll drag and drop my pokeball sprite sheet into it so let's change its import settings i'll change the sprite mode to multiple pixel per unit to 32 filter mode to point no filter and compression do not okay i'll hit apply and now we can open this in sprite editor and slice this into individual sprites so i'm just going to slice this automatically okay so now you can see that the sprites are sliced properly so this is a sprite that i need so i'll just rename this to pokeball okay and i'll hit apply so next i'll create a game object for our pokeball so i'll go ahead and add a sprite and i'll rename this to pokeball and for the sprite i'll just choose my pokeball sprite so we want the pokeball to be rendered on top of the battle system right so for that what i'll do is i'll create a new sorting layer called battle wait i don't want to add a layer i want to add a sorting layer instead so i'll create a new sorting layer called battle and it will be below all the other game objects so that it appears on the top all right so first i'll assign my battle canvas to the battle sorting layer okay and i'll keep the order in layer as zero and then i'll assign the pokeball also to the battle sorting layer and for the pokeball i'll set the order in layer to 10 so that it'll appear on top of our battle system okay so let's test that by enabling our battle system and placing the pokeball on top of it okay so you can see that it is being rendered on the top just like we want so i want to instantiate this pokeball from the code so what i'll do is i'll turn this into a prefab so inside my game folder this is where we keep all our prefabs so here i'll create another folder called items and i'll drag and drop my pokeball game object into this folder so this will turn it into a prefab and now we can just delete our pokeball from the scene so let's also disable our battle system since we don't want it to be active by default so next let's try to instantiate the pokeball freefab from the code so for that first we need a reference to the pokeball game object so inside my battle system script i'll create a variable to store the pokeball game object okay and then i'll create a function called throw pokeball so this function is going to be according so inside this function first i'll set the state to pc so that nothing else will happen when throwing the book bar and then i'll show a dialog like the player use the pokeball all right and then we need to instantiate the pokeball prefab so to instantiate the prefab we can use the instantiate function enter the function first we need to pass the game object to instantiate so that will be our pokeball sprite and then we can pass the position at which we want to instantiate it so let's say i want to instantiate it at the position of my player pokemon so for the position i'll pass player unit dot transform dot position okay and finally we need to pass the default rotation in which the prefab should be instantiated so we don't want it to have any rotation so for the rotational pass quaternion dot identity okay so this is what you use if you don't want any rotation so this function will return a reference to the instantiated object so i'll show this inevitably called pocobar object all right so next to make this function easy to test i'll call this function when the user presses the t button okay t for test so inside my handle update function if the user presses the t button i'll call the throw book bar corrupting okay so let's go to unity and test this so before we test we need to assign the pokeball sprite to our battle system script so let me do that and now let's test the game so if i start a battle all right and if i press t oops we have a null reference error at line number six not one so let's go to that line okay so it's this line so we are getting the null reference exception when calling player.name so if we look at the definition of our player variable we are assigning the player from start trainer battle function but we are not doing it from start battle function right so in the case of a wild pokemon battle we are not actually initializing this and its value will be nodded so that's why we are getting the node reference exception so let's also initialize the player from the start battle function and now let's test this okay so now if i press t you can see that first we are showing this dialogue and after that we are instantiating the pokeball prefab at the position of the player pokemon alright so next we need to implement the pokeball throwing and capturing animation so these animations can be divided into four steps first we need to throw the pokeball to a position on top of the enemy pokemon second we need to play the animation where the enemy pokemon is put inside the pokeball third the pokeball should drop down and fourth the pokeball should shake okay so we can create all these animations by using bluetween right so let's do that so inside of the pokeball function first i'll grab a reference to the pokeball sprite from the pokeball game object so that it's easier to animate so i'll show this in available car pokeball and i'll say pokeball object dot get component and we need to get the sprite so i'll get the sprite renderer of the game object okay so next we need to implement the animations so first we need to throw the pokeball at the enemy pokemon so i'll use dotween for implementing all our animations so to throw the pokeball i can say pokeball dot transform and then call the do move function of two tween so in order to use do mo we have to import the do twin library and now you can see that we have a function called do move all right so we can throw the pokeball towards the enemy pokemon using more but the problem with domo is that it will move the pokeball towards the target position in a straight line so instead of do move i'll use two jump so do jump will make the pokeball jump towards the target position so it'll look like we are throwing the pokeball all right so i'll be using do jump and for the first parameter we have to pass the target position so the target position will be the position of the enemy unit and i want the target position to be little above the enemy's position so i'll add a vector of zero comma two all right so the target position will be two units above the enemy's position so next we need to pass the jump power so i'll just pass two for it you can play around with this value and find another number that you like but i'll just go with two and next for the number of jumps we just need one jump and finally for duration i want the animation to play in one second so i'll pass one all right so this will throw the pokeball at the enemy pokemon so before playing the next animation i want to wait until this animation is complete so for that in doing we can use the weight for completion function and this will return a core routine and then we can simply use the yield return in order to wait until this animation is complete okay so we have an error here so since position is vector 3 we need to add vector three to it okay so now the error is gone so we're done with the pokeball throwing animation but one thing i want to change is right now we are instantiating the pokeball at the player pokemon's position right but i don't want to start throwing the pokeball from there instead i want this position to be towards left of the player pokemon so i'll subtract two from the player pokemon's exposition okay so now if you test this is how the animation should look like so next let's implement the capture animation that will put the enemy pokemon inside the pokeball so since this is an animation on the pokemon i'll actually implement it inside the battle unit script all right so we already have lots of animations over here so here i'll create another function called play capture animation so first i'll create a sequence so in the sequence first i want to fade the image of the pokemon and make it transparent so i'll use image dot do fade and i want to fade the alpha to zero and i want to do it in 0.5 seconds okay so next while fading the image i also want to move the pokemon up towards the pokeball so since we want to play both animation at the same time i'll use sequence.join and to move the pokemon up i'll say transform dot do move y and for the y position i will pass the original y position minus 50 okay so we also want to do this animation in 0.5 seconds so i'll set the duration as 0.5 so next along with these two animations i also want to reduce the scale of the pokemon when it is moving towards the pokeball okay just to make it feel like the pokemon is going inside the pokeball so for that i'll say transform dot to scale so i want to reduce the scale to [Music] 0.3 and once again i want to do this in 0.5 seconds so we're done with the capture animation and i want to wait till this capture animation is complete so i'll call sequence start wait for completion all right and in order to wait we have to make this function a co routine okay so let's call this function from battle system so after throwing the ball towards the enemy pokemon i'll call enemy unit dot play capture animation and i'll add yield return at the start since this is according okay so we're done with the captcha animation so this is how it's going to look like so next we want to drop the pokeball towards the enemy units platform so for that i'll say pokeball dot transform dot to [Music] move phi so i want to move it down by one unit so for the position i'll pass the current position minus one okay and i want to do this in 0.5 seconds so let's also call wait for completion and wait until this animation is complete all right so finally let's make the pokeball shake so let's say we want to make it shake three times so i'll create a for loop that'll run three times and inside it to make the pokeball shake we can call pokeball.transform.2 punch rotation so this will rotate the pokeball in multiple directions randomly and for the first parameter we have to pass a punch value which is the strength of the rotation so for that i'll pass a vector 3 which is going to have 0 for x and y but for z i'll say 10 all right in 2d we'll only do rotation in the z axis and the second parameter is the duration so i want this to happen for let's say 0.8 second all right so let's call dot wait for completion so this will shake the pokeball three times and let's say between each shake i want to wait for 0.5 seconds so i call wait for second score routine okay so we're done with all the animation so let's test this so if i start a battle and if i press t oops looks like there are some issues in the code so first issue is we want to move the pokemon up towards the pokeball right so the target position should be original position plus 50. okay not minus 50. the next issue is if you look at the original position you can see that it's actually the local position of the image so since we're dealing with local position we need to use do local move i instead of do move i okay so let's test this now so if i start a battle and press d okay our animations are working correctly i can only see one problem i want the pokeball to drop at the center of the platform so while dropping the pokeball i'll set the target position to current my position minus 1.3 okay so this should be better and yes that looks much better so now we are done with the animations so next we'll use an algorithm to determine if the pokemon is caught or if it'll break out of the pokeball so we'll use this formula from gen 3 and 4 to check if a pokemon is caught so here first we'll calculate a value a based on pokemon's current hb its catch rate status condition and all that okay and then we'll use the value of a to calculate a new value b and finally the value of b is used to determine how many times the pokeball should shake so let's call it shake count and the shake count will be between 0 and 4 and if the shake count is equal to 4 then the pokemon will be caught and otherwise the ball will shake that many number of times and the pokemon will break out okay so let's actually implement this formula inside a function so inside my battle system script i'll create a new function called try to catch pokemon and this function will take a pokemon okay so note that this function is returning an integer so basically it's returning the shake count so inside this function first let's calculate the value of a so this is the equation to calculate a and here you can see that we don't have some of the values that are required in this equation so right now we don't have the catch rate of the pokemon and we don't have status bonus so first let's add the catch rate so every pokemon should have a catch rate which determines how easy or hard it is to catch that pokemon okay so i'll add catch rate to my pokemon base class so i'll create an integer called catch rate so its value is going to be between 0 and 255 so if it's 255 then it means it will be easy to catch that pokemon and if it's a rare pokemon that's hard to catch then it'll have a low catch rate you can find the catch rate of other pokemon from bulbapedia so here i'll just give a default value of 255 to the catch rate and then i'll create a property to expose it so here's a short way to create properties so if the property only needs the getter then we don't have to write it in this format instead we can simply write it like this so it's much shorter right so now in the battle system for the catch rate i can say pokemon dot base dot catch rate okay so next we need the status bonus so if the pokemon has a status condition like sleep poison or paralyzed it will be easy to catch the pokemon okay so in bulbapedia you can see that the status bonus is two for sleep and freeze and it's 1.5 for paralyze poison and burn so what i'll do is inside my conditions db i'll create a public static function call get status bonus and this function will return a float and take the condition as the parameter okay so if the condition is null then the status bonus will be one otherwise if condition is equal to sleep or if condition is equal to freeze then the status bonus will be two um finally if condition is paralyzed poison or burn then the condition will be 1.5 and if none of this is true then we'll simply return one okay so that's how we'll find the status bonus so let's call that from the battle system so i'll call condition db dot get status bonus and for the condition i'll pass pokemon dot status okay so we are done with the calculation of a so if a is greater than or equal to 255 then it means the pokemon will be caught so we can return 4 so remember if the shake count is 4 it means pokemon will be caught okay so if it's not greater than 255 then we have to calculate another value b so this is the formula for calculating b and finally we can use the value of b to find the number of shake count so initially i'll set the shake count to zero and to calculate the shake count i'll run a loop for four times okay and inside the loop what we'll do is we'll calculate a random number between zero and sixty five thousand five 535 okay and if this random number is greater than or equal to b then we'll break the loop okay and otherwise we'll update the shake count and continue the loop so if the first time we run this loop if the random number is greater than b then we'll break it immediately and the value of shake on will be zero if it's the second time then shakedown will be one and if it's third time it'll be two and so on okay so this is how we find the shake count from the value of b so now we can just return the value of shake on all right so let's actually call this function from here okay and for pokemon i'll pass the enemy pokemon so now instead of shaking the pokeball three times we can shake it according to the number of shake count okay but the value of shake count can be four but at max we only want to shake it for three times so what i'll do here is i'll say math if dot min of shake count comma 3 so what this will do is if shake account is lesser than or equal to 3 then it will use the shake count itself but if the shake count is 4 then it'll only shake for 3 times okay so next if the shake count is equal to 4 then it means the pokemon is caught and otherwise it means pokemon broke out so if the pokemon is caught then we can show a dialogue like the pokemon was caught and then we can slowly fade the book bar so i'm doing it in 0.5 seconds and then we can just destroy the pokeball game object and then we can end the battle by calling battle over so next if the pokemon broke out then i want to play the breakout animation but before doing that i actually want to wait for one second so i'll call wait for seconds and i'll wait for one second and then i'll just fade the pokeball to zero very quickly so i'll do that in 0.2 seconds and at the same time it's fading i want to play breakout animation so the breakout animation will be the opposite of the capture animation so i'll just copy this and rename this to play breakout animation and here i want to fade it to 1 which means i'll make the image visible then i want to move the pokemon back to its original position so let me remove this 50 and finally i want to change its scale back to 1. okay so pokemon will break out of the pokeball and return to its normal position so let's call this okay and i want to wait until it's complete so here for the do fade you can see that i'm not waiting that's because i want both of this to happen at the same time we can also achieve this by using sequence but in this case i think not waiting is the better solution so after playing the animation i can show a dialogue like the pokemon broke free actually i can show different dialogue based on the shake count so if the shake count is less than two then i'll show this dialogue and otherwise i'll just say something like almost got it so yeah you can have a different dialogue for each shake count so after showing the dialogue i can just destroy the pokeball and in this case we don't want to end the battle instead we should continue the battle right so what i'll do is i'll set the state back to running turn in order to continue the battle so we are almost done with the catching mechanic but once the pokemon is caught we should add the pokemon to our party right so what i'll do is in the pokemon party script i'll create another function called add pokemon so this will take a pokemon as a parameter and we simply have to add it to the list of pokemons in the party right but i only want to add it if the current party has less than six pokemons okay so in this case i'll add a new pokemon to the party okay so let me actually rename this parameter to new pokemon because that makes more sense and if we already have more than six pokemons in our party then we should transfer this new pokemon to our pc right but we haven't implemented pc yet so for now let me just start a to-do and we can do this once we implement pc so now let's actually call this function from our battle system script okay so once the pokemon is caught i'll add the pokemon to the player party okay and then also show a dialogue saying the pokemon has been added to your party so next i want to handle an edge case so we should not allow the user to cache the pokemon if it's a trainer battle right so at the start of the function i'll check if it's a trainer battle then i'll show a dialogue like you can't steal the trainer's pokemon and then i'll set the state back to running turn in order to continue the battle and finally i'll just use yield break in order to break out of the protein okay so we are done with the implementation of catching mechanic so we are actually calling this function when the user presses the t button right but instead of this what i'll do is i'll throw the pokeball when the user selects the back option okay so when the back option is selected i'll call the run turn score routine and for the action i'll pass battle action dot use item okay so you have to put this inside start corrupting so yeah just like we do for move we are calling run turns and passing use item as the action so let's go inside the run turn and here we are handling the move and such pokemon action so here i'll say else if player action is equal to battle action dot use item then we'll just call the throw poke ball corotine okay and i also want to disable the action selector when we are using an item so let me do that okay so that's it so now we should try to catch the pokemon when we use the bag action okay so let's test that so let me start a battle real quick all right and if i choose the bag action you can see that we'll throw a pokeball and well we actually got the pg you can see that pg has been added to our party so if we start another battle oops we are getting this weird bug so there are some more issues that we need to fix so the first issue is while playing the capture animation we are setting the scale of the enemy pokemon to 0.3 right so that's why it was very small when we tested so in our setup function we should reset the scale back to one so now when starting a new battle the scale of the enemy unit will be reset so there is also another issue that we need to solve so inside of a game controller and starting a battle we are randomly selecting a bile pokemon from our map area right so the problem is if we directly pass the reference of this pokemon to the battle then when we catch the pokemon we will have the pokemon inside the map area in our party right the same pokemon so what we have to do is instead of directly passing the while pokemon we have to create a copy of it and then pass that all right so how can we make a copy of an object so for that what i'll do is in our pokemon class i'll create a constructor which we can use to create a new instance of the pokemon okay so basically we can use this to create a copy of the wild pokemon so to the constructor we have to pass the base and the level so let me do that and in here i'll assign it to our private variables okay so this should be pokemon base and not pokemon so yeah the error is gone so finally i'll call the init function from the constructor to initialize the moves and stats and all that okay so let me call in it and now in the game controller script when starting a battle first we'll create a copy of the wild pokemon instead of passing the class directly so we can do that by using the constructor and i'll pass the base and level of the while pokemon okay so instead of passing the white pokemon directly i'll pass the copy so now the pokemon in the battle will actually be a copy of the one in the map area right so even if we cast the pokemon the one in the map area will be different so let's test this now and see if they choose assault okay so let me throw a pokeball and yeah you can see this time the pidgey broke out oops looks like we have another so it's in line 121 so it's because that the status change is q it's not initialized so let me see so yeah we are initializing the status changes q by defining it itself so the problem is when we create a new copy it is not initialized so instead what we can do is we can actually initialize it inside the init function i don't know why i initialized it from there so let me initialize it from here okay so let's test it and this time hopefully we won't have any issues okay so if i throw a book bar all right the pidgey was caught and you can see that it's added to our party so let's start another battle and yeah this time we don't have any issues the wild pokemon is spawned correctly but now if we look at our party you can see that we also have a pg that we just got okay so let's try throwing the pokeball again to test the breakout condition okay so the pg probe free and it used attack so yeah that's working fine so if we try to reduce pidgey's health then we'll have more chance of catching it okay so the health is much lower now and now if i try to use a pokeball again hopefully it should be caught alright so awesome we have implemented the pokemon catching mechanic so i'll stop the video here if you think this video is helpful please leave a like and consider subscribing to my channel and also if you can afford it you can also support the series by becoming a patreon so thanks a lot for watching and i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 3,419
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, pokemon catching unity tutorial, pokemon catching unity
Id: _CTKjN1lN4w
Channel Id: undefined
Length: 42min 10sec (2530 seconds)
Published: Sun Dec 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.