2D FOLLOW AI WITH UNITY AND C# - EASY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone I'm Noah and welcome to Blackthorne brought some of the very first games I've made with unity were 3d top-down shooters the enemies in these games all had the same basic artificial behavior chase after the player achieving this was vendored very easy thanks to unities built-in nav mesh agent components tutorial topic for another day but there wasn't and still isn't such a component for 2d thankfully achieving such a behavior in 2d is very simple in just a few simple lines of codes you'll end up with an enemy that frantically chases after the player character in a 2d space so without further ado let's get cracking so I have a new unity project opened up and in it - simple looking squares that will act as our prototype characters so I have a blue player that can move around the scene and this for now static red enemy I'll now make a brand new c-sharp script called enemy phone and open it up inside of my scripting editor I'll begin by making two variables one public of type float called speed as the name implies we will be using this variable to tweak how fast our enemy runs after the player and I'll also make a private variable of type transform called targets you guess that this variable will hold the game object our enemy is chasing after in my starts method I will make the targets variable equal to the game object that has a tag called player and more precisely the transform information of that object back in unity I will make sure my player character has the player tank on it this way the enemy will find his targets lastly I'll make an update function and inside of it typed the following easy line of code basically what this does is move the enemy character from his position towards the targets position at a certain speed time.deltatime is used to make sure that faster more performant computers don't have an enemy running a lot faster than enemies on a slower computer so to recap put whatever position you wish to move before the equal sign then type in vector 2 dot move towards and inside the parentheses state where you want to move from and to and of course at what speed let's now put our enemy follow script onto our red enemy square and put in a value of 10 for speed if we now press play you'll notice that our enemy is moving towards the player awesome we've put in place the solid base for a fun - the enemy character one curve you may notice however is the fact that our enemy will go exactly at the player's position and cover him up let's say we wanted our enemy to stop moving towards the player when he gets a certain close distance doing so is very easy all we need to do is add an if statement and check how close our enemy is to the player we can achieve this by using vector 2 dot distance inside the parentheses will ask to check the distance between the enemy and his target in other words the player and if that distance is greater than say three then the enemy is allowed to continue moving towards the player obviously if the distance is smaller than three the crucial line of code will not run and has a result the enemy will stop moving towards his target let's hit play inside of unity and make sure everything is working and you'll see that indeed when our enemy is close by he'll stop if I then move my player away the enemy will resume chasing him because the distance separating the pair is greater than three of course if you want more flexibility over the stopping distance just make a public floats variable called stopping distance and replace this three value with the stopping distance variable now directly in our inspector we have full control over the enemies stopping distance setting the value for example to seven we'll have the enemy stopped very far away from his target and that is all I have to show you for this beginner level AI tutorial with unity and c-sharp obviously you can use this script for many types of games I myself am currently developing a game called the fire of a belief a 2d top-down shooter that features a pack of various enemies hungrily chasing after the player characters definitely check out my dev log on this project if you are interested in seeing my progress thought process difficulties and more while making a game from scratch with that said thanks so much for watching I'll definitely be making more AI tutorials in the future of course if you have a request post away in the comment section down below and I'll seriously consider making a video on it alright take care stay tuned Cheers you [Music]
Info
Channel: Blackthornprod
Views: 233,660
Rating: undefined out of 5
Keywords: blackthornprod, blackthorn, noa calice, indie game dev, unity, AI, ai, enemy, follow, chasing, 2D, navmesh, agent, chase, following, C#, script, easy, tutorial, 2D FOLLOW AI WITH UNITY AND C# - EASY TUTORIAL, programming, code, fire of belief, top down, quick, short, Vector2, move towards, vector2, distance, how to
Id: rhoQd6IAtDo
Channel Id: undefined
Length: 6min 31sec (391 seconds)
Published: Wed Dec 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.