COMPLETE COURSE - Create a Unity RPG in 4 HOURS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone how are you welcome to the unity RPG tutorial in this Atari we're going to learn how to do three different moments in an RPG game first of all we have a very simple title scene where we're going to learn how to work with the user interface we're going to have a town where we can use our player to navigate around this town and it collides properly with trees and then later we can implement more towns with the usage of the tile too and we also can enter battles where we have our player party with the major in a warrior we can attack these snakes here do some damage okay and basically destroy them collect the reward and go back to the town it's a simple game when you quickly talk about it but we're going to cover lots of pass pects and they're going to be very interesting topics for your game development career my name is glocal pedis I've been working with games since 2010 and I've worked with lots of technologies like unity game maker unreal I've made lots of html5 games so I'm going to share that experience with you and divide this lesson into multiple topics so we can talk about things one by one at the right pace and at the right time okay so I hope excited to start this in I see you soon all right guys so let's start by setting up this project first of all we need to make a unity project so if you don't have unity downloaded yet you should install it now I can't just select the four options if you want to generate notes for Android iOS or WebGL targets you might want to check them as well when you're installing unity but anyway to start it is very simple you just have to open unity click on this new button to start a new project and let's fill a few things in the project name we're going to name this unity RPG and that's for the location I like to teach my students to make it to have a good practice of creating a projects folder under their home folder it's much better than just throwing your project in the documents or downloads folder because things tend to be messy on the other hand if you have a projects folder you're going to make sure that all the games that you create are going to be here as for how this game is going to be rendered we're going to select to the two-dimensional mode this is just to help you by setting a few things you could start with a 3d project and converted to 2d normally there will be no problems with this but if we select 2d we're going to save time and have lots of things already set for us after doing this we don't need to add any external asset packages and we're going to create project so the goal of this video is to talk about what things were going to use in this project okay and what external tools are going to be used what assets are going to be used so we're going to make sure that we already know everything that we need ok so here we have an empty project but we have a pack of assets that can be used these assets are in a zip file that should be packed with this course ok so I'm going to have this file already here I have this art zip I'm simply going to copy it my right-clicking and choosing copy and in the project well since we are making a unity project this assets folder is located somewhere in your computer so quickly access it you just have to right-click and choose reveal in finder ok so we have this assets folder right now it's empty but I want to make a new folder inside I'm going to right-click choose new folder and I'm going to rename this folder as project you're going to list in Wyson inside the project folder I'm going to paste the zip file with the art assets I'm going to double click it to expand it ok so you have these files here and I'm going to get these folders inside the project folder then we can get rid of these art files ok so move to thrash so this is how it looks assets folder project folder and then we have fonts maps and sprites so if we go to unity now and with a little bit for unity to process all of these files because they have to convert to the proper format this is what we have in our project folder ok so you know some people might ask themselves when they're making their first projects why do we need a project folder if we are making a project ok it should be clear that everything that we are doing here is related to again the thing is when you add external packages when you add external tools they're going to add their own files and their own folders in the root of the assets folder so if you just put all of our folders in the assets one if you put fonts Maps sprites scripts and animations there is a there there is a risk of you confusing what folders belong to your game and what folders are actually they actually belong to third party companies for example okay so it's much better to do it like this because we're going to make sure that everything that we create is going to be in this project folder okay so we have fonts maps and sprites since this is a two-dimensional game there are a few things we have to care about if you go to the sprites folder you're going to see that we have battled hi-oh set and world let's take a look at world right now okay we have a few images here a bad dragons neigh villager so there are some some interesting images here but one of them is a bit different than the others if you look at player okay I'm going if you select player and I look in the inspector and increase this a little bit you're going to see that this is a sprite sheet okay so it's a collection of sprites you can see the player moving down up left right you can even see the player throwing some punches here but the thing is you can't use this image exactly as it is right now if you were to put this image here in the scene you would see the entire sprite sheet but that's not what we want whenever we're dealing with sprite sheets with sprite animations that look like this we want to slice this image in smaller images okay for example maybe we just want the image of the player looking like this maybe we just want the image of the play the player throwing a punch to the right for example so how do we do this thankfully game development has evolved a lot in the past few years and this is super simple with unity we're going to select the player image so it's going to be loaded in the inspector window we're going to press actually before that we're going to look at the sprite mode if you look at this pride mode it says single so it's just one sprite just one single image we're going to click here and choose multiple okay when we do this we scroll down a little bit and press apply so the information that we're going to do with multiple images is going to be saved and now we go to the sprite editor and with the sprite open here we want to slice it okay so if you click on slice you're going to see that there are multiple types of slicing okay we have our automatic grid my cell size and grid by cell count this is the easiest one of them grid by cell count something to click here and you're going to see that we need to set a few things we need to set the amount of columns and amount of rows so we basically count we have 4 columns and we have 7 rows so 4 columns 7 rows and then we hit slice it's going to be a bit difficult actually yeah we can see that you're going to see that there are these green squares now these are the slices that we were able to make in this player okay so with that set here we can press apply to save all of the sprites we can close this and if you look at the project folder now you have the player here and if you click on this arrow you're going to see all of the slices that we made ok all of the images that belong to the player are now here so you can drag them individually and put them in the game ok so this is basically what we need to put these sprites we have to do something Lices in other places here as well so just so we don't do that on multiple lessons we can do everything that's going to be needed right now so we're going to the battle folder okay so let's check this price that we have here we want to deal with these icons we're going to use these in the battle scene so I'm going to select this icons image sprite mode multiple apply and then we sprite editor we need to count we have 10 columns and four rows so we're going to slice this is going to be 10 columns and four rows slice and then we have the squares properly set here once we apply all the sprites are going to be saved and if you expand the economy to see that every icon that we have here has been separated so you see it's a good system it's much better to do this because you don't have to include hundreds of images you just have a sprite sheet and you just slice it and use it inside unity okay we're going to need to use the mage as well so sprite mode multiple we can even press sprite editor and press apply here so we have five columns and four rows five columns four rows slice we're going to apply so every time you do this you want you might want to check if it looks good okay and apparently it looks okay we're also going to need the snake so we said it it's multiple sprite editor apply before we add it then we have five by four again slice five by four we press slice and then we press apply okay and finally we just need the warrior now so select the warrior this is five before once again so before that by the way we put it on multiple sprite editor apply okay slice five by four great we press the slice button and everything should be good now press apply and there we go alright so this this is all the setup that we need to to our sprites you notice that there there are other sprites here we have a slime we have a reptile we have multiple enemies here we have mats and dinos but since we're not going to use them in this tutorial we're going to leave it to you to edit these Prydz and use them later you're going to see that it's going to be very easy to integrate new enemies in this project okay so this is what we need to use these assets and by the way these assets they belong to this altar here so it was made by pixel boy and it belongs to this super powers asset packs you don't have to worry about using these assets and worry about a copyright or things like that because all of these assets are released under this license Creative Commons 0 that means it's public domain you can use it freely you can use it on personal projects commercial projects and there are lots of interesting asset packs here so if you are interesting on making other games and using other assets you can just go to this page here okay github calm / sparkling labs / superpowers asset packs okay so good we are we already know everything we need to prepare about our assets but what about this tile set image what exactly is this it's kind of like a sprite sheet but these are tiles you're going to see that there's a small unit here a small square that can be used with all of these assets here okay so we're going to build a map by using tiles we already have a prepared map here but we need to to use this these tiles and if you want to build other maps you can just build them with these images that we have here okay but what exactly do we need to convert this all of these tiles into a map if you look at the maps folder they're going to be two TMX files okay these files are the add are the actual maps but they are text files so how exactly are we going to tell unity by using tags that we are building a map and at a certain place there's going to be grass and another place there's going to be a fountain how exactly do we do this first of all we need a tool called tile it is a fantastic - it's a classical tool and it works very very great okay so you just have to go to this page map editor org and then you download the proper version for this project okay and remember to support these tools if you want if you are able to because they are just great and they're free to download ok so it's very important for us to support all of these authors ok so you have to download this tool and store on your computer and once you do that you're going to have this project here ok we're going to have tiled so if you want to open this once you have tiled install you just double click on town for example and it's going to open like this this is the map that was already built ok for this specific tutorial so you can use it in this project ok so this is what we have for tiled but also we need another tool this is just a tiled project but if we want to use that project in unity Rivington needs tiled to unity okay once again it's a free tool we can use it as we want but it's very important for us to support the authors so go to this page Shaun va.com /tao to unity and you're going to be able to download this tool okay made by Shaun Barton so another great tool it's a very straightforward to use but there are some things we need to be careful about here okay so we have these maps in unity we can just drag the map and drop them in the hierarchy like we do with images or in this scene for example we need to have the tool installed in your computer and you also need the two files in unity so we're going to open tile to unity this is the Mac version but there are Windows versions as well and at the top of this tool we're going to click on help and press import unity package to project once we do this this window is going to open in unity it's a regular import package tool okay we're going to have multiple folders here it doesn't really matter what exactly we have here but once we press import all of the files are going to be extracted and added to your project so once this compilation is done you can look at the assets folder and you're now going to have two folders you're going to have project and then tile to unity that's why it's important to have this project folder here okay so there we go assets folder we have project and tile to unity and notice again that how to unity has its own file Syria materials meshes prefab scripts it has its own thing to make it work completely okay so all right so how do we integrate these Maps here if you go to tile to unity you are able to select the TMX file and then tell where to import it to so here in how to unity I'm going to file open tile file then I'm going to projects we're going to unity RPG assets project it's our own folder Maps and then we're going to select town we're going to press open with tau selected it's going to be properly loaded but we're not done yet we also have to tell where are the exported files going to so we need to click on this export to file ok and once again Project unity RPG assets this time we're going to select title to unity and then we choose this txt file tiled unity export dot txt so I press open make sure that is going to be properly loaded here and once we do this if you want to make sure your tile map has been read correctly just press preview map so you're going to see this image okay you're going to notice that these tiles they also have squares around them these are collision areas that have been already built in your map ok if you want to change it because you're making other maps you have to select the tile set which is this one already loaded you have to click here in this tool to edit these tiles you have to make sure that you have the tile collision editor active here so we just click on this button and every time you choose a tile for example this one you're able to draw the collision area you just click and drag around ok and I think you should use this two here yeah insert rectangle if you want to add another collision area you can just click and drag and it's going to be added ok notice that some of these tiles they don't have collision areas like this one here this one doesn't have either but it's because they're not going to be used in this tutorial but if you want to improve if you want to use other tiles and if you want the player to collide with them you have to use this editor here okay but we're not going to edit anything here no need to save I'm just showing that to you okay so we go to preview map we can see that the map is properly loaded here everything's looking fine we can see these collision areas once we're doing this we have to click on big ass export button ok you just click here and if you go to unity you're going to see that it processed a few files but now if you go to tau 2 unity folder and you look at prefabs we have a town ok so if I drag and drop this into the hierarchy we have our entire town here available to be used and you zoom in you can see that there are some green rectangles on green lines but these are just determining the collision areas okay that means that if we put a player here we're going to be able to collide with this map okay but we're going to delete it for now okay the good thing about this lesson is that basically we have everything set up for us art wise and too wise okay we have all the necessary tools necessary files so we can go into the next lesson and dive straight into coding okay we need to make three different scenes in this game so in the next lesson we're going to work on the menu scene right so I see you soon with everything set we need to make our title scene this is going to be a basic menu where the player is going to be able to press the play button and go to the town to find enemies in adventure into the unknown okay so first of all we need to use a canvas if this is going to be your first time working with unity 2d there are going to be some new concepts that we have to introduce but they are actually very simple we're going to our hierarchy tab here I'm going to right click choose UI and then canvas to add the canvas the canvas is the the object is the thing that is used for drawing interface to the users okay so inside the canvas were able to quickly add images we can add panels we can add buttons and several other elements okay so whenever you think about interfaces you have to think about canvases okay and the interesting thing about canvases is you might have lots of elements here in this game but if you have a canvas in general everything that's going to be here is going to be drawn on top of the gained camera okay it happens like this because the HUD okay on the interface elements that when you you make they need to be on top of the the game okay the player needs to be able to see the buttons to see anchors to see things that they can interact with so this canvas here is going to basically show the background image so we're going to select it good an inspector and rename it to back around campus this is the first step now for the render mode we have your screen space overlay but we're going to use screen space camera okay and once we do this we have to specify what is going to be the camera of the canvas it's a different way of using the canvas but it's going to to make everything easy for us okay so if you do this a new field has appeared we have this rendered camera here we're just going to select the main camera of the hierarchy and drag and drop here okay so the background canvas is kind of going to adjust with the main camera okay now once we do this we also have to care about another thing we have to take a look at the canvas scaler this is what's going to be used to determine how your canvas is going to resize depending on the screen that you are using it could be a constant pixel size but it's better that it scales depending on the resolution of your device depending on the aspect ratio so what we should do here is click here and select scale with screen size we need a reference resolution it's going to be the basis of the resolution that is going to be used so for X we're going to use 1280 and for Y 960 okay and as for the other options they can be left like this this is basically what you need to set up the canvas okay and once we have this we just need to add a background so here inside the background canvas we're going to right click Choose UI in the image and here we can add the background image however there are two multiple ways to make this canvas the this background image okay this is the ease of them the easiest of them you just add an image and well basically that's it but if you want you can build the interface element from the ground up we can just right click and choose create empty to add an empty game object let's rename this to background image and you can add the image component here as well you just click on the add component search for image click here and you're going to have an image the same way as for the background image we are going to our project folder right here then sprites then we're going to see yes not world its battle yes and inside battle we have this background image okay so what you do is drag this image drop it here it's a bit difficult to see but it's because it's pixel size hasn't been set properly so automatically resize this image you can just go to an inspector and press set native size okay so this is how the background is looking like it's fitting the entire screen okay you can see it very well here and this is where we're going to make our menus work okay so this is what we need for the image but now we need another canvas and that canvas is basically going to be holding the title and it's also going to hold the button so let's make another one right click UI canvas we're going to rename this canvas to HUD canvas HUD canvas okay and you're going to start by adding an image actually a title and even before that we also have to change how the canvas is going to be rendered it has to be just like the background canvas so render mode is screen space camera the camera is the main camera that comes bundled with the scene so just drag and drop here once again we change the canvas cable size so scaling screen size 1280 960 and that's it now to add a title a title text we right click on HUD canvas UI text once we do this if you double click on the text you're going to see that you can barely see the text okay so you should use this tool here for 2d two-dimensional editing if you click you can see things just like when you're adding when you're editing images you can resize them by just dragging these blue knobs like this okay and well you're going to be able to see text in a much better way the thing is because of how we have these two canvases we have a background canvas and we have a hot canvas we still can't see the text so I could write a big text line but well I still can't see this text okay if you were to drag this element to the right then you can see the text so we have a few issues it's too small and the biggest issue of them is that this text is being rendered below the background canvas so why this is happening if you select the background canvas and you look at the canvas you're going to see that the sorting layer has been sent to the phone and the order is zero and if you go to the HUD canvas it's the same thing we have a default sorting layer and ordering layer is zero so these two they conflict you can't guarantee that the HUD canvas is going to be rendered on top of the background canvas one way is to simply change the order in layer of this HUD canvas to one and you can see the text here or you can make another sorting layer you can just click here on the phone select add sorting layer and here in sorting layers in this inspector we can press the plus sign and add a HUD layer once we do this we select the HUD canvas again and change the sorting layer to HUD okay so since the HUD is below the full one in the sorting layers list this means that HUD is rendered on the top it's a priority over to the full layer and as you add other layers you can change their orders and their priorities okay now that we can see this text let's increase the size of this if you click on any of these blue knobs and you hold the alt or the option button you can resize at the same amount horizontally and vertically okay the text that we want to add here is unity RPG of course we still can't see anything so let's increase this font size to a hundred and thirty so this is starting to looking much better to look much better this font a suit of too simple so we can change the font here in area by clicking on this circle and choosing this one Kel's and one interesting thing here is you notice that this font is too big so we just see unity we don't see RPG because the text couldn't fit so we can increase this a little bit more and we can see unity RPG good as for the alignment let's choose center horizontally and Center vertically and also move this a little bit to the top okay so we're going to have our title here it doesn't do anything special here so what we want to add the other thing that we want to add here is apply button okay of course we if we have just this this is basically nothing so we need to go to the HUD canvas right click uy and then button okay you're going to see to the button is too small so we're going to resize it the same way we click on a blue knob hold alt or option to increase the size of the button so it looks like this we're going to move it now a little bit and we're going to choose this text here increase its font size the text is going to be play in we also going to change the font from Arial to Kel's okay so this is the button that we have and if you want to test this scene to see if you can interact with this button you can just press play okay if you do that and once the game properly loads its going to the game window and you can click on the button you can see that you even have you even have this feedback of clicking and shading the button like this okay so if you want to change the color of the button by the way you can just go to the button and change the color here in the image let's make it a bit gray okay so good however we are we're close to finishing this title scene okay but we need this button to do something in what we needed to do is to change things okay right now we are in the title scene so we want to save this okay so it's going to be used later so we go to file save scene I'm going to name this title and press save and in the assets folder I want to move this title scene to a scenes folder so let's make one in the project folder we're going to right click create folder this is going to be named scenes and back in the assets I'm going to move this tile scene file to the scenes folder okay good this is the first step whenever we click on the play button we are going to load another window we're going to to to load basically the town scene but when we do this we need to make sure that the scene that we are going to load is going to be in a list this list can be accessed by going to file build settings and there's the scenes in build list you need to go on everything that you want to add and press add open scenes so it's going to be added in this list this means that this scene can be dynamically loaded in your game okay so now that we have this title scene added here we want to make a new empty scene okay something very simple I'm just go to file new scene I'm going to file save scene I'm reading in this town it's just something empty move this town scene to the scenes folder and once we do this file build settings and I'm going to click on add open scenes just to make sure town is going to be here okay good now back to the title how do we make this play button to load that scene so far I've been using things that we're already provided by unity so textures images buttons as well but we don't have anything super specific to do this to load another scene so we need to make a very short script for this I'm going to the project folder we're going to create a new folder here and name this scripts okay we're going to double click this now right-click create c-sharp script and I'm going to name this change scene ok it's going to be a simple script like that once we double click this it's going to open in monodevelop which already comes bundled with unity ok it's free it's open source so it's going to work just fine for us ok wait a few seconds and and there we go we should have our script here and once we do this once we have this change scene class open here we should be able to add a new method here I think just zoom in a little bit there we go so we're not going to use the start and update methods even though they're used on plenty of different classes and components we need to make our own method here so we're going to make public void load scene and it's going to require one parameter which is going to be string name the parameter is the exact name of the scene that we want to load and to load however we need to include the namespace here we can't change things with the full script like this one so here at the top we need to add using unity engine dot scene management this contains all of the classes methods and utils that we might want to use to change the scene so now that we have this in the load scene method we're simply going to type scene manager dot load scene in between parentheses name okay so you might be thinking okay we are making a method like this but how exactly are we going to call this load scene method with a specific name well the trick here is that we're making this method public so whenever you do this okay and whenever you you do you see realize other things they can be accessed by the unity editor so what we want to do is we're going to select our button game object let's collapse a few components here to make this easier to read I'm going to wear this change scene script here okay so I'm just going to drag and drop so we have this script and now in the button you're going to notice that there is a non click area on click panel this with this area here we are able to list multiple methods that can be called whenever this button is clicked without having to write any complicated logic so here in the unclick we press the plus button we need to select a target object which is going to be the button itself so a drag and drop here and now we're going to determine what is going to be the callback so we're going to click here we're going to change Sene we select the load scene method that we just determined and you're going to see that there's a feud for us to pass the parameter the parameter here is going to be town so that means that whenever this button is clicked this method is going to be clicked with the town parameter and since this town is a string it's going to be passed as a parameter here and we should be able to load our town scene ok so just by doing that let's save that's also here in the scripts folder right-click choose create then folder let's name this title so all spirits of the titles in are going to be here I mean to put this change in here just to keep things a little bit more organized and now save your scene we're going to press play ok and with the game loaded whenever I press the play button we should load the town scene it's empty right now but that's X that's and just as we expect it ok it's going to work like this for now while we don't implement other things okay so this is it this is basically the first step it's a quick exercise on how to work with the user interface how to make textures and buttons and work with canvases and with this ready we should move on to the next lesson where we're going to look a little bit more on how the RPG game is going to work ok we're going to work on the player party all right so even though we are in the title scene we should care about how the player party is going to work to make a player party work we have to remember that all of their health points or of their mana experience coins all of these things they have to persist over scenes there are multiple ways to do this but we're going to aim on a simple solution okay so right here in the title scene we're going to make a new game object so we right click here choose create empty we're going to select this game object here and rename it to player hearty we also want to make sure that this is going to be in the position 0 0 0 okay just for the sake of keeping things in their proper places okay so he'll play a party here it's invisible there is nothing assigned to it so we need to make sure that this player part is going to behave differently depending on the scene they're currently at okay so if we are in a title scene then this player party is not exactly going to be used okay we need to use it on other places we need to use this player party when we are in the battle scene for example so we're going to be able to see our players the mage and denied okay so to do this we're going to need an auxiliary script for us in the scripts folder we're going to right-click choose create c-sharp script and we're going to name this start battle okay so with this script loaded here we're going to wait for it to compile so it's going to be properly processed then I'm going to grab this file and drop here in the player party and finally we're going to double click it to make sure that this is going to be loaded okay so how does this go into work in the start method we need to do something very specific here we're going to type we're going to call the method don't destroy on load okay and the target here is going to be the own game object the player party game object and you can even see here in the summer that it says makes the object target not to be destroyed automatically when loading a new scene whenever you load a new scene everything you have in your previous scene is going to be destroyed it's going to be deleted from the internal memory and in a way it's not going to well occupy resources on your mobile phone so it needs to do this to clean things up but for some specific cases we don't want this to happen so we use this don't destroy load method and we type game object okay this game object here is a reference to the game object that contains this taught batter component so the start batter component is in the player party it means the game object is going to be the player party so this means the player party is not going to be destroyed okay however whenever this the scene is load whenever the scene where the player party is we need to do something here we need to make sure that we are going to activate or deactivate this player party so we're going to need to be able to read what is our current scene so we once again type using Unity engine dot scene management like this okay and now we need to do something here I'm going to get rid of this update method call and what we want to do here is type C manager dot scene loaded + equals on scene loaded okay so what did I just do here when we type scene manager we are of course accessing the instance of the same manager keep the thing that's managing the scenes this same loaded here is an event so the scene manager is going to call this event is going to raise this event whenever the scene is completely loaded it is done like this because that way multiple scripts they can sign to this event here so whenever it is reason we can do something it's like we are making the start battle to react whenever a single loaded event is reason so whenever a new scene has been loaded and we're going to declare our own on sing loaded method here so I'm going to type private void on scene loaded and it needs exactly two parameters the first one is scene and I'm going to name it scene okay so it's a the parameter is called scene with the lowercase s and it's type is seen with an uppercase s and we also use load sing load mode so this is the method that we're going to use it needs to be this exact same signature because if you hover your mouse on sing loaded you're going to see that this event the the delegate of this event needs a scene and a load scene mode okay these two arguments here good so now that we have this we have to check what scene has been loaded so I'm going to type if scene dot name and we're going to compare if this name if this string okay if this scene is title that's why we do this comparison here we compare the name with the string title and you need to make sure that this string is exactly the same name as this scene here title okay if you used another name then you have to make sure you use the proper name here okay so if this happens if we are in the title scene we no longer need to listen for this event so we can type same manager dot scene loaded - equals on sing loaded so this is not going to happen again and I'm going to destroy the player party so destroy and then game object just going to destroy itself okay however else so we do the else block if the name is not title we want to activate this player party if the new scene we are is called battle so to do this in a single line you can just type game object dot said active and this method is basically to activate or deactivate a game object and inside here we have to pass true or false because this is it requires a boolean value so we can just ask here for a comparison we can just type seen not name equals to battle so if this if the scene we are currently in is battle this is going to be true so the player party is going to be activated it's exactly where we want it to be however if this is false it's not going to be activated it happens in the town scene okay so we leave it like this and finally whenever we start whenever we have this start battle object called here I'm going to type game object dot set active false so the player party is going to start deactivated so if you save this and go to unity you're going to be able to test this out okay so let's wait for the code to compile and after this is done pay attention to how the player party is right now it's it's normal okay we have this game object here it contains the start battle script but if we press play notice how the hierarchy is going to change we have this don't destroy unload and player party is now gray okay this means is deactivated if you select the player party and you look at the inspector you're going to see that it's unmarked okay if it was like this it would be activated but it looks like this it's deactivated okay so let's move on and improve this player party a little bit more a player party of course is going to need multiple players multiple characters that are going to help you playing this game and attacking enemies destroying them maybe healing your teammates so what we're going to do here is we're going to need player units okay so first of all here in player party I'm going to right-click choose create empty and rename this as player in unit just for now okay so this unit is going to require two components first of all we're going to use an image so I'm going to press add component select image and I'm going to give it a look okay so here in source image I'm going to select this circle and we're going to search for mage okay and we can use any of the sprites of the mage that we have cut so I'm going to use this one okay the single is mage and also set the native size notice however that you still can't see this player in unit okay it's invisible for now so we have to do a little adjustment to make this work the thing is the way we have been using images so far is images are components that work on canvases okay so if you were to select this player unit and put it a HUD canvas you would see that it's going to be back to normal okay you can see the image now because the image component only works inside canvases however the player unit is going to work as a normal game element okay it's something that's going to be outside of a canvas whenever we have the background canvas we have the HUD canvas but the player party is not an interface element it's not a HUD canvas it's not a background canvas it's just an empty game object in the hierarchy here so player units they don't use images like this okay we don't need this image component and we're not going to use a canvas renderer what we're going to use is a sprite render it's something that renders an image in the scene and it doesn't require a canvas okay so now that we do this we can search the sprite search from age and we can choose the first one so it's going to be here okay we don't even have to press set native size or change the scale it's already in the correct scale here okay for the camera that were using for the canvases that we have adapted to this camera okay this is basically our mage and I'm going to position it here now the second component that the our mage is going to need besides the sprite renderer is going to be something that we still need to create we're going to make a unit stat script okay this is basically going to save data about the players that we're going to use so here in the scripts folder let's right click create c-sharp script and this is going to be named unit stats okay so we hit enter we wait for this to compile and let's just done we're going to double click and we're going to add a few fields here okay so let's take off start an update for now we're going to type public float health this is going to store the health points it's a floating point number so there can be decimal values like 3.2 1.7 or it could be whole values as well like 3 for 150 and it's public so it can be accessed you can read and write this value and it can be exposed in unity so you can check the values that we have we do the same thing for mana public float mana same thing for attack magic defense and also speed if these are the values that we're going to use for now okay we can already just save this and once this is done we can go back to unity and if you look at the player unit all of these public values are going to be exposed here once you get this script and you drop here in the play unit okay right now everything is set to 0 but we can already set a few values here so for health I'm going to use 100 100 attack 100 magic let's use 10 defense 0 and speed 10 we can change these things later as we make game remember you are control here these are just a few suggestions of values that can be used but once you're done with this course you're going to have to do your own work on tuning the game okay so now that we have this all of these settings adjusted here I'm also going to rename this play unit to mage unit okay so this is our first player this is the first one that is going to help us achieve our goals which is beating the enemies that we have in the scene okay so now if we press the play button okay you're going to see that the scene is going to load the player party is going to be disabled so if you press play it's still going to be disabled it's going to remain like this okay it's going to be grayed out and well we're going to be able to do all of the other things we should do now that we are done with this we're going to move on to another scene okay so we are done with the title for now let's save the scene okay so we have the player party we have the mage unit we have the entire interface now we're going to move on to a different game mode we're going to work with the town scene where we're going to have our tile map and we're also going to be able to - well walk around this map and find some enemies see you in the next lesson okay so now we're going to our town scene the one that we made it has nothing inside it and the first thing that we want to do is to make do well to add the town okay so we're going to be able to see what we exported with the tiled tool so remember we have to go to the tile to unity folder we're going to prefabs where our town is already completely working okay it was built automatically by the tools that we used all the colliders are in place so we're going to be able to navigate here so basically we get this town and drag and drop here in the hierarchy okay you're going to see that the town is a bit too big because of well how the camera is set so we're going to set a few values here first of all we want to scale it by typing 0.0 21 on X and the same thing on Y so it's going to be smaller you see that it matches much better that we have here the positional ex is going to be - 6.67 y is going to be 5 ok so if you look at your main camera ok you can even see the camera preview the town is going to be completely covered by our camera and if you look at the game window you can see the town like this ok so it's fitting properly so that's basically it okay since we already covered everything related to tiled in our first lesson to talk about the tools that we need to use and to prepare ourselves to add all the assets it's basically done okay that's all we need for this town but of course if you press play nothing really interesting is happening right now we just have these assets here by the way it would be interesting if you made your own levels to try this out but we need a player ok the player is going to be a simple object that we can use to navigate around the scene and with the play we're going to be able to trigger some battles ok so let's go ahead and make a very simple player I'm going to right click in the hierarchy choose create empty also make sure you have no objects selected otherwise that game object might end up inside the camera or the town so click on this outside area and then once again create empty let's rename this guy to player we're going to set its position to 0 and 0 ok so it's going to be here you can even move it to another place if you want like this ok and display is going to require a few components first of all we need a sprite render ok when we have this we need to select the sprite so I'm going to click here search for player and we can use the first player sprite ok which is this one ok so now that we have this player sprite we also need to add a rigidbody to it and also notice that we have the same problem that we had with the text and the previous scene of the sorting layer ok the town is in a certain layer which is the full one even though it's not specifically set here okay unity uses of the full sorting layer for basically everything so we might want to add another layer here so inserting layer that sorting layer we click here and let's add a town layer or gain layer let's put it between the full and HUD now we select the player again and we change the sorting layer to game ok so we can see the player it's a bit tiny so if you want to change its size you can kill it on two units so it's going to look like this ok it fits much better and ok that's it for the sprite renderer this also needs a rigid body a rigid body is basically something that adds physics to the game but in the specific cases just for us to verify for collisions remember you have to add rigidbody 2d it's very important that you choose this one whenever you're working with two-dimensional games and you're going to add a component to make sure you have to verify there is a two dimensional version for that component and if that there is make sure you use it it's the case for rigidbody this is the 3d one and I'm going to use the two dimensional one okay so here we're going to change a few things okay to make sure collisions are going to work properly interpolate here is going to be set to interpolate collision detection is going to be continuous and sleeping mode is start awake as it is right now okay one very important change that we have to make here is gravity scale is going to be zero this is not a game that you're going to see by the side it's not a platformer game or a physics game you're looking from the top down so no gravity should be applied here okay you might want to use velocity to move things in the horizontal and vertical axis but you're never going to make this fall so you need to make sure that gravity is set to zero okay and here in constraints we also want to make sure that this object is not going to rotate so we freeze the rotation on C which is the only access that can be used in a two dimensional game okay so freeze rotation interpolate sleeping mode collision detection gravity scale set to zero and the final one and very important one as well is AB component and we use box Collider to the which is this one okay once you've added you're going to see a little square around the player and this square is the invisible area that is going to be used to collide with things like this tree for example remember if you click on this prefab made by the tile to unity tool you see that we already see this green rectangle here so these two rectangles they can collide with each other and if the player tries to move to the right too much it's going to bump here okay it's not going to be able to move past this so collisions are going to work automatically okay so let me zoom out here okay so we have a very simple player it's we have this collision work but one thing that we have to do here before we make the player move is working with the players animation okay so there are two things we have to do make the player move so write the code for reading the input and moving the player but also change its animation the animation is going to be a bit extense but it's actually a simple concept so let's cover this in the next lesson Unity's animation system makes working with two-dimensional animations and three-dimensional animations as well very easy but we have to understand how it works okay so we need to go to the project folder we're going to make a new folder here it's going to be named animations and inside it we're going to add another folder that's called player to make an animation we need two different things we need one animator and we need multiple animations the actual animations that are going to make this player to move okay so what exactly is an animator if you go to the player here you click Add component and search for animator and add it of course you're going to see this component here the animator is the state machine it's like an internal system that it's engineered to determine what animation is going to be executed the player and the enemies they have multiple different animations so there needs to be a logic to switch between them you could do that through code easily but it's important to understand how animators work because it's better to do things how they were supposed to do in the editor okay the editor is your friend you can easily switch animations you can change how the anime at the speed of how animations are going to be executed here in the editor without having to deal with a lot of code the code is needed just to do a little communication but the actual animation can be done by its entirety here in the unity editor so first of all we need an animator controller so we need to make one here in our player folder we right-click choose create we're going to select animator controller and I'm mentioning this player animator and that should be us okay so we go to the player and I'm going to drag this player animator and drop here okay so this has been bound here so this is going to be used a number two something important that we need to understand the player is going to require eight different animations it sounds like there are a lot of them but it's actually pretty simple we have idle animations and we have walking animations in each of these they're going to work on each of the directions that we have left right up and down so we need idle left idle right idle of an idle down and the same thing for the walking animations so let's make them let's make empty animations right now I'm going to right-click choose create then animation and this is going to be idle left to make this super easy to to create you can duplicate an animation by hitting comment the if you're on a mac or control-d so we have this you can just rename it by hitting Enter on a Mac or f2 on Windows and in this idle right duplicate rename idle up duplicate rename idle down or you can just create one by one we do the same thing for the walk-in animations so duplicate walking left duplicate walking right duplicate walking up and duplicate walking down so all of the eight animation files have been implemented here they have been created actually but we need to make them work okay so first of all if you go to the player here and you see the animator you need to figure out how do we edit animations and the animator itself you're going to notice that I have two windows already added here we have animator and animation but if you don't have them yet you can just go to window and then click on animator to open that one and you can click on animation to open the second one so we have these two here ok so notice that if you go to animation and you have the player selected you notice that you can select anything anything everything is basically greyed out so it's it's like it doesn't understand this player is going to have animations if you go to the animator you're going to see this state machine we have this any state you don't have to worry about this and we have this entry point so before we do anything else here in the player folder I'm going to select all of these animation files here so select all of them or you can just drag one by one I'm going to drag and drop them here ok so we're going to see that this state machine it starts in entry and there's an arrow that's pointing to idle down but let's not worry about this right now not as we are we are kind of telling unity that this animator is going to do with these eight animations so if you go to the animation now it's no longer greyed out because it understands that the animator contains multiple animations inside it so if you go to the animation here you can see this little editor and you can use and if you click here on auto down you can see the eight animations that we made all of them are empty you can just check here in this timeline in the right that there is nothing assign it in here but just by doing this we can already see this editor and work with it to make the animations work okay so how are these animations going to work these are going to be surprised animations okay so we're going to the sprites folder drag into select world we're going to select the player here we expand it and look here so how is the idle animation going to work the idol is basically going to be one single sprite of the player standing still so to make this animation we are going to look at the sprite where the player is looking down we can just drag it and drop here okay so if you have the player selected and you drag that here you're going to see that you have this symbol here it's going to work with the player game object and it's going to change its sprite property okay so this is basically the idle down animation if you go to either left you can do the same thing grab the sprite of the play looking to the left drop it here it's going to understand that it's changing this pride of the player and we do the same thing for idle right drag and drop and idle up and drag and drop okay that's all we need here okay for IO now left right and up now for walking down we have to be careful about this price that we're going to use notice that 0 1 2 & 3 these 4 sprites were used for setting the direction now we have four is the player 1 frame of the player walking down but 5 is walking up so they're not exactly in sequence so if you select things by if you select surprise that are 4 units apart from each other so player 4 player 8 player 12 and player 16 you're going to kind of see an animation working here okay you actually need 4 8 12 ok look at this little square here showing the preview of the animation if I get 4 8 12 8 and I keep doing this little playing with these prides ok if you use these three surprise you can make an animation of the player walking ok first it moves its left arm then it stands still like this and then right arm so this is the walking animation that we want to use we want to use all of these frames here so for walking down I'm going to drag player 4 here player 8 I'm going to make sure it's going to be more or less here in ten frame mark so player eight here now I'm going to drag player 12 here then player eight again here okay so this is one walking cycle and to make sure that the player is not going to instantly move from this sprite for this one I'm going to repeat the last sprite I'm going to grab this and drop here in the timeline okay so we have this guy this one this one in these two okay you can even finding them by selecting these diamonds here and making sure they're in the proper frame okay so then frames this one's on 20 this one is on 30 and this one is on 40 frames okay so this is enough for making the walking animation to work if you want to check it working you can even grab the scene window put it here and in the animation if you press play you're going to see the player doing the walking animation okay that's pretty much what you need to make this work okay let me get the scene back here okay and now we have to do the same thing for the for the other walking animations it takes a little bit of time but it's a very good practice okay so for the walking left we're going to use player six okay so player ten which is here make sure it's on ten we're going to use player of 14 and then player ten again and one more time we repeat player ten okay that's the walking left walking right let's use let me see here we're going to need player seven player eleven here layer 15 and then player 11 twice and try doing the last one by yourself okay and check on the scene two to make it work if you want you can pause the video right now and do player down by yourself actually walking up okay so for walking up we're going to use player five then play your nine we're going to use player 13 and then player nine twice okay good and if you want to check you can grab the scene window put it here and play the animations so walking up looks like this walking right good walking left like this good and walking down okay good and if you look on the idle ones they should be the simplest of them because well there's nothing special going on here okay it's just one single frame it changes the sprite promptly to the one that should work and it's going to well basically work fine and if you want to stop recording if you want to stop editing you can just click on this recording symbol okay and before we move on to the state machines there's also one little thing that we have to adjust okay so we've edited the animations if you get a 2-player folder these files are no longer going to be empty okay we have data recording in them so what we want to do is these walk-in animations all of them they have to loop okay otherwise they would just play once and then stop playing so I'm going to select each of the walking animations walking down left right and up and mark this loop time setting here okay and when we're done with this we need to work with the animator okay so we're going to make the state machine logic for changing the animators okay all of the animations let's work on this in the next lesson all right so let's go to the animator if you select the player here and you go to the animator you're going to see the animator that's working here so animators they have an interesting way to communicate between the cold files so animators they can have parameters so you can just click here on parameters and we can add some things here it's like we were working with variables I'm going to click on this plus sign and I'm going to choose int and this is going to be named Direction X we're going to add another int it's an integer number Direction Y these two values here are going to be used to determine what animation is going to be working so if you don't remember yet if you go to the scene window and you select any game object okay with this transform to select it you're going to see that you have two arrows here we have the red one which is the x value of the horizontal value and we have the green arrow which is the vertical value that's why x increases to the right okay y increases up so if we want the player to move to the right we're going to use a positive value for X we're going to use one on X if you want to make it move up we use one on Y but if we want to move to the left or down we're going to use the negative values since x increases to the right it decreases to the left so if the player should move to the left we use -1 on X and to move down we use -1 on Y okay that's why we use these two values here and now what should we do here now that we have these two things here we need to work on this state machine here we have multiple states we have multiple animations and we have to do transitions between them so we're going to need the two to work with a little bit of a design here ok so what we're going to do is we're going to put idle down here in this place ok and we're going to - let me select this any state here and move far away from this as well let me put any state here okay so we have idle down we now have idle left so I'm going to put idle left here idle right is going to be here and finally idle up we're going to move it up to this position here should be enough ok so you have the four Auto animations in this pattern here but for every idle animation we also have a walking animation and we're going to put these animations close to the idle values so walking down is going to be here close to either down walking left is going to be here close to idle left walking right here close to idle right and this and finally walking up is going to be here on top of idle up okay actually let's make it down I'm going to move out up here and walking up here the order actually doesn't matter we're just doing this in a way that it's going to be easy for us to to develop this okay and I don't we put all of these states here okay remember these are states but if you look at the states they have a setting here in motion these are the animations so the walking left side has the walking last animation either right has the idol right animation so if we go to these states the correct animations are going to be played okay but what we need to do now is we need to make connections between these states okay so we can determine what kinds of transitions need to happen okay so the first thing that we want to do is to change how the transitions are going to work for the idle animations it doesn't matter where you stand okay it doesn't mean if you are on the left on the right top down but every idle animation is going to have in transition to every walking animation okay so we have idle down here if we right-click and make a transition we can point to walking down idle down can point to walking left idle down can point to walking right and I wrote down can point to walking up okay if you select idle down you should see here in the transitions and you should check that you have all of the transitions set correctly either daughter walking down it's walking left it's walking right and walking up so all of the walking animations are here okay we do the same thing for the other ones idle left goes to walking left walking up walking right and walk-in down okay so left up right down same thing for idle walking up walking right walking left and finally walking down and for idle right walking right walking up walking actually this is idle left let me undo this walking left and walking down it could be a bit easy for you to confuse on a few things here that's why I keep telling you to verify if all of the walking animations are here right up left down up right left now and walking on the four ones and here for walk-ins okay so good so yeah we have added all of the transitions between the idle States and the walking States also it's very common when you are walking in a certain direction to stop so if I'm walking to the right and I stop the animation that should be executed is idle right if I'm walking to the left and I stop then it should go to idle left so every walking state should go to its correspondent idle state so walking down goes to idle down walking right goes to either right walking up goes to idle up and finally walking left goes to our left okay so we're nearly done there is just one little thing missing whenever we are walking we can also change directions without stopping we are covering here the transitions whenever we should stop but maybe we are pressing the right button then we release it and immediately press up we should go to the walking up animation so what it means is every walking animation can transition to every other walk in animation a so walking left can transition to walking up to walk in right and so walking down walking up can go to walking left walking right and walking down walking right can go to walking left walking up and walking down and walking down can go to walking left walking right and finally walking up okay so you can review that by going to every walking State and checking it goes to its correspondent out of state and all of the other walking States okay I'll booster open to idle up and all the other states either right on the other states and walking down goes right on down and all of the other walking States okay so all of the transitions have been set here we have lots and lots of transitions but there's one important thing that we have to do on every transition of that okay we have eight animations each of them they have multiple transitions but we have to understand that each of these transitions whenever you click on them you can set conditions okay so what I'm going to do is reduce this little panel here and I'm going to choose each of these transitions that have been said an add a condition for them to work okay so in order for idle down to go to walking down I need to add a condition and that condition is going to be Direction Y okay so we're going to move under vertical axis to walk in down direction Y needs to be lesser than zero okay that's all we need to do we go to this transition to move to walking down it in this direction Y needs to be negative to walk left I need to add a condition and say the direction X is going to be lesser than zero it's just like we talked before we have the X and the y arrows the axis if you want to move to the left it's a negative value on X if you want to move down it's a negative value on Y okay so walking down and walking I've have been set walking right just means Direction X is greater than zero and walking up just means Direction Y is greater than zero it's everything for idle down we do the same thing for all the other states and again it takes some time but it's important to practice this either left goes to walking left when Direction y is lesser than 0 it goes up when Direction y is greater than 0 it goes right when direction x is greater than 0 and it goes down when Direction Y is lesser than 0 now for idle right here is Direction X greater than 0 walking up direction y greater than 0 now left direction X less than 0 walking down Direction Y lesser than 0 we're almost done for idle up walking up we're going to add y greater than 0 to the right X greater than 0 to the left X lesser than 0 and down we need to use Y lesser than 0 okay and now for idle right yes I think we have all transitions set here of the conditions for the idle animations ok good let me check idle down here as well okay so apparently we have all of these set now we also have the walking down animations okay actually all the walking animations let's do it reverse order if we are walking down and we want to transition with walking up this is simply click here we're going to add Direction y greater than 0 okay it's the same thing we do for changing any direction that we are working with walking right X is greater than 0 walking left X is lesser than 0 but for idle down how why why should we stop we should stop in the idle down state if we release all of the keys so that means Direction x equals to 0 and that means the direct show why this one it's going to be equal to 0 because the player has stopped moving no keys are being pressed we should use the 0 here and 0 here and this is basically going to work we do the same thing for all of the other animations okay let's go for walking right it goes to idle right if x and y direction X in Direction Y are both equal to 0 both conditions must satisfy for this to work okay walking left the direction X lesser than 0 woken up Direction Y is greater than 0 and walking down Direction Y is lesser than 0 okay good now for walking up almost done so x and y both equal to 0 of course walking left X is lesser than 0 walking right X is greater than 0 and lock it down Y is lesser walking down yes Y is lesser than 0 and now for walking left we're going to look at idle left here this is once again x and y both equal to 0 to walk up y greater than 0 so after the ride X is greater than 0 and to walk down Y is a lesser than 0 ok so if I'm not wrong I think all walking animations they have their condition set here ok good walking down let's check here yeah so everything seems good now in this animator we have made a huge state machine here actually it's not huge it's just it's a bit complicated when you're dealing with 8 different directions but the concept is really simple ok if you are walking to stop in the auto animation you have to release both keys and in whatever state you are to move to the correspondent left right up or down state you just check for the values set in Direction Y and Direction X ok so the animator has been completely set on the player and with this done we should already be able to work on the script to make the player move and to make the animations to play correctly because right now if you just press play okay if you look at the player and you look at the animator you're going to see that we are here stopped in the idle down state it's not transitioning because we didn't add any logic by pressing the keys to make the player move okay so we're going to work on this logic in the next video okay so the player is going to need a script to make it able to move okay so to do this we're going to the project folder scripts folder in here we're going to right click choose create c-sharp script and name this player movement okay so let's name it like this where movement we're going to double click it to open in mono and also let's remember to attach this script to the player game object just to make sure we don't forget this is going to be executed okay so while your movement goes here inside the player game object good so back to Mono here in the player movement we're going to need two things we're going to need a speed value and we're going to need an animator value but these two things they need to be exposed in unity there are two ways to do this one of them is to simply type public float speed and public animator and weakening this animator since they are public both of these values are going to be exposed in unity so if you go if you save that script and go to unity with just a few seconds they're both going to appear here and you can set both of these values as you see right now it works it's going to be fine but if you want to learn some good practices right now in the beginning of this game you have to remember one thing when these two values are public it means other scripts that access this player movement component are going to be able to change these but we don't want this to happen at runtime we don't want it to happen like this these are values that are shouldn't only be edited when we are dealing with the unity editor so one way to improve this is to make both of these fields private and we can add an attribute here we can just type between these brackets here this serialize field okay so if you hover your mouse here you're going to see that this is a basic attribute and it's going to force unity to serialize a private field okay so it's a private value it can only be changed here if you want of course inside the player movement scope but it can also be changed in unity so we did for a speed and we do it for animator for some reason I have a hard time writing the serialized word but anyway well that's all you need ok so speed and animator are both serializable if you save the script and go to unity and once the code compiles you're going to see that it's going to stay the same way ok it's just going to be looking like this so right now let's already change the speed to 3 and the animator is already inside the player so we can just drag the player and drop it here okay it's going to basically reference itself and reach for the animator component and once we're done with this we can work on the movement logic ok so this is going to be done in the fixed update method so we're not going to use start we're not going to use update we're going to use avoid fixed update this is different from the update because this works with the physics system ok physics are simulated in a fixed time step okay it's kind of independent of if you are playing on a slow on or a fast device it's going to use the same amount of time to calculate all of the physical operations ok so that's why we use this text update ok now what we want to do is to figure out what is well to check if the player should move to the left or to the right so let's work with the horizontal movement like this ok so here we're going to type float new velocity X and we're going to start by setting this to 0 F okay remember it's a floating-point number so we need this F value here okay it could be just zero but let's use the zero F to make sure we understand that we're dealing with these floating-point numbers ok now we want to verify if we are moving to the left or if we are moving to the right there are multiple ways for us to work on this logic but bring aim from the symbols of them I'm simply going to ask if and between parenthesis input dot get key this method is going to return true if you're holding down a key in your keyboard okay so if input get key and I'm going to pass the parameter key code dot left arrow that means we are pressing the left arrow so if this is happening here the player should move to the left the way we're going to to make it move left is we're just going to set new velocity X to be minus speed okay so remember we have this big value set here okay and we have this internal variable that we're going to store the speed value to change it later okay so if left arrow is being pressed new velocity X is going to be minus speed also when we do this we have to update our Automator to make sure our state machine is going to be connected with our script so to do this we type animator dot remember the parameters that we made they are integer numbers so we use sent integer and it needs a name the name of the property that we want to use and the value that is going to be set set integer we're going to use Direction X and the value here is going to be minus 1 because we're moving to the left okay in the same block here that we have I'm going to add else and we have to check if input dot get key we check if that key is key code dot right arrow if this is the case the player should move to the right so it's very similar I'm going to type new velocity X equals 2 speed so the positive value here because we should move to the right and animator dot set integer direction X and the value is 1 okay now we're going to add another else block because that means the player has released both keys if this has happened it means new velocity X actually don't even need to change this new velocity X because it's already set to 0 here you could do that just for clarity okay you can type new velocity x equals to 0 just to make everything following the same pattern and finally animator dot set in for Direction X is going to be zero so this is the final look of this fixed update here okay for the horizontal part the vertical part is very similar so I'm going to add a comment here vertical movement float a new velocity y starts a zero f we're going to check if inputs dot get ki and we check for the key code in that key code let's check for down first so key code down arrow so if this happens new velocity Y should be minus speed an animator dot set integer Direction Y and minus one because we're moving down else if we are pressing the up arrow so key code dot up arrow so if we are trying to move up velocity Y is going to be positive speed and then animator dot set integer direction Y is going to be the positive one so just type one here else if nothing else is matching here new velocity Y is going to be zero F an animator dot set integer direction Y is going to be zero okay so it should kind of go to the Iowa animation if X is zero as well so we did all the logic for horizontal movement and a vertical movement but we still have to update the velocity of our player and to do this we need to access the rigidbody it's one of the reasons we use the rigidbody it already implements the physics of using speed and moving things so you can just type here game object to access the game object that contains the player component the player movement component which is the player itself dot get component of the type rigidbody 2d we type this between the lesser than or greater than signs open and close parentheses as this is a method call dot velocity and that velocity is going to be a vector to value because we're dealing with the two dimensional space new vector2 we use vector tools because we want to set values for X and for y x is new velocity x and y new velocity y ok so that's basically what you need to make the player move okay we have the speed we have the animator we do the logic for the horizontal movement and the logic for the vertical movement so let's see if all of this go to unity let's wait for the code to compile okay so that's read just a little bit and once this is complete if we press the play button let's give it a try let's open the console to check for errors but the player is here and if I try walking to the left we play the left animation right right animation if we stop walking the player plays the idle animation down up okay so the animations are working properly and notice that you're also colliding with the things that have collision boxes added to them okay we can walk to multiple different directions the animations are working the stig machine is working and the player stops moving when it should okay so this is basically what we need for the player movement okay you see when you use tools like tiled when you can draw your own map you can draw your own collision maps you can easily make multiple levels you can make this game huge make it a very big map and it could be easily integrated through code okay it's very important for you to whenever you're starting a project to search for the what tools are available online for you to use and what can make your workflow much easier because it gets as simple as that okay and now that we have this let's go to the project folder in the scripts folder now I'm going to right click Choose create folder I'm going to name this player and I'm going to move the player movement here okay actually instead of player lessoning this town okay I think it's going to be better start battle and unit stats are going to stay outside for now because we're going to make more scripts regarding the battles okay so in the next video we're going to talk about how we can start battles and interact with enemies there are multiple ways to trigger battles in some RPGs you just have to walk around in the middle of the grass and then well there's going to be a random encounter it's like an enemy's going to ambush you and try to destroy destroy your party but in other games in other RPGs you can see a specific icon of that enemy in the scene okay so you clearly see who you're going to try to to to battle and well a battle is going to be triggered in this game we're going to name these things enemies Palmer's so the enemy spawns are going to be little indicators on the map well that if you collide with them you as the player if you touch these enemies partners then we're going to move on to a battle scene and then the battle is going to begin okay it's going to be basically how this is going to work but before we even start by working with the battle we want to make the enemy itself okay just for reference if you go back to the title scene here I also remember to save your town scene just so you don't lose anything you're going to remember that the player party has this mage unit which has its own sprite render its own unit stats well we're going to do the same thing for one enemy which is going to be the snake okay so what we're going to do for this is come here to the hierarchy right click we choose create empty we're going to rename this game object as a snake okay and better than this lessening this snake unit okay name it like this and we're going to add a sprite render to this snake and as for the sprite we're going to search for a snake and we're going to use this one here snake zero okay so you can even see where this link is going to be more or less I'm going to put this one here okay and we also are going to use a unit stat script because it's something we can battle with okay so let's change a few values here let's use 20 for health 10 for mana 15 for attack 5 from magic 5 for defense and finally 10 for speed ok so this is going to be a basic snake unit but the thing is this is going to be transformed into a prefab because we want to dynamically instantiate the snake unit later it's not going to be already present like this just like we have at the mage unit so we're going to the project folder right click create a new folder and name this prefabs and once we're done with this we're going to enter this folder and to transform the snake unit into a prefab we can just drag it from here and drop it here so it's a prefab we can even delete it from the scene right now it's not going to be needed here okay but anyway we already have our snake unit but whenever we're going to spawn things okay whenever we we want to spawn an enemy we're not going to just spawn a snake unit okay we're going to spawn maybe two snakes or maybe three snakes or two snakes into bats we can have different sets of enemies so these are these were going to call enemy encounters okay so here we're going to right-click choose create empty we're going to name this game out to this enemy encounter and I'm going to drag this neck unit prefab and drop here in the enemy encounter okay I'm going to duplicate this neck unit instance and put another one here so we have two snakes and I'm going to get this enemy encounter and transform it into a prefab I'm going to drag and drop here so we can see two snakes here that's rename this to snake encounter by the way but there is one important note here unity when you look at this neck encounter we don't have three prefabs here some of you might think we have the snake encounter prefab and the snake unit prefabs but that's not how it works in unity you have to be really careful about this if you change this neck unit prefab and it did well if you change its color if you change the health points for example and you apply the changes for this prefab it doesn't mean the snake encounter prefab is going to be updated we could use an another technique to make this work but it's not going to be in the scope of this lesson but if you change the snake unit you have to update the snake counter prefab as well okay this is very important for you and here in prefabs let's make another folder I'm going to make enemy units and put the snake unit inside here and I'm also going to make enemy encounters and I'm going to put this let's rename this to snake encounter and we're going to put it here in enemy encounters so everything is going to be in their own folders okay so now that we have this we need to work with the enemy spawn okay and now we just have to snake okay and the enemy spawner as I told you before is something that you can touch and once you touch it it's going to well trigger a battle so let's go back to our town scene here in the scenes folder we're going to open town let's save changes here even though the snake isn't there anymore we just save to be sure you're going to make an enemy spawner so in the hierarchy we're going to right-click choose create empty let's move this game object to this position here should be good we're going to rename this game object to enemy spawner and we're going to add a few components here for some role we're going to add the sprite renderer component because we're going to have a threaded visual here sorting layer is going to be gained and the sprite well let's click here in the circle and search for a snake but instead of using this red snake let's use this one here okay because it's going to work as an icon just like we have for the player and something small so we select the snake now this let's increase the scale on to as well to make it as large as the player like this now we're going to click on add component we're going to add a box Collider 2d okay because that means the player can touch the snake you can see that there's a green square surrounding the snake completely and well let's also make sure that we're going to have the is trigger selected this is important because we're going to make a very simple collision check between the snake and the player okay and how do we have this of course we didn't implement any logic for the player to collide with this snake we just move inside the snake okay that's not what we want so we need to make a script in the enemy spawner so in the scripts folder we're going to right-click choose create c-sharp script and name this enemy spawner okay so now we have this enemy spawner script let's add it here okay actually instead of enemies spawn or listening this spawn enemy let's use this pattern for script naming okay so spawn enemy so we do this hit enter wait for the code to be ready and apply the script here and once we do this we have to write just a few lines for us to spawn the enemy encounter so I'm going to get this pawn enemy script and drop it here good now we're going to double-click to open this script and we're going to add a few things okay it's going to have a similar logic to what we have with the the start battle script we're going to use this don't destroy load as well we're going to check what scene has been loaded so here let's start by adding using Unity engine dot seam management okay we want to add a field here so let's use serialized field and private game object enemy encounter prefab every enemy spawner that you collide with is going to have an enemy encounter prefab a side to it serving to to spawn the enemies in our case it's going to spawn two snakes and the player is going to be able to battle them also we're going to add here a private bull spawning and that's going to be false this is for internal use okay so in the start method we want to make sure that this game object is not going to vanish once we switch or three switch scenes so don't destroy a load and we pass game object as a parameter and we want to listen for when the scene is loaded so scene manager dot sing loaded plus equals on scene loaded okay good this video of course don't have this method yet we need to make it so private void on scene loaded scene scene as the first parameter and low scene mode mode as the second one and let's do something similar to what we did before so if sing that name equals to battle notice that this is a scene that we're still going to make okay so what's going to happen is if spawning okay so if spawning it is the same thing as asking if spawning equals to true but we can leave it like this just if spawning we're going to instantiate the enemy encounter prefab okay so if only has been set to true and the battle scene has just been loaded it means this enemy encounter prefab should appear okay and when this happens we type scene manager dot sing loaded - equals on scene loaded okay let me just fix the name here and here because this event no longer needs to be treated it nowhere needs to be well don't need to do anything else and we can destroy the enemy's Paulaner itself because we're just going to have the enemy encounter prefab okay and the most important part here besides this of course is we want to check if the player is colliding with the spawn enemy game object okay to do this we need to write a specific name we need to type the void on trigger enter because we're going to do a trigger collision okay remember this point that the enemy spawn or uses a trigger box Collider so we need to use ontriggerenter and don't forget to use 2d it's a two-dimensional collision the parameter is collider 2d as well other and what we want to do is to check if that other thing that we collided with the player is the player there are multiple ways to check this but the simplest of them is to basically ask if other dot game object dot tag equals it's a player the tag is a very simple way for us to to give names to game objects okay and this is basically to ensure that we can replace the name of the player for example it could be hero it could be toy could be any name but it's tag can remain as player okay to adjust this soon so when this happens we're going to say spawning equals to true and finally seeing manager dot load seen and we pass the string battle as a parameter okay so this is ensure that when we are in the battle scene the spawning is set to true the enemy encounter prefab is going to appear and everything is going to be done okay now I can save this let's go back to you anything we are going to the player here let's select the player game object when we want to ensure that it's tagged is going to be player so make sure you have this selected let's save the scene and to test this out let's make a battle scene okay so file let's go to your new scene let's go straight ahead and save this as battle remember you have to go to file build settings and press add open scenes so ensure the battle script is going to be here and also move this battle here to the scenes folder okay so if you look here we have title town and battle the three of them so let's go back to the title here to test this entire flow we're going to press play the player party has been loaded remember him to test everything along with the player party load the title scene because it's loaded here let's press play I'm going to touch the enemy spawner and once I do this we're going to the battle scene but the thing is we still haven't sent the enemy encounter prefab that's why the enemy still looks like this okay we try to instantiate it here this is null so the code interrupted here that's why this own enemy hasn't been destroyed so we can just go back to the town select the enemy spawner we have this feud here we go to prefabs enemy encounter okay let's like this owner and we're going to put these two snakes here okay so let's just drag and drop them here let me just check it's yeah we just have two snakes good okay so when we do this the enemy spawner has this encounter set let's save this scene title let's try one more time we press play we are here we can press play we can walk around avoid the enemy or if we walk towards the enemy we go to this scene so you can see that the battle scene is starting to build up we can already see our party activated again okay we have our major we have our properties here and we have these two enemies that we're going to battle okay they're both unit stats then we're going to to battle them later so from this point on we're going to to talk basically just about the battle we are going to build the HUD here we're going to build build the visuals of this battle and start working on the logic for attacking receiving damage showing texts so at some points we're going to work a lot on doing one logic and whenever you write a script the code is not going to work because we need to write more scripts or more places so we need to be patient here because we're going to build several things at once we're going to build a big communication system to make sure that we can expand this game later and make sure it's going to be easy to add new enemies new levels new heroes if you want and basically keep moving on so let's move on to the next lesson so this is what we have right now we have a title scene where it can press play to start the game we have a town scene where we can navigate and we have the battle scene that we can enter so the first step that we're going to take now once we have all of this game flow here is whenever we enter this battle scene we need a little Hut here we need a little interface to show some information like what player in our party is currently active which enemies we can attack so we need buttons for these enemies we need a certain group of buttons that we can click to either attack or throw a magic attack or maybe just run away from the battle so we need to implement the HUD for this battle scene okay so what we want to do is go to the battle scene right now it is empty but we are going to make a very simple background HUD like we had before so we're going to create a canvas we're going to name this background cannabis just as we did before render mode is screen space camera we're going to use our main camera here good and finally we go to the canvas scalar scale with screen size the same resolution of been using 1280 by 960 good and for this background canvas we're going to right click select UI image then we're going to click here to search for the background which is actually already here and we're going to press set native size okay so we have the same old background that we've been using throughout this entire course but now we want to make the actual HUD here we're going to right-click UI canvas rename this as how the canvas let's move it up here in the hierarchy also screen space camera main camera goes here we changed the scale of screen size 1280 960 okay good so this canvas is going to be separated into a few groups it's important that we divide things into groups so we can make everything work in the proper way the first thing that I want to do in the HUD canvas is to right-click choose you why and then raw image with this raw image we are able to basically use a very simple color we no need don't need images for that it could be just white red blue gray right and with this we're going to make a certain overlay here first of all let's go to the HUD canvas and change the sorting layer to HUD so we are able to see our raw image I'm going to rename this raw image to overlay and I'm going to do a little trick so this raw image can go straight to the bottom here okay we're going to click on this square where we can open some anchor presets for interface elements and when we have this we can change how its position works based on certain anchors in this scene okay it could be the top left could be the bottom right it could be bottom Center and notice that if you look in this last row here at the bottom or at the right you also have some stretch options so you can adjust what to be the size of this overlay if you hold the out button you're going to also set the position for the element that you are resizing so I'm going to hold the alt button or the option 1 and I'm going to select this option here we're going to stretch this overlay to the bottom okay so once we click here that white square became a rectangle and it's now bound to the bottom so we can resize this a little bit to to a size that looks good for us we might want to review this once we are making it work but well it's approximately this size and I'm going to change this color as well to tone of gray okay I think this one should be good and we can not just this as we go okay alright so now that we have this background here now that we have this working we need to add an action menu this action menu is going to show possible actions for the player it could be attack it could be throw some magic or it could be running away from that okay so what we want to start doing is to create an empty object okay that object is going to store all of these actions we do this we worry about hierarchy right from the beginning because we're going to be able to hide all of the actions at once or show all of the actions at once it is very important that we care about this because the interface needs to be controlled options need to be shown or options need to be hidden so to start we go to the hunt canvas we right-click which is create empty and finally we're going to rename this game object here to actions menu like this make sure the position is on 0 pages so we don't have any surprises here and now we want to make three different buttons physical attack action magic attack action and run action let's start with the physical attack action I'm going to right click choose create empty ok just so we have a quick start here this is going to be physical attack action this needs an image because it's something that we need to see and of course we need to click so I'm going to select add component I'm going to search for M and add an image as for the image we're going to click here in the circle and if you remember we had an icons sprite sheet and we divided that into several other icons okay so we have all of these we're going to use this one it's the sword icons 13 okay you press that native size just to make sure that the sprite is set correctly and also we're going to press add components and add a button okay so when we have this button here it means that this button this sword icon that we made here can be clicked also I'm going to set its position to be minus 131 X minus 130 on Y & Z can be left as 0 and later we can adjust that as we want and now that we have this physical attack action we need to make the other two okay it's going to be very simple you can just right click here choose duplicate we're going to rename this to magic attack action it's X position 0 so it's going to be centered and let's change the icon let me search for a good one here okay there are lots of options so it could be this a thunder icon but I think there are better ones here I think just a good check let's use this book here it's like a book of spells okay let me use this book icon okay and now we have to do the third one which is going to be the run action we're going to set its positional next to 130 let's change that icon to these boots okay actually just one and one thing I we can do that is a bit better for for the hierarchy here is to move all these actions to the position y 0 again because we can get the Actions menu and move itself down okay if we move the Actions menu the buttons are going to move down as well okay this happens because of the hierarchy since all of these three elements belong to the Actions menu most of the changes that you do to the actions mean you are going to be reflected to these buttons as well so let's keep them here and maybe put them a little closer let's use minus 90 and 90 for this one okay so they're going to look like this nice and simple okay very easy to see very easy to click so screens work well these buttons they're going to have callbacks when we click it means whenever we click on these buttons something might happen okay if we click on the attack button then our player is going to attack one of the enemies same thing for magic and if we run we try to leave to exit from this battle but we're not going to implement that right now we're just worrying about the interface the second menu that we are going to implement is the enemy units menu for example when we face two snakes when we have two battle two different snakes or bats or any enemy that we are going to to use here and we want to attack them we need to target one way could be to click in the icons of these enemies but maybe there are just too many enemies and it might be difficult to click you don't want to risk frustrating your players for Miss clicking into an icon so we're going to make another menu here okay I'm going to right click on hunt Kevin's choose create empty and I'm in training this two enemy units menu okay but how is this menu going to work how exactly are we going to add any icons here we need to make a menu item prefab it's going to be an image that is going to work as a button and it's going to show the icon of the enemy that we want to hit so let's make that briefer right now I'm going to right-click choose create empty here it's saying game object right now but we're going to rename this to target enemy okay we're going to add an image to this so we search for image here in that component select image I'm going to choose the snake for now but don't select this one it's this one it's kind of a preview of the the other snakes and after there is another one at the bottom which is better it's a snake face okay we could use these ones here but the snake face looks more like a button because of this black background here okay so we double click on snake face select set native size looks like this we're going as to select add component choose button so now there is a button for itself and we can later change this on click effect okay this is going to be adjusted as we make this game right now we're going to implement lots of things it might take some time to do everything right but once we get over this it's going to be much better okay let's also change the scale to 0.7 on X and on Y so it's going to be smaller and I'm going to move this to this position so we can fit more than one okay probably with the the scale 1 it wouldn't be we wouldn't have enough space okay so we have this target enemy here and I'm going to transform this into a prefab and to do that you should already be used of this we just select the target enemy we drag it from the hierarchy and drop in the prefabs folder so our target enemy button is going to be here and then we can delete the instance from our scene the question now is how do we add these target enemies prefabs here the one responsible for this is going to be the enemy unit prefab okay so you have to remember we have this snake unit prefab here and this guy is going to be responsible for doing for adding these options so we're going to edit the snake unit right now but you have to remember when you edit the snake unit you have to update the enemy encounter prefabs just to make sure okay so let's bring this snake unit here okay so it's going to be easy to edit good and now we're going to need two scripts I'm going to the scripts folder I'm going to add them now so let's go to right click create C sharp script I'm interning this kill enemy and we're going to right click create another script this is going to be create enemy menu items you can create enemy menu items so we're going to double click on both these scripts to open them and I'm going to tell you what they're going to do right now okay so the kill enemy script is going to be something very simple whenever we kill the enemy we want them a new item for that enemy to disappear okay doesn't make sense to leave something there for a nanny that's dead okay if the enemy is gone if we destroy it it should no longer be an option because we're not going to revive enemies or not going to interact with dead enemies we just remove this button so what we want to do here is to add a public game object menu item and we're going to remove the star in the update methods they're actually not needed and we're going to add here a void on destroy it needs to be written exactly like this because this is automatically called by unity when a game object is destroyed okay so it means if we destroy the kill enemy script it means that we have destroyed the snake unit you have to understand this this kind of workflow here snake unit is a game object that game object is going to have the kill enemy script so if we destroy the snake unit game object we're going to trigger the own destroy method in whatever scripts we have attached to that enemy unit and when we are here we're going to order this script to destroy the menu item so I'm going to have to destroy menu item notice that this is set to public if we if we save this script right now we go to the snake unit and we add the kill and I mean let's just wait a little bit if I add it here you notice that the menu item is set to none but we're not going to use anything here we're not going to attach anything that the reference here because another script is going to do that for us okay by reading the kill enemy script but anyway that's basically what we need with the kill enemy script now we want to go to the create enemy new items we're going to need a few things here but we're going to go over them one by one you want to have filled so we're going to serialize it so it's exposed in unity and I'm going to type private game object target enemy unit prefab like this this is going to be that button that prefab that we created that is going to be added to our interface this target enemy here okay so target enemy unit prefab let's serialize another field serialize field private sprite menu items sprite if you use snakes this is the sprite that we're going to use we're going to use snake face if we use bats then we use another sprite it's basically the sprite that is going to be used here okay remember this can be changed maybe we are dealing with other enemies besides the the snake so instead of making one target enemy prefab for every enemy you should make that a little bit more generic so you just pawn one prefab and you edit its values okay again serialize field we type private vector to initial position and also item dimensions these are going to be values that are going to help us to determine where in the interface these menu items are going to appear and finally serialize field private kill enemy kill enemy script this is where we're going to assign in this script is where we're going to assign the menu item value of the kill enemy that we are developing right now okay so everything that we're going to do here is going to be in an awake method so we're not going to use update or start we're going to use awake awake happens before starts so it's important that we understand this order of execution because if we do it in this start we cannot actually ensure the order of the script execution we want this to happen before because this is a setup okay so we got an awake first of all we need to get a reference to the enemy units menu we could do that by well making it a public variable here in the create menu items script and then making the assignment but we don't know yet what enemy is going to be spawned here so to make sure you can search in the hierarchy in the scene for that element you can type game object enemy units menu equals to game object dot find and then enemy units menu as a string here notice that this game object has a capital G okay we're calling this fine method and that is a static method it's something that belongs to the game object class itself and what this means is it's going to search along among all of the game objects in the scene for one game object that has exactly this name enemy units menu and we have it here enemy units menu so that means enemy units minigame object is going to reference this game object here okay which is what we want now we also want to see if we already have other items okay if we have other menu items in the game and that needs to be done because if you have what if you let's say we have two snakes the first snake is going to be let me illustrate that for you let me get the script I'm going to put it here okay so what is going to happen in this code is this whenever we want to add a new target enemy is going to check if there is another target enemy item here if there isn't it's going to be in this position okay so let's say we are adding a second snake it needs to check how many snakes are here how many items are here because depending on how many items we have we're going to add it in this position plus an offset okay because we had one snake before if we were to add a third snake we would do it like this we add it in this position and then we offset twice to the right so the third snake would be here and so on okay we need to check what how many items we have here so we can do that adjustment okay so let me take that off and to do this we're going to type game object and since it's an array since we can have multiple elements I'm going to to add these brackets here existing items is going to be and once again we're going to call game object with the capital G because we're access the class dot find game objects with tag this is a very useful method very useful static method when we want to search for multiple game objects in the scene and get multiple references to them okay and the tag that we're going to use here is going to be target enemy unit okay since we are trying to use these this tag we need to make sure that this tag is created in unity okay because if you look at the unity application here you're going to see that target enemy okay which is the item that we are using has no tag okay so we need to apply a tag here let's click on tag we select add tag we're going to click on this plus sign here and we're going to type target enemy unit it needs to be exactly the name that we're trying to use in this script target enemy unit okay so back to this prefab here we still have to apply the tag so we click here where it says untagged and move the target enemy unit notice that before we use the player because the player tag is already built in unity but since target enemy unit is something very specific we had to create our own tag okay so I'm going to mark this good let's save our scene and save our project just to make sure everything is going to be set here and now to calculate the position we're going to come here let's add a new line and I'm going to type vector to next position this is very going to calculate where the items are going to be so new vector2 we need to define the position in X and the position in Y okay let's start by doing the simplest thing I'm going to to add some line breaks here so we have to set a value for X and a value for y if we were to add only one enemy this would be very simple X would be initial position dot x and y would be the initial position dot Y simple as that okay y is not going to change in this game however X needs to take into account how many existing items we have so we're going to add an offset to this calculation and this is actually pretty simple we're going to add here existing items dot length so it's going to count how many game objects we have here and we're going to multiply that by item dimensions dot X okay this is something in the atom dimensions value as well as the initial position value these two guys are going to be defined in unity so we can manually set everyone's position okay so this should be enough to calculate the next position and now finally we're going to take the prefab reference and instantiate that into the game we're going to do that dynamically okay so let's just add here a few comments find the unit's menu then calculate the item position and finally instantiate the item okay so this is what we're going to do game object target enemy unit is going to be instantiate it's a method that comes with every mana behavior and here the first parameter is going to be the target enemy unit prefab which we are going to set soon and the second parameter we're going to automatically set the parent which is going to be the enemy units menu dot transform when we do this it means that this prefab that we're going to instantiate is going to automatically be under the enemy units menu in the hierarchy if you don't do this it's going to be in the root so some calculations might not work correctly it's going to be outside the menu we're going to have several problems with this that's why you care about doing this right now okay now we're going to type target enemy unit dot name is going to be the string target okay plus game object dot name okay remember this create enemy menu items is going to be here in the snake unit okay so when we do this operation here we're going to say that this enemy unit is going to be called target snake unit if we had a bad unit here the item that we were making the target bat unit so it just changed names anything's easy to see now we need to change the target enemy position okay so it's going to be where we calculated to do this target enemy unit dot trains from that local position is going to be next position it's the position that we calculated and it needs to be local here because we're going to well check the position here in unity so for example let me get this target enemy and put it here the position that we see here is local okay so it's relative to the parent so if you want to change we're going to use some certain values here but if you want to change it you can just move your item to where you want right now some values and then put it in the snake unit we're going to see that shortly right so all right we set the position now very important here whenever we click a button we want a callback to happen we want an event to happen and that event I'm going to declare here as a public method it is void and its name is select enemy target okay so this is the method that we're going to make and to make sure that when we click the target enemy unit we're going to call this method here we're going to type target enemy unit dot get component of type button okay once we do this you see that button is red and it is red because we don't this rip node doesn't have access to the unity interface scripts so we need to go to the top here and type using unity engine uy okay so you see the button is now all ready to be used so button dot on click this is the the button click event okay this is called whenever I click the button and now finally we're going to type dot add listener so when we do this we're able to call a method so this ad listener here is a method so we need to open in closed parenthesis and this is what we're going to type we're going to open and close parentheses inside it type an arrow like this you cosign greater than sign and then we're going to type select enemy select enemy target like this like we are calling the method okay so it's going to look like this okay we are going to add the listener we're going to want it to select enemy target it's going to work also before that let's make sure that the scale of this target enemy unit is going to work correctly target enemy unit dot transform dot local scale and that is going to be a new vector2 and the values are going to be 0.7 F 0.7 F okay so finally when we do all of these things we also have to update the sprite of that snake okay so target enemy unit dot and get component of type image dot sprite okay so we're going to change it it's going to be the new items price okay we're going to see that in action very soon and when now that we're done is instantiating the item we have to set the menu item to the kill actually in the kill enemy script so we do this by typing kill enemy script dot menu item equals target enemy unit when we are done with this we go to unity let's wait for the code to be compiled so we can change everything else okay so we're done we go to the snake unit let's add the create enemy mini item script here okay and we're going to add a few things so the target enemy unit prefab is going to be this prefab that we created target Amy let's drag and drop here the menu items pride for this snake unit it's going to be the snake face it is the one that is already in the prefab but still it's important for you to sell it here so I'm going to click on this circle search for snake face which is this one good the initial position is going to be minus 550 and minus 360 and the item dimensions that we use are going to be a hundred and on X and on Y as for the kill enemy script you just drag the snake unit itself here okay so it's going to reference the snake unit that we have okay so this is basically what we need for changing the snake unit for adding the kill enemy create any menu items we're going to press apply here in snake unit and we're going to remember to update the snake encounter so I'm going to drag this prefab here I'm going to delete these two snake units that are inside the snake encounter it's going to break the prefab but it's fine I'm going to put this snake unit here duplicate it and add another one here and finally it does end this is very important we select the snake encounter game object and we press apply okay so this prefab has been updated and the snake units inside it are going to have the kill enemy script and they create enemy menu items okay now we can get rid of this encounter here okay so after we are done with this let's do a quick test ring into the scenes folder then we're going here to to title let me just clear the console here we're going to press play so we have our title we press play let's go to the snake battle unless we enter the snake battle we have the two snake buttons here notice how they automatically set their positions by just having the initial position in the dimensions of these buttons they automatically go to the proper places if we had a third snake then we would have a third button here okay now that we're done with this initial setup of the battle scene we need to move on and add some player information let's do this in the next video all right let's go ahead and show some player information we're going to open our hub cannabis and we're going to add another empty game object inside so right click create empty make sure it's in the position zero and here we're going to call this player unit information okay let's leave it like this this player unit information is going to have kind of three elements we're going to have the player unit face we're going to have the player its HP that's health points and the players MP mana points or magic points let's start by adding the player unit face right-click UI image let's rename this to player unit face and we're going to search the source image for the mage face ok let's see if we have it here there we go mage face we double click here select set native size and you're going to move that player unit face to the bottom right ok so it's going to fit nicely here in our interface this position should be good ok so we're going to add now the health bar and that's going to be composed of two things this is going to need the actual bar so that's the image and we're also going to need the text for that health bar just to say HP for example let's go ahead and create these things here in the player unit face I'm going to right-click choose UI then image we're going to rename this image to player unit health bar the source image is going to be health bar which is already here we're going to set its native size so it's going to be exactly as in the source file and let's change the x position to 1 9 2 on X ok actually notice that the player unit health bar here is under the player unit face let's make sure it's outside it ok it needs to be and inside the player unit information ok let's try again 191 X actually it needs to be a little bit more so let me put it here let's manually set this ok and position on Y let's use minus 3 3 3 here and we also need the player unit health text so I'm going to right click and player unit information uy text I'm going to move this text down here and also make sure it's going to be big enough to show the HP information so we're going to select this tool for resizing change its size like this the font is going to be Arial let's use 36 as font and make sure this is high enough to to fit the text okay the text is going to be HP let's make this bold and also align to the center horizontally and down vertically okay and you might want to change this color too wide or too green whatever you think is going to be better I'm going to use white here okay so we have HP now there is one interesting thing about the player unit health more notice that if you deal with this unit health bar and you try to scale it on X the health bar is going to scale relative to the center so there is a very important thing we have to talk about here okay we need to talk about the angers of this image okay so if you look here in Angers we have these positions set and on X on minimum maximum and the pivot all of them are set to 1/2 okay but if you change all of these values let me just suggest here if you change all of these values to zero if you change the scale of the bar then it's going to scale like this okay it's exactly what we want every image that you make its pivot is set to the center okay so that's why because when you try to resize things it's going to resize equally in the left right top and bottom sides but by setting the pivot to the left okay when you scale it's going to look like this and that's what we want normally games use them like this it's a bar that decreases from the right to the left okay so we need to adjust this now too to ease our calculations you could leave that on 0.5 like this but the calculation would be much harder and there's an interesting example in unity of that notice that if I resize this bar not only the width is changing but the position on X is changing as well unity is doing that calculation automatically to us so it this left size of the bar is going to stay in this position okay and we would need to manually this calculation to adjust the size of the image through code but that would take a lot of work okay of course it wouldn't it would eventually work of course we just need to do the mathematical calculations to do this but if you change the pivot to zero well you can just change the scale okay notice that now that I've changed it to zero and I'm resizing it from here only the width property is changing or you can just change the scale as well we would have the same effect okay so that's what we need for the health bar as for the the mana bar we can basically get let me just rename this two-player unit health text we just get these two we're going to right click and choose duplicate so they are here down I'm going to rename this player unit health bar to player unit mana bar and this one is going to be player unit mana text I'm going to select both of them by holding comment or ctrl and clicking on one and then on the other and I'm going to move them down here okay let's change the mana text from HP to MP and also get the bar change its image so instead of health bar we're going to use mana bar okay so you have HP and MP properly set here this should be enough for us to finish all the necessary work for setting the images we still have a lot of work to do to make this work but as I told you before things need to be done step by step so we can make this work properly okay we already have an interface buttons don't work the the bars are not updated as we try to play the game but we need to move on to other lessons so we can build this game and in the end you're going to have this working completely let's move on to the next lesson if you look at the battle scene you're going to see all of the elements that we made so far but there are no animations it's a bit dull a bit simple and there's no movement so that's not going to bring excitement to the player when they are playing the games so this is something that we have to care about that we have to adjust in order to do this let's make animations for these elements the snakes the mage and another player that we're going to make which is the night okay let's stop the scene and let's make sure we are in the title scene because remember the player party starts here we're also going to add our enemy unit here so we can change it and then update its enemy encounter I'm going to drag and drop the snake unit here so there we go I'm going to now select the mage units here and we have to remember a few things for the animations of the mage to work and any unit that we make here to work we need to have animators so we need to make a few things here in the animations folder we have the animations for the player in the town scene but I'm now going to right-click choose create folder and name this mage and sine mage I'm going to right-click create then animator controller this is going to be mage controller magenta lighter actually okay good to make sure this is going to be loaded into the mage units we can just drag this animator controller drop it here and the animator component is going to be automatically added and now that we have this we can work on the mage animations so for every element that we make here we're going to have four different animations let's right-click choose create then animation this is going to be idle which is the four which is the full animation every element every unit by the four is going to be playing the idle animation I'm going to duplicate that to make a hit animation that's when we receive damage we duplicate that and rename this to physical attack when we perform a physical attack of course we duplicate that again and we do a magical attack okay when we cast some spells so we need to select the mage unit now go to the animator and put these states here okay so to begin we're going to drag this auto animation and drop it here so we have the idle state automatically created for us with our arrow in here okay but there is one thing we have to do here we need to add the other three animations we need to add hit we need to add magical attack and we need to add physical attack this is going to be much different than the player animator that we had before and why is that basically the idle animation is going to be played all the time okay at all times but when we play the head animation the magical attack or the physical attack they're going to animate and then move back to idle so basically what we have to do is to make the transitions from each of these states to the idle one so we right click on hit make transition idle right click on magical make transition idle physical makes transition so idle okay and the idle animation also needs to be looping okay because the player is going to be moving it's not going to just play once different from the other animations now that we have this we can actually change the animations down let me make this state machine so we're going to mage unit here select animation and we can see that we have idle hit magical attack and physical attack so to help us make these animations we're going to the surprise folder we are going to select battle ok and we have our mage here so an easy way for you to check which sprites should be used this you can just select image zero not mean improve the size of this and you can use the arrow keys to to navigate through all of these pride so we have 0 1 2 3 4 ok when you go to 5 it's a bit different so we can now see that the idle animation is composed of 0 1 2 3 4 ok so and you can see actually that 0 is the same thing as 4 so we don't even need this it could be 0 1 2 3 0 1 2 3 okay it's basically up to you to decide what sequence you're going to use ok so let's go ahead and make that animation I'm going to drag mage 0 and put it here right in the beginning mage 1 is going to the the frame 10 mage 2 2 frame 20 mage 3 to 30 and we can even put 4 here because it's basically going to repeat the the first one okay so if we look at the scene on the scene window here we go the animation and press play this is how the idea is going to work okay so it looks smooth looks good enough that's why we use that that last frame here so I repeat it so it looks smooth okay so the idle animation is done let's go ahead and make the order once okay that was idle so let's see what animation is this this is probably let's use this one for the magical animation seven eight nine ten okay seven eight nine ten this should be good enough for us okay so I'm going to magical attack and put seven here eight here nine in this frame and ten that's going to be the magical attack as for the physical attack let's use eleven twelve thirteen okay so eleven zero twelve on ten and thirteen on 20 and finally for the hit we're going to use fourteen and fifteen fourteen on zero fifteen on ten okay so we have all for animations let's give them a try I'm going to play the hit animation okay like this also notice that the frames are different but since this last one is going to be played instantly we should repeat mage 15 here okay sometimes we have to do these adjustments so it's very important for you to check so it looks like this much better let's see magical attack looks good enough physical attack good enough as well and I do as we know it is already working okay so this is all that we do to make the animations for our mage they're going to be played later when we are working on the code but now we need to basically do the other animations so let's go for the snake unit okay animations great so we see we make lots of folders we make lots of we try to follow a good hierarchy here so everything that you do is going to be organized in improper places it's just much easier to go straight to where you want by making the proper folders and well just diving into them and making things work for the snake let's create an animator controller snake controller okay so let's make the animations for it as well create animation we need to use the same names even though the the snake doesn't have a magical attack let's create in anyway just to make sure it's going to follow the same structure so we have Idol we have hit physical attack and magical attack okay the snake unit is going to have the snake controller inside it good we open its animator to edit it so there we go we have the entry I'm going to put idle here and as usual remember to set idle to loop we're going to put hit here physical attack goes here and magical attack goes there we make transitions from for each of them to the other one so we do the transitions here and we can now go to the animation okay notice that we are seeing the mage units so that's why it's important for you to make sure to click on the thing that you are editing so we can change the idle values and let's go to the sprites okay we can collapse the mage here we can open the snake and deal with just these sprites so let's see how I do is going to work 0 1 2 3 4 5 so it's just like the major 0 1 2 2 4 5 so I'm going to drag 0 here and notice that when I'm dropping it this time it's asking me to select between sprite renderer and create mini items it's asking me this because since I'm dragging and dropping in the animation here you know it doesn't know what image we are trying to change and there are two images in the snake unit ok we have let me check here we have the image for it ok in this price range we have this pride field here but we also have another field which is create enemy menu items we have this here snake face the sea and it doesn't know what we want to change it's going to ask us so if we drag and drop here we just select sprite renter okay so 0 1 2 123 and 34 140 I think this one is not exactly where it shouldn't be you can also fine-tune them by just clicking on these diamonds and making sure they're on these hard lines okay so I've added 0 1 2 3 4 and 5 goes here you can even do it like this ok you can drag into the the time line so this is the idle one this one could be the physical attack so that's 6 7 8 9 10 ok for the physical attack so let's go 6 nice with sprite renderer of course 6 7 8 9 and 10 like this we could even yes this is good enough ok so we have the physical attack we're not going to do anything for the magical attack now you could use the ones that we have here but since the snake is not going to do magical attacks let's leave it empty for now and as for hit we can use 17 and 18 okay so 17 on 0 sprite renderer 18 here and we repeat 18 one more time ok so to check the animations we bring the scene down here ok let's play the hit animation looks like this the physical attack like this very interesting and idle if you play it looks like this ok looping very well so let's move the scene back here and we should be done with the snake animations ok so here in the snake unit let's review the animator looks good we're going to apply the changes of the prefab all right go to where the snake unit was here and let's delete I actually can leave the prefab here because we're going to grab the enemy encounter drop the snake encounter here I'm going to delete these two snake units inside the snake encounter it's going to break the prefab that's fine snake unit goes here I duplicate and I put another one here just to update the snake encounter prefab okay so let's delete this prefab as it's been saved okay if you want to confirm you can look inside the prefab for the animators of the snakes which is good and we can even test if the snakes are animating properly we can just press play here face the snake and we can see that the snakes are playing the idle animation and the mage is also playing the idle animation so to finish this let's make another unit for us we're going to duplicate this mage unit okay let's move it down and we're going to change a few of these stats later okay but let's keep it the same unit stats here but let's rename this to Warrior Unit okay let me just check if that's in a name here so yes we have a warrior here so that's going to be the warrior unit the sprite renderer is not going to use mage zero it's going to use warrior zero so it is going to have this sprite here and of course we need to change its animator as well so you see making new units is super easy you just duplicate change the sprite and then add the animations for it and later it's going to work just fine in the animations folder we create a new folder warrior we need a controller so create animator controller warrior animator we go to the warrior unit and we put that animator here so we have the animator controller now we make our for animations create animation we have Idol we duplicate that we have hit duplicate physical attack duplicate magical attack okay as usual the four animations that are going to be used now we make sure that Idol is looping and now we need to select the warrior unit look at the animator and drag the Idol here and to make the auto state we also drag hit here magic attack here and physical attack even though it seems like we're doing a lot of the same thing over and over again this you have no idea how important it is to practice this it's actually very good that we are making the same step several times because this is going to become natural for you as you make other games or as you improve this game okay make sure you add the transitions between the other states and idle and now let's go to animation select the warrior unit and make its animations so I'm going to the to the battle sprites folder let's expand the warrior here so for the idol 0 1 2 3 4 and that's enough K 0 1 2 3 4 0 goes here 1 here 2 here 3 goes into this place and 4 goes into this place and I think we should repeat 4 by the way I came to 0 let me check here 0 1 2 3 4 0 1 yeah we should repeat number 4 okay so 6 7 8 9 10 is going to be the attack 6 7 8 9 10 ok so let's go to physical attack 6 here then 7 8 in the proper place 9 and 10 for the physical attack so we have one for defense well we're not going to use this we're not going to use magical attacks for it as well but well for now we can use this guy for for kind of making a magical attack for the warrior unit if you want to use it it'll be something very weak ok so 12 to 15 12 here 13 goes into this place 14 here and 15 here let's repeat 15 as well this place and let's go to the hit effect which is simple we're already used to this as well we're gonna select 16 here 17 here and we repeat 17 as well okay to test this out we can get the scene window put it down here and play the animations okay so hit looks like this let's go to magical attack looks like this physical attack it's a Pierce with the Warriors Pike and the IDA one is this okay we can stop changing the animation we can move this scene back here let's go back to the animator let's check if everything is in place and after we are done with this we can go back to the scene and give it a try we now have a party with two elements both the the magic of the mage and the world they disappear because they're not going to be used in this scene if we start a battle though we can see both of them but as you see they're not in the proper positions here so you can just adjust them here looking at the player party and finding good values so the mage I'm going to move with it to the position 0.8 and the warrior two - your dot actually - one okay so let's memorize these values 0.8 for the mage and -1 for the warrior and back in this scene here 0.8 and - one for the okay so they're going to look like this in the title scene you have to do a little bit of investigating to see what are going to be the good values for position once you do this it's going to look like this okay much better we can see both of our units here and the animations are playing correctly now that we have this animations we can dive into completing the logic for this game we need a turn-based battle system okay this is going to allow the enemies to attack the players and the players to attack the enemies and try to destroy all of them so this is going to be our focus from now on I see you in the next video all right so let's go ahead and develop a simple yet interesting turn-based battle system what is going to happen here is that metal system is going to find all of the units that we have it could have player units enemy units it doesn't matter and it's going to keep all of these units in a list these units they're meant to be ordered based on their speed okay so for example if we have a fast enemy much faster than everyone else this is going to be the first one to attack once it attacks its going to recalculate what its going to be its next acting moment okay so we're going to do a little little to play a little with numbers to define who is going to attack first also this system is going to enable us to attack the enemies but we're going to do that later okay in a later lesson but anyway we need to make that turn system script so let's move on to the scenes folder go to battle okay this scene which doesn't have much information inside it and here we're going to make a turn system game object so we right-click choose create empty they rename this game object as turned system it also needs its own script so we're going to right click here create c-sharp script and then turn system okay once the script finishes loading we're going to get this turn system and drop it here okay so I'm going to drag it here and drop it here and we're going to double click the turn system script to open it so this is going to require a few things okay we're going to see thing one by one and I'm going to explain that II okay first of all in the top of the class we're going to type private lists Bliss's is an array of elements it works as an array but we have some interesting methods inside it so that's why we're going to use this class it's a generic array so we have to specify what's the type of the things we're going to store so I'm going to specify it between the lesser than greater than signs unit stats and the name of this variable is going to be units stats okay it's going to be an array of units also we're going to need some private fields so we can add some references to us okay I'm going to add here a serialized field attribute and then private game object Actions menu and enemies menu actually enemy units menu this is very important because the turn system is going to be responsible for activating and deactivating these two menus here it depends on what's going to happen right now it really depends if the enemies are attacking if the players are attacking if they are selecting a certain enemy so we need to play with these two menus here so right in the beginning in the start method we're going to type units stats equals to an evil list okay this object needs to be in it in this meeting well preloaded we need to initialize it otherwise we would deal with a null element so we wouldn't be able to move forward and now we want to get all the player units okay so add player units to the list to do this we're going to type game object it's an array then player units and that is going to be game object don't find game objects with tang remember it's the plural version game objects with tag and the tag is going to be player unit okay and again this is a tag that we still don't have we don't have player unit or enemy unit so we should go ahead and make these two tags okay let's go back to the title here let's save the scene as well we're going to select both of our units here and we need to apply the text so let's go to the tag menu here press add tag click on the plus sign and we're going to add player units let's since we are here let's also add enemy unit okay now it uses both of our player units here and apply the player unit tag okay so they're both going to be here player unit we also need to do the same thing for the enemies so let's drag this neck unit here I'm going to apply the enemy unit tag and hit apply and that's where the encounter since this is a very minor change we can just drag the enemy encounter prefab here I'm going to expand that select both snake units and choose enemy unit here okay so they're both going to be set we apply the changes to the snake encounter and let's get rid of this before I move forward let's take a look on this warning here it says that create enemy new items that item dimensions never assign so we have initial position so probably what we have to do here is just put item dimensions in another line private vector to item dimensions let me just save this go back to unity let's wait for that to be ready and I think it should be good let's just check the enemy prefab here okay so dimensions are still here yes so it's looking good now let's move back to the battle scene let's save the changes so the players are going to bet they did so back to the turn system all right now let me go here now we we are able to fetch the player units by doing this since the party is going to be in the battle scene this array is going to basically show two of our any of our players and now we want to iterate over these units so I'm going to type for each game object player unit in player units that means we're going to iterate over every element of this array and every element that will working we're going to give a name to it called player unit so you can do you anything called related to it so now I'm going to type unit stats current unit stats is going to be player unit dot getcomponent of type unit stats okay so we're going to get this component from it now we're going to type a line here of something that hasn't been implemented yet but we're still going to do we still going to add it here in order for us to look at this entire script and then move on to the other script we're going to type current unit stats dot calculate next act turn and we pass a zero as a parameter it's Marcus red because it doesn't exist but you're going to see that soon enough finally units stats dot add and we pass current unit stats as a parameter so the list here is going to be updated now we do the same thing for the enemies add enemy units to the list so game object array enemy units is going to be game object dot find game objects with tag enemy unit okay should look like this now for each game object enemy unit in enemy units we're going to do this loop here unit stats current unit stats equals to enemy unit don't get component of type unit stats good current unit stats and then the same thing calculate next act turn and we pass zero as a parameter and then units stats dot add current unit stats now we're going to do something that is going to be used for ordering this list every time we change the list we need to sort it okay so sort the list based on the speeds I'm going to type here units stats dot sort it's going to be like this okay we're going to talk about this in a few minutes as well now when we start this we're going to actually to to to type as well actions menu dot set active false and enemy units menu dot set active false both are going to be hidden and then we're going to call a method called next turn which is still not implemented we're going to implement it now this next turn is going to be called whenever a new unit is going to act it could be another enemy it could be the player but still this is what is going to drive the entire round mechanics here now here public void next turn so first of all we need to get the first unit in our list remember the list has been sorted okay because of this call here so we want to get the first unit which is the one that is going to act service type unit stats current unit stats is going to be units stats in the position zero even though we units stats is a list we can still access elements in order like this as an array by using these brackets and when we do this we're going to type units stats not remove and then current unit stats we remove it because we're going to re add it to our list we're going to recalculate what is going to be its next acts acting turn and sort the list one more time okay now this unit we need to check if it's dead okay because if it's dead we don't want to do anything else it's basically going to be removed so we ask if not current unit stats dot is dead okay this is a method that we're going to make as well but it's basically going to check if it has positive health points okay so just do it like this this is basically asking if the unit is alive if it is we're going to type King object current unit is going to be current unit stats dot game object okay serene to access that also we're going to type current unit stats dot calculate next act turn okay and what we're going to pass here is current unit stats dot next act turn ok again something that doesn't exist but it's going to be implemented we're basically getting what it's correct next add X turn and we're going to increment a little bit more based on the speed when we do this we type units stats dot add current unit stats so we're going to re add it to the list and since we re added that unit to the list we want to sort it again units starts dot sort okay all of this that we are writing is not about acting it's not about the enemy's attacking or you applying magic is just defining who is going to act first and reorganizing the list ok now that we are here we're going to type if current unit dot tag equals to player unit it means the player should act but as we have no methods for the player acting we're just going to type debug not clog and then player should act we're going to type also else if it's not a player unit then we want to print a message to the console which is going to be debug log and the string enemy unit should act okay or just enemy should act now if the player is dead however so we're talking about this if block we're just going to call next turn again else next turn can we do this because if we don't do it then the code would stop here we would move on to a next turn we wouldn't move on to a next turn the game would basically stop okay so if something is that it's not going to act we just move on okay so now we have this implemented here we need to implement the remaining methods and variables that we should add to our units stats class okay so let's go to unity let's go to scripts go to unit stats and let's do some improvements here okay we already have health man attack magic defense and speed we're also going to add public int next act turn which starts a 0 this is the thing that is going to be calculated on every turn we're also going to add private boom dead okay and then for the methods we're going to type public void calculate next act turn and it receives an int current turn here okay which is the the parameter so this is basically going to recalculate the value stored here so I'm going to type next act turn is going to be current turn passed as a parameter and now for the mathematical correlation Plus math F dot seal to int I'm going to explain that and here we're going to put a hundred F divided by speed next act turn is an integer number so when we do a division we want to make sure that it's going to be an integer monomer with no decimal part that's why we use this to int type of method its ceiling because for example if the current turn is 2.2 it's going to round up to three if it's 4.7 it rounds up to five okay it's the the fair solution here mathematically speaking for calculating the speed okay and we do this operation here because small speeds if we have a speed called one this is me to return a hundred so it's going to take a hundred units later of the the acting turn system that we're making here if the speed is high however and this is going to return a small number if the speed were a hundred then this would return just one so that would be a very fast unit it's important for you to understand this little mathematical system here okay of getting a hundred and dividing by speed okay we're going to also make public bull is dead and this is basically going to return that okay the value stored here and we also need to implement something for when we sort the array okay when we call only stats sort we need to implement something that makes the elements of this list to be comparable so to do this in the unit stats we need to implement an interface we had a comma here in type I comparable but this I comparable interface here needs is it belongs to another namespace so we're going to add here using system okay so we can use this eigen parable since we have this interface we need to implement a certain method of this interface which is public int compare to the parameter is going to be an object and I'm going to call this other stats and here we're going to type return next act turn dot compare to let's open and close parentheses and we're going to compare with something else here we need to compare this to other stats dot next act turn however other stats is an object it doesn't has this next actor that's why we need to change this to unit stats okay we're going to cast this object to be unit stats and also we're going to put this in parentheses as well so then this call here is going to work correctly okay the code is going to understand that we are calling the next act turn with this object converted to a unit stats class okay so this is basically what we need for this compare to okay now when we do this we should save the script go to unity let's check for the console to see if something else was changed notice that dead here it's saying it's never assigned to it's always going to be false let's force it to be false just for now this is not necessary but since we're not changing the value of dead let's well set it to false so we don't have any warnings it's kind of weird we are telling unity that we know what we are doing okay and now let's go to the title scene and give this a try we go to title here we are a unity RPG play we're going to the snake and once we get here we already have a problem and that happens because the turn system doesn't have the actions menu or enemy units menu we need to change this so back to battle we go to turn system we expand the HUD canvas and put the actions menu here and the enemy units menu here alright let's save that move back to the title and press play ok so there we go play go to the snake we started here and in the console we successfully printed enemy should act ok so we are on the right path here we are making that system that turn system and that is going to enable us to make these guys to attack the players and later the players attack the enemies ok so let's move on to the next lesson and talk about the attack logic all right so let's implement the attack system we're going to work in a usual way to make this attack system attacks are going to be implemented into prefabs we do it like this because we might want to implement different attack powers with different magic powers with different capabilities to different elements so for example the player the mage could have its own attack properties and the knight has its other attack properties the mage could have higher magical powers and weaker physical powers and the warrior would be the complete opposite of that it would be strong but it wouldn't have good magical powers so it's interesting to work on these attacks like this first of all we need to go to the scripts folder and make a script we right-click choose create c-sharp script and we name this attack target so we double-click to open this n model and do a few things first of all we need to type public game object owner every attack needs an owner it's the the unit that is performing the attack and now we need to add a few things so we're going to type serialized field we start by typing private string attack animation it's the name of the animation that is going to be triggered we also see realized viewed again private boo magic attack it's going to determine if this attack is magical or not same thing for the mana cost private float mana costs okay same thing for private float minimum attack multiplier we type private float max attack multiplier and finally private float min defense multiplier and also max private float max defense multiplier all of these feeds are serializable but the owner okay attack animation magic attack mana cost minimum and maximum attack multipliers and minimum and maximum defense multipliers and this is basically going to have one method for now okay we're going to type public void hit whenever we want to hit we want to hit a target so the parameter is going to be game object target so we need to do a few things here we need to get the unit stats of the owner and the target so we can do some calculations in order to perform the attack so to do this we're going to type unit stats owner stats is going to be owner dot get component of type unit stats the enemy is going to be very very similar unit stats target stats is going to be target not get component of type unit stats okay good to perform an attack we need to make sure that we have enough mana to do this so we need to ask if owner stats dot mana is greater or equal to man accost okay to perform the attack if this happens we can already add a line in the end that is going to be owner stats mana - equals mana cost okay just to make sure we're going to consume the necessary power to perform this attack right now here we need to do some calculations we start by typing float attack multiplier okay and so what exactly is the attack multiplier we do this to calculate what attack value is going to be used in a range of the minimum and maximum attack values so we can just type here random range and we can pass main attack multiplier and max attack multiplier as parameters we're going to get a value between both of these okay and now we type float damage first of all we need to check if we are doing a magical attack so we're going to ask magic attack in a question mark okay it's a ternary operator that we're going to do first we put a boolean variable it's like we're asking a question hence the question mark then we're going to put the outcome if this question is true if magic attack is set to true if this is set to true then we're going to use attack multiplier multiplied by owner stats dot magic however else so we used this column here if it's not a magic attack we're going to use attack multiplier multiplied by owner stats dot attack okay so just to review we have three parts here first of all we ask a question if this is true then this is the first value that we return after the question mark if this is false then we return this second value here after the column okay so this is what we need to calculate the damage now we need to move on to calculating the defense multiplier so float defense multiplier is going to be random dot range minimum defense multiplier maximum defense multiplier just like we did with the attack and now we're going to recalculate the damage based on the defense that we have here okay we're going to type damage so we're going to override the damage value here this is going to be math F dot Max between zero and value we do this because depending on the defense values of the target maybe it's going to reduce so much damage it would return a negative value so when we do this we insure the lowest value possible is going to be zero okay so math f dot max and here we're going to type damage - and between parentheses we put defense multiplier x target stats dot defense okay so we're going to recalculate the damage here now that we have this damage value we want to play the head animation on the target so we're going to type owner dot get component of type animator okay dot play and actually we're going to play the the animation here in between permanent this is I'm going to put attack animation which is the string of the animation that we want to play okay now we're going to type target stats dot receive damage and we're going to pass damage as a parameter it's something that still needs to be implemented but we already make this work here okay so we need to implement this receive damage to do this let's go to unit stats here let's add another method public void receive damage and it needs to receive a float damage parameter because we need to tell how many health points are going to be lost so now we're going to type a health minus equals damage to decrease the health and since we are being hit we're also going to type we also need to get the animator of this unit stats and play its hit animation so here I'm going to add let me put it at the top you realize field and then public animator animator okay and here we're going to put animator don't play and we're going to play the name of the state hit everyone has this hit state here okay now we do this we need to also whenever we receive a damage we need to verify if the player is dead so if health is lesser or equal than zero it means that that equals to true okay no no the player no longer has enough health points so it should be set to true I'm also going to add here a game object dot tag equals two deadly unit and finally just add the semicolon and finally destroy game object like this let's go to unity and implement this tag so I'm going to come here and let's select any element actually we don't need to go straight to in unit you can just go to tag add tag press play and press the plus button here and add dead unit and hit save okay but don't worry it's not set here to the player party you don't have to worry about this it's just to create the tag just to make sure it's not going to be lost anywhere also whenever we receive damage we should put some text to do to work as a feedback so we need to add two more values here in the unit stats we're going to serialize field and we're going to add a private this one is private as well by the way we're going to add a private game object and then damage text prefab okay and we're also going to add here a private of vector to damage text position okay we're going to in to add a text to the game change its its text of course and set its position this we're going to deal with texts we're going to add a namespace here using unity engine dot you I okay so what are we going to do here to implement this text first of all we need a reference to the HUD canvas because this is where the text needs to be game object HUD canvas is going to be game object dot find HUD canvas okay remember it needs to be exactly this name now game object damaged text is going to be instantiate to dynamically add it to the game and we pass damaged text prefab okay and also HUD canvas dot transform so it's going to be its parent okay now to change the text we're going to type damaged text dot and get component of type text dot text to change its string is going to be I'm going to add two double quotes plus math dot let me use this as floor to wind and I'm going to put damage as a parameter okay so it's basically going to change whatever text we have there to the number of the damage that we are using here now damage text dot transform that local position is going to be damage text position like this and damage text dot transform local scale is going to be vector 2.1 this line here is for basically setting the text to be in the position we are determining and this one is to make sure that the scale is going to be one it's important to do this because when you instantiate elements and set its its parent the scale might change depending on the scale of the elements that we have here it's important to do this to ensure that it's going to work properly okay so now that we have this the attack attack target should work fine we now have this receive damage is no longer read but there are a few things we have to set first of all let's make this damage text prefab okay i'm going back here let's go to let's go to the battle scene let's do it there sings battle so in the HUD canvas I'm going to right click create you wine then text I'm returning this damaged text ok let's use this two here increase its size I'm going to change this text to 9999 just to make sure it's going to be big enough for many cases font size 36 then it's going to be bold and I'm going to change the color to red okay it should be bigger by the way let's use 50 yes let's align in the center horizontally and Center vertically and now we have this it should be enough for us to save the damage tax into a prefab I'm just going to drag and drop here in the prefabs folder okay damaged text I'm going to delete it from here and now we have to update the units to put the necessary values there I'm going to save the scene go back to title and go to our units remember we have new things on every unit so we have to update them we have the animator the damaged text prefab and the position so for the animator of the mage unit we just reference itself we drag and drop it here not-not-not ex prefab it's going to be here animator damaged text prefab we just grab this damaged text and drop it here and for the text position let's use 0 0 so we do the same thing for the warrior unit drag and drop here damage text prefab goes here tax position 0 0 I don't think that's anything else besides these two settings so let's also update our enemies the snake unit the animator references itself damage text goes here to the prefab we hit apply let's leave the snake here because we're going to update the enemy encounter so we have the snake encounter I'm going to take its two children off here snake unit goes inside the snake encounter we duplicate it move it to another position maybe here or here select the snake encounter press apply and everything is going to be update now that we have dived into implementing this receive damage we need to go back just a little bit more to create the the attack prefabs remember we have this new attack target script here okay with the head with multipliers but we need to make some prefabs so let's go to the battle scene here I'm going to right click hit create empty and I'm going to rename this game object to magical attack okay we're going to add our attack target script and we're going to set a few things first of all owner is going to be left like this the attack animation is magical attack okay since this is a magic attack we're going to mark this checkbox and we're going to set a few values here mana costs 10 minimal attack is 0.4 maximal attack 1.2 minimal defense zero to maximum defense zero eight this is the magical attack I'm not going to duplicate it rename this as physical attack okay we have two game objects now magical attack and physical attack and that's where the physical attack the attack animation is physical attack it's not a magic attack since its physical mana costs zero as we're just using our fists for example zero three one zero two zero eight okay so we have the magical attack and physical attack game objects here let's go to the prefabs folder we're going to create a folder here named this attacks and we're going to put the both magic when physical attack inside here let me just open the folder both of them we need to drag one by one so magical attack physical attack let's also put damaged text and attacks here I'm going to make a new folder named uy and I'm going to put the target enemy here as well so it's organized okay so we have magical attack and physical attacks both in the attacks prefab folder we can already believe them okay we already implemented the attacks so with this we are finishing we are nearly finishing ours our tax system okay a tax they have information they have numbers they have values set on them and now we have to use these values okay so what we have to do is create two scripts we need to create the enemy unit action and then we need to create the player unit action so we are able to use these attacks let's talk about this in the next video so for the attack systems let's make the scripts that we're going to use first of all I'm going to make a folder here create folder and then I'm going to name this attack I'm going to put a tag target here and inside this folder we're going to create two scripts okay so right click create c-sharp script and let me delete this one it wasn't good creates a sharp script this is going to be enemy unit action and after that we're also going to create player unit action okay first ones here create an another script clear unit action we hit enter and both should be good all right so let's go for the enemy unit action first of all we're going to add a serialized feed here attribute and type private game object attack okay so what's going to happen here is this attack is actually an attack prefab it's something that the enemy is going to instantiate and use it to attack somebody okay we're also going to add here a serialize field and put private string targets tag since we're talking about enemies the targets tag is basically going to be the player unit okay so now we're going to use the awake method so I'm going to type avoid awake we use the awake because this is kind of a pre loading instruction and what we're going to do here is we're going to type let's make another field here let's make private no actually this is not needed never mind so in your wake method this is what we're going to type attack is going to be instantiate attack prefab okay I'm going to type here private game object attack this is going to be better okay just so you don't use the same variable for two things this is going to holding the instance of the attack and this is going to hold the prefab okay so attack is going to hold the instance of attack prefab also we're going to type attack dot and get component of type attack target dot owner is going to be gain object so it means the owner of this attack is going to be the enemy unit action now how our enemy is going to work they're basically going to get a random target along all of the targets that we have and attack that target to do this we need to make a method so I'm going to type private game object find random target and this is how it's going to work just like we did with the turn-based system we get a list of all the possible targets so that's going to be we need to find some units then type in game object opening closed brackets and then possible targets is going to be game object not find game objects with tag with the tag that we determine here targets tag okay so we have this list now what we want to do is to check if we have multiple targets here if possible targets dot length is greater than zero we're going to do something else what we're going to do is return null okay if there are no targets available we are into return null but if there are targets available we need to get a random target over the possible solutions so to do this we're gonna type return possible targets and between brackets we need to get an index it could be zero one two three it depends on the size of your party to make this independent of the size of your party you can just type here random.range first parameter is zero and the second one is possible targets dot length it's going to return a number between zero and this minus one so if we have three targets the possible outcome for this would be zero one and two if we had five targets the possible outcomes for this code would be zero one two three four which are valid numbers when you are using them too as indexes of an array okay arrays are zero based in c-sharp so if you have three elements it starts at zero anyway zero one and two okay so this is what we have here now I'm going to add a public void act this is the most important methods here so we're going to type in game object target is going to be find random target okay and after this we're going to take attack dot and get component of type attack target dot hit and then we pass target as a parameter so now finally the attack target is finally going to hit something okay it's going to hit the a random player that we have in our party okay so this is the enemy unit action now we need to do something similar to the player unit action okay so this is what we're going to do here serialize field and then private game object physical attack prefab we see realized it again private game object magical attack prefab and we also have private game object current attack okay so this is what we're going to do we're not going to implement the awake method avoid awake like this and we need to have here a private game object physical attack and private game object magical attack so physical attack is going to be instantiate physical attack prefab ok and it's going to be under transform it's going to be a child of player unit action it's a very similar thing to the magic attack magical attack is going to be instantiate magical attack prefab and transform so we set the parent now physical attack not get component of type attack target dot owner is going to be game object so it's going to be the game object of the player and same thing for magical attack we can even copy this line paste it here and type magical attack ok and for now we're going to type current attack equals to physical attack ok so we set it as physical in the beginning we are now going to implement the the act method so public void act game object target we're going to act on a target but different from the from the enemy we need to specify that target which we're going to do in another lesson and here we type current attack that get component of type attack target dot hit and we pass the target as a parameter ok so these are the unit methods that we are making we made player unit action and the enemy unit action now that we have both of them let's Adam s components of the scripts of the units that we have so in scenes we move back to the title let's save the scene just in case and for both mage unit and warrior units we're going to click Add component and then add player unit action ok and you're going to set there prefabs so we go to the prefabs folder attacks so magical attack goes here and physical attack goes here ok you do this for both units to make sure the prefabs are properly set here ok now let's get our snake again let's update it we're going to add component enemy unit action for the prefabs we're going to use the same attack prefab as we have here in physical attack and we need to set the target stack remember mage unit and warrior unit are both player unit they have this tag set here so if you go to snake unit this has to be player unit okay it has to be the exact same name that you have right now we're going to press the apply button to save the units and one more time let's update our enemy encounter snake encounter snake unit and here let's delete these snakes break the instance snake unit goes inside snake encounter we duplicate it we put it here and we press apply to save our snake encounter okay so let's delete this let's save and then now that we have done all of these things we can move on to where we define the next turn in the turn system here with but these console messages just because we didn't have any active scripts but now we can update this at the enemy round which is this one we can type current unit that get component of type enemy unit action dot act so it should hit one of our players okay let's save this let's wait for the code to compile go to unity and give this a try so we press the play button we play in the title collide with this snake and so as you see the snake attacked we had the text showing the amount of damage that was done and if you play code if you pay close attention to the to your party you're going to see that the damage animation was executed as well okay let's see that with a bit more care so I'm going to zoom in here press play and pay attention to this area okay so the warrior was hit notice that the text however stays forever here which is something that we don't want so what we have to do is we go to the unit stat here or reset the damage text and you're going to type destroy then damage text here dot game object like this and then we can specify the time it's going to be alive I'm going to put one F so one second for the text to disappear you can try that again you just go back here wait for the time to pass and you're going to press play there you go once we do this we can play a collab it's the snake again and the text is going to appear and after one second disappears okay so now the enemies are attacking but now we need to be able to attack them back of course we need to react and well select our players attack our targets and move on to finishing this game let's do this in the next lesson alright let's continue now we need to be able to select attacks okay we could start playing with the the magician then throw some magic powers into the snakes then we could play with this warrior and attack so we need to be able to select the units and play with them so we need to do a little bit of an improvement in our player party in the title scene what we're going to need is to make a new script so we right-click choose create c-sharp script and we're going to name this select unit let's hit enter let's wait for the code to compile and continue we're going to select our player party attach this red here okay so we have to start battle we also have select unit and continue working here just like with start battle we need to check what scene we're currently in so we're going to use the same management using unity engine dot scene management what's going to happen is this this select unity script is going to to need to be able to identify the actions menu and the enemy units menu but these things only exist in the battle scene so we're going to make a little check here okay so let's declare a few variables private and game object current unit as the name says it's going to determine which player unit is currently active we also are going to type private game object actions menu and private game object enemy units menu okay we're going to use these three variables here first of all that's it let's remove this for now we're going to use avoid awake to identify if a scene has been loaded scene manager dot sing loaded plus equals on scene loaded we're going to implement that method so private avoid on scene loaded we need the first parameter s scene scene the second one load scene mode mode okay so here we need to check if the scene is the battle one because if it is we can define who is the Action menu and who is the enemy units minute so if st. dot name equals to the string battle like this then actions menu is going to be game object dot find with the string Actions menu and enemy units menu is going to be game object fight enemy units menu these need to be exactly the same names as the ones we have in the battle scene but if you follow this tutorial with the same names it should work just fine now we're going to implement public void select current unit and this is going to receive a game object unit okay so when we call this we're going to set what is the current unit so we need to set current unit to be unit which was passed as a parameter here and when we select a unit and this is very important we need to activate the Actions menu so we can determine if we're going to attack to apply a magic attack or to run so actions menu dot set active true now we also need a public void select ok we already select the unit we should be able to also select an attack ok so here I'm going to put full physical we're going to determine if if it's going to be a physical attack or a magical one okay so when this happens we're going to type current unit don't get component of type player unit action okay we're going to get this component and then select attack and we're going to pass physical as a parameter as usual this is a method that still needs to be implemented but we're going to get there it's just to determine the current attack if you remember if you go back to play unit action weird affording it to be physical but what we need to do here is type public void select attack then boom physical and when we do this we need to type current attack equals to is it physical if it is then we return a physical attack if it isn't then we return magical attack okay we use this ternary operator to set the current attack very simply like this okay so select the text done back here select the Tech has returned to normal okay so this is good then when we select the attack we need to type actions menu dot set active and we determined this as false because we already selected the attack and then enemy units menu dot set active true because we need to select the the enemy we want to hit and finally and this is the last step when we are selecting the who the unit is going to attack what attack is going to be used we're going to type public void attack enemy target and then game object target notice that reading these three elements is like telling a story we first select who which player is going to attack then the player is going to determine their attack of its physical or magical and then they're effectively going to attack okay it's a three-step process to to make the player interact with the game okay now here we type actions menu dot set active false enemy units menu dot set active false as well and finally current units don't get component Tagg player unit action dot act over the target that the player selected okay this is basically what we need for this select unit script okay and now that we have this complete we can move on to let me check here let me open unity let's go to scripts we're going to open the turn system here okay let's wait a little bit for it to be done we open the turn system good and here instead of making this check here the certification we're going to type player party which we need a reference to as well dot get component of type select unit like this dot select current unit and that's like current unit is going to be current unit dot game object so we're probably selecting the current units in the player party but we also need a reference to this player party we can do here game object dot find player party it could work like this okay okay mantra dot fire player party we call the select current unit and we pass the current unit here remember this current unit is what we get from the list of units stats since we are here it means that unit is a player unit that's why this is going to work correctly okay now to select the attacks however we need to find a way to make a communication between the buttons that we have in the battle scene which are these three and the players that we have the problem is they don't have their own click methods yet defined but and they also need to be defined dynamically because we know we still don't have a player party here okay so we're going to make a new script here so let's go to scripts I'm going to right click choose create c-sharp script and this is going to be add button call back okay we're going to double click to open the script and add just one serializable field which is going to be physical private boo physical okay in the start method we're going to type game object dot get component of type button okay not add button call back just button you should add the namespace here as well using a unity engine that you I okay there we go we're going to get the button we're going to type on click and then dot add listener just like we did with the enemy units in the interface here in adlistener we open and close parentheses equal sign greater than and then we call the method add callback which we're going to define right now okay this is just us using this method to be called whenever we click the button ok and here instead of the devoid we're going to type private void add callback callback like this good and here we're going to type game object player party equals to game object dot find a game object with the name player party okay and then we type player party the get component of type select unit dot select attack and finally this is going to complete the link between clicking in the button and actually selecting the attack now we pass physical which is determined here okay and now that we do this we need to update the physical attack action in the magic attack action so let's go to unity let's wait for this to compile and once this is done we're going to select the HUD canvas here then we're going to select the Actions menu and for the physical attack action we're going to click Add component we're going to add the add button callback script and mark physical for this one and in magic attack action we're going to click Add component Add button callback and we don't need to add physical ok this is what we need to be able to click these two buttons and from the attack now we need to move back a few steps - to the enemy buttons that we created that we're here okay so just remind you here in the prefabs folder in you why we have this target enemy script okay to create this here with a data another script before which is this create enemy menu items and here we made a method called select enemy target which is sad as doing nothing right now okay because we still couldn't perform the entire attack logic but now that we can now that we've implemented the methods of this lesson here in the create enemy menu item script we can type game object party data equals to game object dot find player party and then party data dot get component of type select units dot attack enemy target and when we reach this we type game object so we're going to attack this enemy here okay so this should be enough for being able to attack the enemy now that we have this logic working we should implement the changes to update the HUD okay so we can update the health points we can update the players face so let's do this in the next lesson to update the herd elements that we have here we're going to implement an interesting abstract system so in the scripts folder I'm going to create a new folder called UI and inside the UI folder we're going to right click choose create c-sharp script and name this show unit stat okay I'm going to open this and this show unit stat here is going to be in public abstract class because this is not going to be used directly we're going to extend this class for when we want to show the unit's health and the unit's mana so this is what we're going to need to do okay so first of all we're going to serialize a feud here and this is going to be a private actually uh protected in this case game object units okay so we serialize feud again and private float Max value and we also use private vector to initial-scale okay we use this to know the initial scale and then we can change the scale of the bars that we are using in the start method initial scale is going to be game object dot transform not local scale we're going to save this information in this method in the update method we're going to type if unit is different than null if we have a unit a kind of related related to this show unit stat script okay so if we're showing player one or play - it doesn't matter we're going to call float new value is going to be new stat value which is a method and abstract method that we're going to define sim float new scale and then we're going to do a very simple calculation here between parentheses we're going to type initial scale dot X multiplied my new value divided by max value okay so this is for the new scale and finally to actually do to do change the scale we're going to type game object dot transform the local scale is going to be a new vector2 and finally we're going to put here new scale on X an initial scale dot why on why okay so we just changed the scale of the bar horizontally just like I told you before we don't change anything vertically all right and the interesting thing here is that this new scale is going to in turn kind of a value between zero and one between a hundred percent which is the full bar and zero percent with which is an empty bar okay so let's move on we're going to need other things here we're now going to type public void change unit and type game object new unit here once we do this we're going to type unit equals two new unit this method is very important because this is what is going to determine which players data is going to be to be shown if it's the Warriors data or if it's the mages data and finally we type abstract protected float new stat value okay this is going to be calculated and this is going to be implemented in the specific show unit stat scripts and I'm going to show that to you now okay we're going to unity let's wait for the code to be ready and we're going to make two scripts here okay the first one I'm going to create the Sharpe script is going to be show unit health so this show unit health is going to extend show units that but this script is going to be much shorter because we're just going to access the unit's health we do the same thing for the manner so show unit mana we open both scripts health and mana both of them are going to extend show unit stats okay actually show unit stat just one in singular like this this one show unit stat okay both of them are extending show unit stat and all that we have to do here in the health part is going to be override new stat value if you if you type override space and then hit enter we already have what we need to type for the new stat value implementation protected override float new stat value and here we're going to type returned unit dot and get component of type unit stats dot health to access the health I'm going to copy this method implementation here save the script go to mana and I'm going to paste it here but instead of returning the health we're going to return the mana okay so this is what we need to make these two things work now that we have these set we need to go to the health and the mana bars which should be here in player unit information and add the script of course and the health bar we're going to add show unit health there we go max value let's set this to 100 and let's define that as the maximal one for the mana bar again show unit mana but this time maximum value is going to be 100 units now when this happens we need to set the unit's properly okay so we need to go back to the Select unit script here okay and we have to select a current unit script right and when this happens we're going to type current unit dot getcomponent of type player unity unit action okay and we're going to call a method that we're going to implement right now which is update hut okay it's going to be read because it doesn't exist yet but we're going to our player unit action script to implement this okay which is here all right so here we're going to do two things we're going to add a new field so we serialize a new field okay I'm going to make a new one then we're going to type sprite face sprite in the player unit ActionScript which is going to be updated soon and finally here we're going to implement public void update Hut so inside this we're going to make three things update the bars and then update the players unit face so here in update hub game object player unit face is going to be game object not find player unit face which is present in the battle Singlish this exact name and then play unit face that get component of type image okay trick just image here since we're going to deal with you why we need to use unity engine dot UI here at the top of the script okay here dot sprite and this is going to be face sprite which is going to be determined on every player unit to update the health bar game object player unit health bar is going to be game object dot find player unit health bar should be this exact name here and then player unit health bar dot get component of type show unit health this one dot change unit and we pass game object as a parameter and for the mono one is going to be almost the same thing game object player unit mana bar is going to be key mantra dot find player unit and a bar okay and now player unit mana bar dot get component of type show unit mana dot change unit game object okay so it's going to show the correct data all right now once we have this we're going back to unity and we need to go to our title scene to update our player units ok wait a little bit we go to scenes then title and in the player party notice that we have see here in the player unit action we have the face sprite for so for the mage unit the face sprite is going to be made let me see here mage underline face so we set it here and the warrior unit face pride is going to be warrior underline face which is this one so let's save this same to make sure everything is going to be executing properly and after this we need to implement the logic for finishing the battle okay because when we are able to finish the battle or after this we should implement we should finish finish the adjustments for making the entire turn logic work okay it's not going to work entirely right now what we're getting there okay so let's work on finishing the battle all right so there are three conditions to finish the battle these conditions are if all enemy units are dead the player won if all player units said that then the player lost or the player might run from the battle we need to start with when the player wins the battle when this happens we need to collect the reward so we need to make a script for this I'm going to right click in the scripts folder create c-sharp scripts and name this collect reward we're going to open this collect reward script and add a few things first of all we add serialized field to add a private float experience okay whenever you destroy an enemy encounter you're going to receive a certain amount of experience points okay in the start method this is what we're going to do we're going to type game object turn system is going to be game object dot find a game object with the name turn system written exactly like this now turn system dot get component of type turn system dot enemy encounter which we're going to set is going to be game object like this now we go to the turn system and we're going to add here a public game object enemy encounter let's see if it like this okay so back to collect reward everything looks back to normal here we're going to add a collect reward method we're not going to use the update method okay so let's type public void collect reward like this now game object array living player units is going to be game object dot find objects with tag and we're going to pass the tag here player unit we want to know how many remaining players are going to be here because this is going to depend well on how many if you if you have many players alive we're going to have more experience if you have less players then that's going to be less experience so we're going to type here float experience per unit is going to be experienced divided by living player units dot length okay so this is what's going to be the calculation for that experience per unit let me just check here what is this Marcus red yes actually the Colette reward script has the same name here so let me name this get reward should be enough I think this method get reward okay so now we calculate the experience per unit that means that we're going to distribute the the total experience of this enemy encounter between every enemy so between every player so if an encounter is going to give you a hundred experience points and you have to live in units each unit is going to receive fifty experience points okay so for each game object player unit in living player units we're going to type player unit but get component of type unit stats dot receive experience and we're going to pass experience per unit unit as a parameter like this okay and after we call this get reward we should also destroy the game logic itself since we are trying to use this method here we need to go to unit stat here so we'd go to the declaration this unit stats is going to have an amount of experience points so I'm going to add here a public float current experience or just experience here and in the end of the unit stat script I'm going to type public void receive experience and then here float new experience and now we can type experience plus equals new experience and this you can use later to implement some sort of level system for each of these units it's good to have this ready now to finish the implementation of realizing that the player won or lost the game we need to do some changes we need to open our turn system here okay so whenever we call next turn in this place here before we move forward we have to check a few things okay we want to know how many enemy units are left so I'm going to type game object array remaining enemy units is going to be game object don't find game objects with tag and here we're going to type enemy unit remember whenever an enemy or a player is that their tag is replaced with dead units so this is not going to be found here so we're going to ask if remaining enemy units dot length equals to zero it means we have successfully killed all enemies so we need to collect the reward so enemy encounter that get component of type collect reward dot and then here we're going to type collect reward which we renamed to get reward actually okay and now if we beat a level we're going to move back to the town so scene manager not load scene and we pass town as a parameter since we are trying to switch scenes we also need here to using Unity engine dot scene management okay so this is the rare occasion to check if we beat all enemies we also need to check if we were completely destroyed so check if game over game object array remaining and then lesson this player units is going to be game object dot five game objects with tag and then it's going to be player units to get all the the live players now if remaining player units dot length equals to zero that means we have lost the game so we're going back to the title scene scene manager dot load scene to the title scene like this and now the other way to exit from a battle to two well basically run from it we need to implement another script so we nuna team we're going to wait for the code to compile here just to make sure everything is going to be updated and now we're going to right-click choose create c-sharp script and name this run from battle okay I'm going to double click this and here we're going to add a field serialize field and then private float running chance ok so there's going to be a certain chance that you can run from the battle if you don't run then you're basically going to lose your turn and get hit again so you have to really think if you really want to try to run from a battle let's set this as 0.5 by the phone okay so here we're just going to add one method which is going to be public void try running and this is how it's going to look like we're going to type float random number is going to be random dot value this is going to return a random value between zero and one it's like getting a value between zero and a hundred percent and what we're going to know is if that random number is lesser than running chance if it's smaller than running chance then it means we successfully ran away so seam manager dot load seen town so we go back to town we also include the unity engine dot scene manager name space here okay else that means we just lost a round we weren't lucky enough to get away from this battle so we need to get our turn system by tapping game object not find turn system dot get component of type turn system and then we call the next turn okay so basically we just lost our chance to define okay so now we're going to save this go to unity wait for it to compile and now we're going to put everything together to make this game work so we're going to do this in the next video all right so now that all these changes have been implemented we could try to play the game if we press play here in unity then press play try to beat the snake we are attacked but we still can't act here the problem is we need to call next turn after something happens and we should do this whenever we receive damage because it means the players or the enemies turn has finished but when this happens we also have to wait for a few seconds before moving to the next turn so what we can do here at the bottom of the receive damage method is type game object dot find turn system dot get component of type turn system again dot wait then next turn okay this is a method that doesn't exist so in the turn system here at the bottom I'm going to add public void wait then next turn here we need to call a core routine so we are able to wait for time to pass before doing something else so I'm going to type here start call routine wait then next turn routine and finally our last method is going to be private eye a numerator wait then next turn routine and here we type yield return new wait for seconds to wait for one second okay so we pass 1.0 half as a parameter and then we call next turn like this okay so we're going to save this let's go to unity wait for the compilation to be done in after that's complete we are able to press the play button here we can press play and we are attacked after once when I have second passes one second passes we select the warrior now we can choose an attack I'm going to choose this one and attack this snake I killed it this tagged now I'm going to play with the mage magical attack select the snake and we're able to try to destroy it okay so we get it and there are two little things we still have to do okay first of all we're still not able to run because we didn't implement this in the last lesson so let's go back to the battle here I'm basically going to select the run action add component I'm going to add the run from battle script here and in the run action we're going to add a button callback to itself so we just in the unclick panel we click on the plus sign to add a new one we drag the run action itself here and the callback is going to be run from battle and then we're going to call the method which should be here try running okay so this should be enough to well run away from the battle so let's implement that first before moving on to the collect reward Majak so we're going to play here press play come here go to the snake we are attacked if we try to run notice that I wasn't lucky enough I'm going to try again and this time I was lucky enough okay and notice that we are still being hit the health points are being decreased okay you can see them being updated the warrior has been hit a few times so we can see health points getting diminishing over time okay so that part is done but the only thing that's left now is whenever we try to collect the reward a bug is happening here so let's kill these snakes we got one let's get the second one there we go and if you see the console it says here the variable enemy encounter of turn system has not been assigned if you double click here a model develop should show you exactly what the problem is but if it doesn't you can just check here turn system line 48 so in turn system line 48 here it's trying to use enemy encounter but it's not working correctly okay so we need to find out why this is happening let's take a look on this so what we need to do here is to look at our prefab let's go to project we're going to prefabs enemy encounter the only thing that's missing is of course add the collect reward script we added here let's say it's going to give 100 experience points okay now let's save doesn't give one let's try we're going to press play okay we can play unity here let's go to the snake pearly still another problem missing let's check what's here we just have a problem with the current unit stats in the enemies so let's try to move on here it's C okay so apparently we have another problem here let's try that again let's see if the battle scene looks good enough yes go back to the title press play let's try another battle so apparently it worked now okay so let's try again we're going to press the attack button to kill that snake we're going to kill the other one give this one so the time passes okay we're able to destroy everyone and then kill them and if you look at your player units you can see that they have received 50 experience points each okay this happened because the enemy encounter had a hundred experience points and it divided into two different players okay so we can kill the snakes again and receive fifty more experience points and this happens both units are going to be holding a hundred experience points so the point here is to show you that the experience points amount is saved over time okay because this player party can't be destroyed on load okay so this should be it for this lesson okay I hope you enjoyed this tutorial you have we have covered lots of aspects here okay we've talked about you why we've talked about players and enemies and what you can do is implement other enemies if you look at the sprites folder in battle you're going to see there are lots of things you can add bats dinos dragons and they could be much stronger enemies ok reptile slimes so there are many many many options for you to choose here implement new things and also another interesting thing to implement is new Maps okay the town that we have here is just one scene but you can make other scenes to further improve this game and have more challenges you can purchase weapons this is the basic building blocks for you to implement and make this game something huge ok so this is it thank you very much for watching my name is glauco pedis and i see you next time
Info
Channel: Zenva
Views: 55,496
Rating: undefined out of 5
Keywords: zenva, zenva lounge, tutorial, programming, coding, software development, web development, online course, unity, rpg, hours, game engine, unity tutorial, full course, complete course, complete, 2d
Id: e4i0mtbvJn4
Channel Id: undefined
Length: 239min 21sec (14361 seconds)
Published: Tue Jul 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.