Making enemy AI detect the player in Unity 2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when creating even a simple ai for your game you usually want to have a way to detect different objects of interests like obstacles enemies or the player what we need to have is a way to sense or detect whatever is of interest to us the best way to approach creating those from the standpoint of architecture of your game is to have a separate sensor object all we need to have is a bull flag something like player detected that will inform us if we indeed have detected a player or not and a transform reference or a game object reference called for example target to direct our player or our enemy to move in the direction of the target now to use this sort of a sensor class we would need to have something like ai brain class now this ai brain would be a high level class that would control the enemy actions like moving or shooting or whatever you need to have and this would have access to our ai sensors so that we can reuse our ai sensors for different enemies or for different implementations depending on what we want to have in our game but basically this gives us this modular approach of creating our ai now in this video of course we are going to focus on creating our ai sensors one example would be a simple box collider that is a trigger that would detect when our boss enemy should approach or should start walking towards the player and this should only happen when our player is in range here where our boss enemy can reach our player so if i switch to game view if i jump here our boss enemy will start attacking and charging at the player because it knows that the player is here in this range and here is the player in area is said to be true and to detect the player i'm using a tag comparison so here is the script a player enter area detector and what we have here is a bull flag that we can access this is a public property that is privately set and a player trials from reference that we can also access now i'm using a tag to compare when we call on trigger enter 2d and ontrigger.exit2d because we are using the default box collider 2d that is a trigger and we are going to check if the collision compare tag is of type detection tag and only then i can set the bull flag to be true and the player transformer friends to be collision game object the transform and this is one downside of this approach because we are going to basically detect all the collisions and every time there is a collision we are going to check the comparator tag of the objects that are colliding and on exit we would simply set the player in area to false and the transform reference to be now while we can provide a tag here this was specifically created to detect if player enters the area to make our script more reusable we can use the same box collider that is a trigger but instead of calling specific functionality or saving the player transform or is the player detected we can expose unity events so for example on trigger enter event and on trigger exit event that will call specific code that we want to call whenever for example the player using the same method comparing the target tag when the player enters this collider we want to invoke all of those methods and let's see the script so the script itself will be simply called ontrigger2dutil and of course this would work for 3d as well and instead of exposing here a bull and a transform or a game object we're going to have a public unity event and this is inside a used unity engine.events library and this will simply create a in the inspector a window where we can assign a game object and call on it a public method and this will basically be called whenever something triggers that collider or when something exits the collider and basically this makes it the script more reusable for example if you want to create some sort of logic to open the doors whenever the player is closed or to make something appear now still we have one more issue that this is pretty inefficient because we keep those colliders on all the time but our player will not constantly walk in and out of this collider it will happen probably once on uh during the game or something like this so to how can we make this code more efficient well instead of using a collider to do the task of detecting our player we can recast a box cast or overlap boxcast to detect our player and we can specify how often we want to boxcast this so in our case when i enter this area this will turn red and our enemy will start attacking us and it will rotate towards the player so basically this contains a reference to a player as well as it knows if the player is in range or not in both physics and physics 2d libraries we have those box cast and overlap box methods and in case of physics 2d i am using this overlapper box method now this allows us to create a box basically a box collider but only temporarily defining its point so this is the origin a size an angle as well as the layer mask so instead of comparing a tag we can provide a layer mask and we can ensure that our boxcast or overlapper box will only detect the colliders on our specific layer as well as it can work with z-axis in a 2d environment and this can be very useful when you want to detect if something is underneath or above a collider now the code implementation is a bit longer and this is because to cast a boxcast we need to predefine a lot of parameters that for the box collider today we can define in the inspector we have our bull flag player detected and the vector 2 direction to target that we can calculate by getting the reference to a target that is here the game object target and beside that we can use a coroutine to simply perform the detection depending on a detection delay so basically we can detect our player or whatever we want to detect in an interval instead of constantly so this can boost the performance of our ai system detecting every 0.3 of a second so we will start the co routine star detection yield return a new wait for seconds detection delay and we can call perform detection and after we do we can restart our core routine and this perform detection will do basically the same thing that we do with our box collider we are going to define a center point where we should call create our overlap box and this will be the origin point of the game object plus the detector origin offset and we are going to provide the detector size so this will be the box size the angle and the detector layer mask and what we can do is simply check if collider is not equal to null we can set the target to be this game object else we can set it to be null now as i already said one downside is that we need to define all of those vector twos like the offset and the size inside our class as well as we will need to have a gizmo to show our result so what where is this box cast or overlap box being casted so this defines a idle color and detected color and show gizmos in case we do not want to show our gizmo and we are going to simply need to call private void android gizmo method that allows us to draw gizmos and basically what i do is check if i want to show gizmos and if the detector origin is not null and in this case i want to set the color of the gizmo depending of if the player is detected or not and i want to call gizmo the draw cube so this is a bit more work but as a result of course we can create this kind of mechanic that debugs us if the player is detected or not and this is really useful when you do not detect the player when the enemy is sending idle this way we know that the detector is working correctly and there is something wrong with our ai and not the detector itself okay so this is great we have our detector what would be the example of using those detectors because we of course need to have some ai structure that will make use of those detectors so an example would be a simple top-down game where we have our enemy and when we press play the enemy will not see us because we are behind the obstacle but when it sees us it starts to chase us now in addition to that we have a smaller detector to detect if we are in the range of attack so if we stand still and the detector reaches our player we will get into the attack range and we are going to get attacked now if we run away from our enemy now we are not being chased because we are outside of the detection range of our enemy now another way to implement those detectors is by attaching one to a enemy now this is a patrolling enemy and this is a detector this circle cast i think or overlap circle and basically whenever it detects that i am in range it will perform an attack and will continue walking so this is a very simple ai system but this allows this enemy to decide if you are in range of the attack so if we are too far there is no point in attacking us but if we are close enough then it can perform the attack and play the attack animation and this detector simply has a target layer to detect instead of using a target uses a layer and has on player detected event that we can call whenever we have detected our layer and let's see the script here and basically this is very uh similar to what we have previously all we have here is the radius instead of a size and we are calling overlap circle method to cast a circle cast and again we need to implement a gizmo to show whether we are colliding or to show the gizmo itself so that we know how to move it or how to change the radius to place our detector and inside the game itself we can see that our detector is simply a game object that we can move around and we can move it and this is a child of a an enemy so we can move it outside and as you can see it is further so now we do not have a need to add an offset to the script instead we are simply changing the position of the game object itself and discussing from the center of the game object the overlap circle cast great thanks for watching i hope this video gave you some ideas on how you can implement player detection system in your game if you want to learn more about ai and how to develop 2d games in unity you can check out my video courses the link will be in the description of this video if you want to see more ai related content on my youtube channel let me know down in the comments take care
Info
Channel: Sunny Valley Studio
Views: 35,353
Rating: undefined out of 5
Keywords: unity ai detect player, unity ai sensor, unity enemy ai tutorial, unity ai, unity simple ai enemy ai 2d, how to ai unity, C# ai 2d, unity ai coroutine, unity overlapbox, unity boxcast, unity overlapcircle, unityevent, unityevents, unity 2d ai, unity artificial intelligence tutorial, unity raycast ai, gamedev, indiedev, game development, indie game development, sunnyvalleystudio
Id: uOobLo2y3KI
Channel Id: undefined
Length: 12min 4sec (724 seconds)
Published: Tue Sep 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.