Easily Make Ai Animals That Chase And Flee In Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this video I'm going to show you how to easily create two different types of AI animals one of the AIS will be aggressive and Chase your character while the other will be passive and run and hide this method is beginner friendly and utilizes blueprints as opposed to behavior trees so with that being said let's jump in we're going to start with our aggressive animal AI first start by creating a new character blueprint we'll call it AI wolf [Music] once in the character blueprint start by adding your animal's skeletal mesh to the blueprint and then moving it into the proper position of the capsule component [Music] access the event graph and add a new event that allows the AI to randomly roam around the map for our event to trigger when the player starts playing the level we'll need to call it from our event begin play component pull out the first node and search for get random location in navigable radius this will make the AI pick a random location within the levels nav mesh get the actor location and plug that into the origin node and increase the radius to something like a thousand pull out the next node and search for AI move to this will be the component that allows your AI to move to the location it randomly selected plug a reference to self into the pawn and plug the random location into the destination on success we're going to add a delay so that the AI doesn't immediately move to another random location after the delay call our random ROM event again this way the AI will continue firing the event without interruption [Music] so let's test this out before we test make sure to add a nav mesh to your level without this the AI will not move at all [Music] okay great our AI is moving around next we'll create an animation blueprint so that the AI will animate while moving and waiting right click in the content browser and hover over animation at the bottom of the pop-out window hover over Legacy and select Blendspace 1D and choose the appropriate skeleton [Music] once in the blend space rename the name of the horizontal access to speed and change the maximum access value to the value in which your AI will reach their Max movement speed if what I just said was confusing go back to your character's blueprint and search for Max walk speed this is the number I'm referring to in the timeline drag in the idle Walk and Run animations from the asset browser window depending on your character you may need to tweak where these animations are located in order to get a more natural looking animation okay now we're ready to create our animation blueprint right click in the content browser and hover over animation select animation blueprint and choose the appropriate skeleton open up the blueprint and make sure you're in the anim graph drag out your blend space that you just created from the asset browser and promote speed to a variable plug in the blend space to your output pose now switch to the event graph of the animation blueprint drag out from the event and cast to your ai's character blueprint plug the trigapon owner into the object of the cast drag the object pin out and search for get velocity you may need to scroll down the list in order to find it foreign drag out the value and search for Vector length set your speed variable and plug the vector length and the character's cast into the designated slots [Music] in your ai's blueprint change the animation mode to use animation blueprint and select your newly created anim BP as the anim class alright cool so now our character is animated if you're running into any issues with how the animation looks try these next couple of steps if the animation appears Snappy access the ai's blueprint and search for yaw uncheck use controller rotation yaw then search for rotation and check use controller desired rotation to make our AI appear more lifelike we're going to create some anim montages that the AI will play when pausing between moving back in the content browser right click go to animation and then select anim montage and pick your skeleton let's make three different montages one for eating one for idling and one for sleeping [Music] drag in the desired animations from the asset browser [Music] thank you [Music] once you've created your anim montages go back to your ai's blueprint and create a new variable of the type and a montage [Music] because we have multiple montages we want to click the icon drop down next to the variable type and select array compile your blueprint and then click the plus icon to add the desired amount of montages in each value select a different Montage for the character to perform foreign graph drag in your newly created array to appear more natural we want our character to pick a random Montage to play in order to do this drag out from the array and search for Shuffle from Shuffle drag out and search for for each Loop this will Loop through our current array of montages and select a random montage from completed drag out and search for play and a montage plug the array element into the anim Montage you can now plug your return value into the duration slot of the delay so that the character will always delay for the duration of the current Montage being played finally let's add an additional layer of logic which will help to avoid the wolf from roaming around while playing in Anna montage at the beginning of your random roam event pull out a reference to your Anna Montage array pull out for each Loop and then search for stop and a montage this will act as an extra condition to help avoid any bugs between the movement and the montages there's one more step we need to complete before our Anna montages will work properly let's go back to the animation blueprint and access the anim graph here we'll need to right-click and search for default slot add this component between your blend space and your output pose [Music] awesome now our wolf is roaming around and Performing random animations when it stops next let's add the ability to chase the player we'll need to add the pawn sensing component to the ai's blueprint with that component selected click the on C Pawn event in the details panel in case our AI is performing an anim Montage when they see the player we want to stop the current Montage before chasing the player to make the chasing component more lifelike let's increase our AI speed first we'll start by decreasing the max walk speed in the character movement component this way our AI will walk around from point to point and only run when chasing the player next drag in a reference to the character movement and set the max walk speed to a higher number use the number you used or a higher number when making your blend space in order for the AI to perform their running animation to get the AI to move toward the player search for AI move to [Music] drag the pawn pin from Pawn sensing component and plug it into the target actor [Music] get a reference to self for the pawn of the move 2 component if you want the AI to quit chasing the player once the player is out of range an easy way to do this is by using a re-triggerable delay after that make sure to set the walk speed back to its original value and then resume your random roam event [Music] thank you [Music] for our passive AI we're going to follow the same exact process so create your AI blueprint blend space animation blueprint and anim montages thank you [Music] foreign for this AI I'll be creating one extra Montage for hiding as I'm going to have the AI run to a particular spot and then disappear [Music] [Music] since I'm using a rabbit I'm going to create a blueprint that will act as a hole in the ground that the rabbit will hide in right click in the content browser and create an actor blueprint [Music] I'll be adding a cylinder mesh for the hole and a box Collision for an overlap event [Music] thank you [Music] in the event graph select the on component begin overlap for the Box Collision cast to the appropriate Ai and then play the hide montage and add a delay foreign [Music] for some reason plugging in the return value as the duration for the delay results in the animation resetting in the final frame to fix this I'm using a manual number that's lower than the duration of the Montage as the delay once the delay is finished set the ai's mesh as invisible [Music] thank you [Music] back in the rabbit ai's blueprint locate the Stop and a montage portion of the on C Pawn event drag out get all actors of class in order to reference our newly created actor blueprint this process can be expensive if you have a lot of actors in your game so we'll drag out get a copy from the array [Music] [Music] drag out the pin from the copy and promote it to a variable plug the variable into the target for the AI move to function foreign [Music] BP in our level and the rabbit AI will look for that actor and move towards it when it sees our player character [Music] we want the rabbit to be able to leave its home and resume roaming around when the player is not within a certain range an easy way to do this is to add an additional box Collision trigger to our actor blueprint we'll make this box much larger and create Logic for it when the player exits the Box [Music] add an on component end overlap event for your outer box a player's character blueprint as the character that will trigger the rabbit coming out of the hole we need to have a new reference to our rabbit in order to avoid the common access to none error to do this we'll search for get actor of class and select the character blueprint we'll get the mesh and check if it's visible as this will tell us if the rabbit's in the hole or not if the rabbit is invisible and the player character leaves the outer Collision box then we want to set the rabbit's mesh to visible and then call our random Rome event foreign [Music] [Music] all right [Music] now let's add an additional check to avoid some common bugs that could occur back in the rabbit's character blueprint let's add a check to the on C Pawn event we're going to check if the rabbit is visible so that we can avoid having the rabbit roam around wall invisible add this to the end of your logic right before you call the random room event [Music] foreign [Music] [Music] and you're done now you have two sets of AI animals that you can use to help fill your game with Wildlife I hope this tutorial was helpful if you got something out of it please leave a like or a comment if you're interested in more Indie Dev and Unreal Engine content feel free to press that subscribe button thanks again everybody and I'll catch you later [Music] thank you
Info
Channel: Elusive Panda
Views: 9,235
Rating: undefined out of 5
Keywords:
Id: Rvzhmth66ec
Channel Id: undefined
Length: 16min 20sec (980 seconds)
Published: Thu Mar 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.