Adding AI and Pathfinding to a Platformer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey they're with me Stephen and as you may or may not know I'm making a game so one day I was working on my game you know adding some cool new features and all when I noticed wow this game is stupid it's just way too easy I mean look at the enemies all they do is stand around and occasionally have seizures that's just not very immersive so then I went and looked at the enemy AI code and remember that it was mostly just randomness so I fixed I now solve randomness just kidding I'm making an actual enemy AI now let's get into it but before we do anything we should probably plan it out first because I'm like most of the things I make I actually want this to work properly and video game AI is fairly complex so let's do that the first thing we have to know before making the AI is what we wanted to do because trying to make something was how first knowing what you're making is a bit difficult believe me I know from experience so well what do we want the ID to do first of all it has to be able to attack the player this should be pretty simple all we need to do is have a randomly shoot while aiming at the player it should also account for whether a melee arranged weapon is used and adjust the distance of the player accordingly after that we'll need the AI to move which brings us to our next point movement and to move we need pass finding basically we need to make an algorithm that finds the most optimal paths or at least a good enough pass to wherever the AI wants to go and that follows it normally this would be pretty easy to do if this were say a top-down game all I would have to do with run a path finding algorithm and have the NPC's following however my game is a platformer in that means it has platforms in gravity and other annoying things so I have to find a way to not only pass fine to follow the past but also calculate where enemies can go and account for things like jumping and falling after some thinking I came up with a kind of sketchy but functional way to do this basically I'm going to look at all the blocks and platforms in the game and test if it's possible to walk in them by checking things like if there's any empty space above it then for each block you can walk on I'm gonna add a couple nodes on tops representing points where the AI can go and that connects the nodes that are next to each other to represent the past AE I can take each connection also stores information like distance which we need for pass finding and by doing this a sort of map or network is formed in computer science this is called a graph y Oh No anyways after we generate the network whenever I need to find a path from one node to another I can just run something like an a-star algorithm on the network to find the path but wait what is say star algorithm well I'll give you the rundown basically the a-star algorithm of the modified Dijkstra's algorithm which is an algorithm that finds the shortest paths and a graph like we have here but with something called heuristic which sacrifices some accuracy in exchange for faster processing times for that sweeps we fps to find a convincing past quickly God that was a mouthful but if you actually want to learn more about what the a stir algorithm is you can go to the link in the description or something I don't have the time to explain the in-depth my Minecraft addiction awaits ok so now that we know what paths to follow we have to actually follow it this parts pretty simple sort of basically every time an enemy needs to go somewhere I'm gonna run the pass finding algorithm and add the past the enemy is going to take into a queue then I have the enemy go through the queue and follow the path through each node using some basic logic and if statements for jumping and stuff until eventually it reaches the goal very cool and now the enemies are moving pair this with the attacking system from earlier and we have a functional slightly jank killing machine but it does have a few problems most notably that all the enemies in the game are apparently omniscient and can track you down to time and space just to be you as a fish from miles away which isn't very immersive so next up is States basically what I want to do is have different states that the enemy can be in including idling attacking fleeing and liquid oh wait no wrong state basically I assign a state variable to each enemy which changes based on a couple conditions for example if the player is out of sight and switch to the idling state and once the player is visible switch to attacking or if the enemies house is low switch to the fleeing state then based on what the state variable is the enemy will perform whatever action needs to perform and yeah that's basically all the components of the enemy AI we have a system for attacking an algorithm to navigate through the game world and immersive decision-making all we have to do now is slap it together and hope it doesn't break um give me a few minutes a few minutes later okay so I've ironed out a couple bugs and now it works the enemies follow me attack everything is running beautifully mostly but for now I think it's pretty good although I might come back later and make a few adjustments or improvement so things like jumping or adding things like wandering around and taking cover but the main element that they are functionally in their work so it's good enough for now and yeah that's all for today if you liked make sure to hit like if you disliked hit dislike and if you subscribed hit subscribe hopefully I'll see you next time bye bye
Info
Channel: Steven
Views: 87,762
Rating: undefined out of 5
Keywords: ai, pathfinding, a* algorithm, pathfinding in a platformer, indie game, game development, devlog, how to make games, libgdx, programming, djikstras algorithm, unity, gamemaker, steven, java game development, game engine, video game ai, video game pathfinding, artificial intelligence, 2d game
Id: 5DhCoqoPmzs
Channel Id: undefined
Length: 5min 2sec (302 seconds)
Published: Tue Apr 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.