Find closest enemy - Unity Tutorial - Find gameobject unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to find the closest enemy game object in unity hello and welcome this is what you're gonna learn in this video I show you what we got here is the player and two enemies the closest enemy is getting marked with a yellow color and then I can attack the enemy and it will die this is what you will learn make sure to watch till the end to see some extra performance tips let's get started alright guys this is it we are in the game project and first of all I want to show you my enemies I got them here and if I focus on one enemy you see them there and we are going to use enemy find enemy attack and for that it's very important that your enemies have a tech defined up here you can see it there I got two tech enemies if you don't have an any tag for your enemies you can easily go here say attack and then you can add a custom new tag here and you can name it like you want so make sure all your enemies have these tags and then we move on and go to the player here is my player object and I'm gonna add a empty in-game object here down there and I will call it enemy radar so with that I will go down here in my script folders and I will create a new C shop script and I will call it enemy radar let's open it up but don't forget to grab the script and add it to the enemy radar anti game object so enemy radar there it is and then edit script open it up in Visual Studio there we go so first of all we need some variables and one thing we need is a private array and it's gonna be a private game object an array of the type a game object which will be defined like this and I will call it multiple enemies because I'm gonna store all the enemies in there so I'm gonna need some more variables and one of this will be a public transform remember the transform component of an a game object is holding all the informations where this game object is so it's gonna be a public transform and have a college closest enemy there we go and I also want to have a public pool and have a college enemy contact so here we go all right we got this and on the stop method I will say closest enemy well that was wrong closest enemy equals null and enemy contact is false alright so next we're going to define the method which will find our enemies and when we are searching for the enemies we want to have the transform information of the closest enemy that's what we want to have in return so we're gonna say public transform and have a close I will call it get closest enemy so there we go now the most important line is the following line multiple enemies this is our array we got up here equals two and here comes the methods game option dot find in game object with TAC and it's going to be the tag enemies remember this is the TEC we sat in the editor if you're not sure about your taggers go back here go to your enemy check it what is my tag and then you enter it here okay so this will find all of the enemy instances and we'll store it in the multiple enemies array so then we need a few more things things in here so what we got right here I will explain it we defined all our stuff we need up here we got our multiple enemies this is the part where we find all the enemies then we got a float which will hold the closest distance and we set it with dysfunction to math after the infinity which is the biggest possible value you can get for a float and then we have a transform defined right here it is called trance and we set it to null so it's empty and then we go here we say an on for each loop for each game object that is stored in our multiple enemies array remember that's the array where we stored all the enemies we could find in the scene for each of this in-game objects we go we will go through all of this and what is it doing we have a float current distance and current distance is vector three distance vector three the distance is calculating a distance between two given points and a one given point is the transform position of our own player and the position of the found enemy in our multiple enemies array so and then we say if current distance which is the distance to the current enemy we are processing if it is smaller than the closest distance remember we set this to math F infinity with this which is a huge value then we will say on the closest distance is equals to the current distance and then we also set the transform we defined up here to the transform of the current processed enemy instance and then we simply return the transform so this for each loop goes through all the enemy objects and it will end with the will return the enemy that is closest to the player and that's what we got here so awesome then in order to make it work we simply have to go into our update method and then we say closest enemy equals to get closest enemy and then I want to also want to make the closest enemy visible and for that I will add another thing here and I will say closest enemy that game object that gets component and I want to have the sprite renderer of this component there we go and I want to access the material and the color of the material and I've set it to a new color with the value of let's see new color I will define a new color here and I will give it some custom values in order to have some yellowish color there is our color all right then let's test this there we go and you can already see the closest enemy is marked it has this yellow color while the other one is not yellow and if I get closer to this one then two dead you can see it is also marked and here you got the same thing so it is actually working you see it's not unmarked in them but we will take care of that later so let's see what we can do more with that so you might have noticed that the closest enemy is always getting tracked no matter what distance I have it can be far far away and it is also tracked and now I want to have a radar distance so that only within this radar distance the enemy gets tracked so let's do this for the enemy radar distance we need a few more components on our enemy radar game object right here and what we need is a circle Collider 2d and I will set it to is trigger that's important otherwise it would not work and we set a rigidbody 2d that's also important set it to simulate it but don't leave it on dynamic very important put it to kinematic and leave the default values right here and then you can get into your radius of the collider and you can see here that the radius distance is getting greater and now you can set the radius to the distance you want to have tracking down enemies that all the enemies within that circle are getting tracked actually so cool we got this now we need to make some changes to a code in order to make the enemy radar distance work we're going to use a on trigger stay to the function so what I'm saying here is we're checking if the collision is not a trigger because we don't want to collide with triggers only with real game objects and I compare the collision that is happening and I'm checking if the tech of the colliding game object is an enemy so now we can take this line of code we got up here in our update function and we can simply paste it down here and voila so what we can also do right here is now we can make use of our boolean if we have enemy contact it's up here on boolean enemy contact that might be useful for cases where you want to check outside the enemy radar class if you are in contact with any player any enemy object and so I will set enemy object equals true what we also can use them is the on trigger exit function in order to detect when the enemy will leave the radios so what I'm doing in here is I'm checking again is it a play is it an enemy and if yes it is an enemy that is leaving our trigger then I say enemy contact equals false and also I grabbed it this enemy that is leaving and I say game object dot a get component sprite renderer material color and I set a color to white so I remove the yellow color that is marking this enemy as tracked down okay ladies and gentlemen let's test this go back into the game hit start you can see the collider right here we are out of range and now we get in range and you see the enemy is marked with the yellow color and if you get closer to this one this one gets marked also when I go away you can see that they stay marked so this one is getting white this one over there not so we have to add some code to fix that and also I want to have a damage function so let's jump right back into the code and under update we will add another function a attack function so what this does is I'm checking if we have enemy come to contact and if that is true I get the closest enemy I grab the component enemy AI which is my enemy script and on the script enemy AI I got a function enemy damage where I can deliver damage to the enemy and I will deliver to damage points and in order to actually trigger that code we have to add some code to our update function and that's pretty simple I say if input that gets key down and we say key code dot Mouse zero which is the left mouse button not any key down but get key down that's why I want a half all right and then simply call the attack function okay now I will add two more lines of code in order to change the colors when I get out of range all right there we have it I added here if you already got a closest enemy in our variable then I will set the color of that closest enemy to white which will remove the yellow effect and also underneath here when a level gets reloaded or this object is destroyed I also grab the closest enemy and I set the color back to white so let's test go back into unity hit play and yes here we go now the player gets marked if I get away he's unmarked I get closer here the other one gets marked now I can attack and you see it's getting damaged the other one getting damaged too is dead yeah so it is working what I also want to say at the end I told you this Collider thing is a really performance boost because the search for all the enemies that are around here in the scene will only happen if an enemy enters this collider area and so this is really boosting performance because these finding operations can be really expensive in in order to the CPU power and that will save you a lot of trouble so it only gets calculated and only get find the enemies when it's needed and you can easily get here and adjust in the radios where you want to search and detect enemies so that's all for this tutorial if you found it helpful consider subscribing there will be more tutorials on my channel I really enjoyed to do this here I hope you do don't forget to Like and subscribe and if you want you can check out this game I'm developing right here this game is called oh my zombie you can find a link in the description to my side to my block you can download your free demo and have fun with it I'm also glad if you give me some feedback to that that's always highly appreciated so folks stay safe stay healthy stay awesome and I see you in the next video
Info
Channel: Indie Games Dev
Views: 4,846
Rating: undefined out of 5
Keywords: unity find gameobject with tag, unity find nearest object, unity find closest enemy, unity find gameobject, how to find nearest gameobject in unity, find closest enemy unity, find closest object unity, find nearest object unity, find nearest gameobject unity, unity closest object, unity find closest object in array, unity tutorial nearest object, unity tutorial closest object, how to get closest gameobject in unity, how to get nearest object in unity, find gameobject unity
Id: VH-bUST_w0o
Channel Id: undefined
Length: 19min 39sec (1179 seconds)
Published: Sat Apr 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.