ENEMY AI - Making an RPG in Unity (E10)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Add a Nav Mesh Agent to your PlayerController. It needs to be something that moves in your scene with your movements. Add an empty GameObject. Add the PlayerManger script made in this video to the new empty GameObject. Add the PlayerController in the PlayerManger script you just added through the Inspector.

Enemy stuff just keep same as in video.

I am newbie. Hoped this helped someone with basic AI in VR.

👍︎︎ 1 👤︎︎ u/PartyCurious 📅︎︎ Sep 26 2021 🗫︎ replies
Captions
in this video we're gonna be making the AI for enemy let's get into it so let's begin by creating an object for our enemy let's right-click in the hierarchy go 3d object and then select capsule let's go to the scene you that's right click on the transform and hit reset let's move this over to near the end of the bridge now I don't want the graphics to sit on this object so let's just remove the mesh renderer and the mesh filter and it's instead right click go 3d object and then cube and this will be our temporary graphics so we can remove the collider on this one we can bump up the y scale to 2 and down the scale on the x and z to something like point 6 so now it has somewhat the dimensions of a character let's rename this object to graphics I rename the parent object to enemy we'll also open up the capsule Collider here let's change the radius to something like point three and we'll leave the height add two let's also select our graphics object and move it up by say one on the Y just to make sure that the pivot of our parent object will be at the bottom and let's now also offset our capsule Collider the same way cool we then want to go ahead and add our nav mesh agent let's just move this to the top here let's collapse the capsule Collider and open up the nav mesh agent and you can see now the collisions cylinder that it creates here we want to set the speed to something like 2 the acceleration bump that up to something around 20 let's set the stopping distance to 2 as well and we can set the radius for obstacle avoidance to around save 0.2 if you're confused about what a lot of this stuff does I recommend watching the first video in this series let's now collapse this as well and let's finally add a rigidbody on top here we just want to make sure to check is kinematic and our enemy has all of the necessary components in order for us to apply movement on top so let's just drag him down to stand somewhat on the ground let's then go under our scripts let's create a new folder here let's call this controllers and yet we can put all of our controller scripts so let's take our camera controller I'll play your controller and let's also take our player motor since these are connected let's drag them onto the folder let's right click hit create C sharp script and now we can add an enemy controller and swap let's make sure to drag this onto our enemy object and let's open it up in visual studio now the first thing that we want to do here is to define a look radius because we only want enemies to chase and attack us if we're within a certain range let's create a public float and let's call it look radius I'm going just to fold this to ten let's also make sure to display this in the editor by going void on draw gizmos selected and yet we'll set the color of the gifts mode to colored red and we then go gizmos dot draw wire sphere in order to draw a sphere around our current position with the radius of look radius so if we save this we should now be able to visually see the look radius of our enemy in the editor so in this case if we were to move on to the bridge we'd be detected by the enemy of course in order for our enemy to check whether or not we're within range we need a reference from our enemy to our player to do that let's go in here and create a transform target and we'll also need a reference to a nav mesh agent in order to move our enemy remember whenever we're using AI inside of unity we have to include the unity engine AI namespace and now we can create a reference to our nav mesh agent let's just call it agent we can set these references in the start method we inside agent equal to get component of type nav mesh agent to find the component on our game object but how do we find our player well of course we could use game object at find object with tag but then we'd have to search through all the objects in our scene and we don't want to go through and manually reference our player for every enemy instead let's set up a script that will always keep track of a player to do that let's right click under scripts go create c-sharp script and we'll call this the player manager and we can just have this sit under our game manager let's open up the script and the first thing we want to do is create a simple singleton pattern we'll do this in the exact same way that we've done it before and as always if you want to learn more about Singleton's there's a link in the description so I'm just gonna wrap this in a region called singleton I'm gonna create a public static player manager called instance and then inside of the awake method I'm gonna set instance equal to this now we can collapse this and we can create here a public game object which will reference our higher so if we save this we can just go into unity and drag our player object into this slot we're doing it this way because we are not currently instantiating the player at runtime however many games spawn in the player while running if that's the case just make sure to update this variable to point to the player you spawn now we can really easily go into our enemy controller and set target equal to our player manager dot instance dot player and of course this is of type game objects that we have to convert it to a transform then the update method we can get the distance from our enemy to a player can create a float called distance and set it equal to vector 3 the distance between our targets position and our precision we then check if distance is less than or equal to our look radius well then we want to start chasing the player so we can use agent dot set destination and feed it our targets of position if we now save this and go into unity let's take our interactable on the bridge here and disable it let's make sure we can see both the game and the scene view let's select our enemy and hit play and you can see now that if we get closer to our enemy it starts to chase us awesome one thing that you'll also notice is that if it gets close enough in order to stop and we then run around it it's not gonna update its rotation so it won't be pointing towards us unless we move away now this might be fine as long as we are using a white cube but when we start using an actual skeleton that is going to try and attack us we don't want it to attack out in thin air to change this we use the same method we did for a player when pointing towards interactable that is first off checking if the distance is less than or equal to the stopping distance of our agent if it is well then we actually want to go ahead and attack the target and we also want to make sure to face the target will be handling attacking in the next video but we can at least make sure to face the target to do that we'll create a method called face target and we did this exact same thing in video number two on interaction so I'm just gonna quickly do it again here first we get a direction to the target we then get a rotation where we point to that target and we then update our own rotation to point in this direction but it's probably a good idea to apply a bit of smoothing here so we'll be using quaternion slope in order to spherically interpolate between our current rotations or transform rotation and our target rotation which is look rotation and we'll do this over time dot delta time multiplied with say five if we then make sure to call the method up here so face target save it and hit play we should see that no matter how we move our enemy will always point towards us but we also want to be able to interact with our enemy to do that let's go to scripts and go create C shop script enemy and we'll of course drag this onto the enemy let's open it up we'll make sure to derive enemy from interactable and we'll then go in and override our interact method so whenever we interact with our enemy we want to attack it but that's again for the next video you can see now that we have our interaction radius let's just set that to four we can also add some stats to our enemy to do that let's right-click on a stats folder co-create c-sharp script let's call this the enemies stats again of course we'll add this to our enemy object let's again delete our two methods and this time we want to derive from our character stats now the only thing that we want to change here is how we died so let's create a public override void for the diameter and here we probably want to display some kind of cold death animation or turn into a rectal so a drag tall effect slash death animation and then we want to destroy the game object so we'll call destroy game object this would also be a good place to add loot let's save this go into unity and we now set a max health of say 40 a base value for the damage of say five let's also give it three armor that's pretty much all of the systems that we need for enemy AI you can see that it will follow us around and if we right-click it we are going to interact with it really nice job making it this far into this series pretty much the only thing left gameplay-wise is combat I can't wait let's pretty much it for this video if you enjoy make sure to subscribe so dome it's a future one also some of you were a bit confused in the description of the last video I understand why we are juggling videos between two different channels so I've gone ahead and compiled a playlist it has all the videos on my channel and all of the graphics integration on Sebastian's channel I'll have a link for that in the description on that thanks for watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in August and a special thanks to hats off - yes pamekasan Thomas Wally stone gamer Sybok Nami Jason a Tito Derrick heaps Kirk face tamara phi with some casa kudamon aaron robert bund and peter lock if your name's not on the list i will make sure to include it in videos later this month and the next month as well you guys Rock
Info
Channel: Brackeys
Views: 421,963
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, models, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, enemy, stats, character, characters, ai, AI, artificial intelligence, follow, interact, enemies, RPG, collab, Sebastian lague, role playing game
Id: xppompv1DBg
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Wed Sep 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.