Unity - Enemy Follows Player [Beginner Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you how to create enemy AI that follows your player around the screen I'm not the greatest artist so for this video I'm going to download some free top-down characters from the asset store to use as my enemies I'll include a link to this asset pack in the description below if you want to follow along then quickly I'll open these assets in Photoshop and export each limb as a sprite into unity once in unity I can add them into a parent game object and rearrange them so they look like an enemy I'll even animate the limbs a little bit so it looks like this object is walking and then I'll do the same thing for a player object and once that's complete we can get started on writing our a I follow script in order to make our enemies follow our player we must calculate two things every frame the first is we need to calculate the direction our player is compared to our enemy we can do this simply by subtracting the position of our player minus our enemy and then secondly we need to take this direction value and use it to calculate the degrees of our angle so we know how much to rotate our enemies so they always face our player so let's go ahead and create a script called enemy by right-clicking in our project window and selecting create and then select c-sharp script let's just go ahead and label this enemy and then let's drag it onto our enemy game object and then open it in the editor so the first thing we want to do is calculate the direction our player is compared to our enemy object in order to do that we need to create a reference for our player object in the script so up at the top right public transform player then in our update function which is executed every frame let's calculate our direction by writing vector three Direction equals player dot position minus transformed opposition since the script is attached to our enemy object we can use transform drop position to reference this object's position to see this in action let's write debug that log and in parentheses right direction then back in unity go ahead and drag our player object into the inspector where it says player then if we press play and move our player object around the screen we will see in real time the distance our object is compared to the player object the closer we drag our player the smaller the numbers become so now let's go ahead and use that value to rotate and move our enemy in real time in this example we are going to use rigidbody to move and rotate our objects so in order to do that let's go ahead and add a rigidbody component to our object since we are working with 2d assets make sure you select rigidbody 2d then go ahead and set the gravity to zero and then back in our code let's create a reference for our rigidbody 2d up at the top by writing private rigidbody 2d and let's just call this RB then in our start function let's define RB by writing our B equals this get component and then rigidbody 2d this now lets us use RB to manipulate the movement and rotation of our object so the first thing we can do is rotate our object in the direction of our player to do this first we need to convert our direction into degrees for this we will use a math function called at on 2 which basically calculates the angle between 0 and a vector to point in our case this means calculating the angle between our enemy object and the player object you might have remembered us using this formula in our point-and-shoot tutorial to rotate our turn to face the direction of the mouse pointer so let's go ahead and write float angle equals math f dot onto and in parentheses right direction dot why for the first parameter and then direction on X for the second parameter this then outputs a value in radians so we need to convert this number to degrees which can easily be achieved by multiplying by math F dot rad to degrees and then with our angle value defined we can then set our rigid bodies rotation by writing our B dot rotation equals angle and now when we go back into unity and press play we should see our enemy rotate to face our player but as you can see in this example it's not quite working right the reason for this is that our enemy needs to be facing the right direction if your object doesn't do this naturally there is an easy fix all you have to do is simply create an empty game object inside your enemy object and then drag all the limbs inside it and rotate it to face the correct direction so now when I press play my enemy is always correctly facing my player object so then naturally the next thing to do is to have this object move towards the player for this we are going to use our standard move character function so firstly up at the top let's define a vector to called movement then in our update function let's set movement to equal direction now if you watch my other videos about movement you'll remember that we want this value to be between negative one and positive one so in order to do that without any complicated formulas we can just write Direction normalize and this built-in function will do the rest and now that we know where we want our enemy to move let's go ahead and put it in motion so let's create a new function called move character and for a parameter let's pass a vector to called Direction then simply we just need to write our B dot new position and in parentheses put transform nap position + and then in another parentheses put Direction times move speed times time Delta time what this will do is take our current position and move it in the direction that we specified which happens to be the direction of our player object to control the speed of this movement we multiply by move speed which we should define above slope at the top let's write public flow move speed equals five you can alter this number to make your enemies move slower or faster and then since our direction is a vector two and our transform is a vector three we might be getting an error when we try to add the two to fix this let's convert our transform into a vector two by writing vector two in parentheses in front of it and then the last thing we need to do is declare our move character function since we are dealing with physics we should do this in a fix update function so go ahead and write void fixed update and then put move character and use movement as a parameter now if we press play in unity we should see that our enemy follows our player object and no matter where our player moves around the screen this enemy will now follow it will even work if we clone this enemy and add multiple enemies throughout the scene and now that you understand how to create enemy AI see if you can build a character controller to spawn and shoot down enemies like this I'm not going to cover these topics in this video but I'll make this project available for download on patreon for anyone who is interested and that's it for today's video if you enjoyed this video be sure to subscribe to our channel so you don't miss any future ones [Music] [Music]
Info
Channel: Press Start
Views: 182,069
Rating: undefined out of 5
Keywords: unity c#, unity programming, unity tutorial, unity (software), enemy, follow, AI, unity enemy follow script, player, enemy follow player, look at player, 2d follow player, 2d follow, enemy 2d, enemy look at player unity, enemy 2d rotate, object rotate in 2d, zombies, aliens, top down, top down shooter, game dev, development, game development, enemy move, enemy move towards, enemy move player
Id: 4Wh22ynlLyk
Channel Id: undefined
Length: 6min 51sec (411 seconds)
Published: Mon Jul 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.