Monster shadow


2D follow AI with Unity - [Tutorial]

In this video, I'll show you how to create an enemy which will follow your player. It uses an adjustable rotation speed to give you greater control over the enemy's accuracy. ========= ❤️ Become a Tarobro on Patreon: ========= 🔔 SUBSCRIBE: 🗨️ DISCORD: ✅ MORE TUTORIALS: MORE TUTORIALS: 0:00 Intro 0:38 Setting the follow target 1:15 Simple following 2:55 Adding rotation speed for realism 4:36 Moving towards our rotation direction

Stupid Sniper
Lorenzo Tesler-Mabe
Good stuff! How is it that the enemy model is not visually rotating (i.e. the face)?
Enemy was made up of 2 layers. The face layer had a simple script which just forced its rotation to stay stagnant :)
Kevin Chen
Nice tutorial, Hey if your target object (the player) is destroyed in the game how to fix the error of NullReferenceException? or any other alternatives?
@Kevin Chen the obvious question: is the player alive when the new enemies spawn? If so, it'd be strange that the awake function isn't finding the player. First step would be putting a debug log after the _target assignment in awake, log the _target to see if it's actually null or if something funny is going on. Ideally you'd want to take the _target assignment out of awake and make a coroutine which runs when target is not assigned. Maybe use physics2d.overlapcircle to detect a player collider and assign that. That way you can actually simulate agro distances. Sorry if this didn't answer your question
Kevin Chen
@Tarodev Hey Tardov, thanks so much for the solution, that totally works and sounds logical in scaleability. Just a following issue, the enemies from the current screen stop when the target == null, but the new enemies from the spawn points still lose their targets when instantiating. Are there any particular reasons that the post instantiated objects can't detect target status within their scripts? Sorry I am really new to unity, cheers again
At the top of your update function add this line: if (_target == null) return; This is good because in the future you could add logic which periodically checks for new targets. So all you'd have to do is set a new target and the follow would start again.


in this video i will show you how to create an enemy which will follow your
player and overshoot so that you can trick it
similar to a homing missile okay so what i have here is a little
tank that that can drive around and i can rotate my turret to my mouse
cursor and if you need to know how to do that i'll just leave a little annotation
up here and uh it would be nice if these enemies
here would rotate uh and travel towards me to
make it a little bit more interesting so let's do that all right so let's open
up the enemy script so all i've got here is just some death
logic when the projectile hits it so i'll just
close that out as it's not relevant and let's begin to first start following
something we first need something to follow so
let's create a reference to a transform here
and it will be our target and now in this tutorial
i know that all i'm going to be wanting to follow is the player
so in my awake function i'm just going to simply
set our target here is equals to find objective type
player and it's transform you can do that in a million ways
depending on what your what you're trying to achieve
now to actually follow the target let's hook into our update function
and the easiest way i've found to do this is to
literally just say transform position equals
back to three move towards and our current will be our current
position uh tug uh goal position will be our target dot
position and now we need a speed variable here so
let's serialize a floats speed
speed and let's maybe default that to nine
and we'll just send in speed times time dot delta time
now if you don't know what delta time is basically it's just telling you the
time since the previous frame so it'll be some like crazy small number
like .001 something like that so basically because we're multiplying
by this it will be standard or the same movement speed
regardless of whether you're on a computer at like 240 frames or on a
i on like an iphone or something at 30 frames so
that's actually all we need let's head back to unity
and i've already got the script on my enemy that spawns so let's just try that
spawned right on us let's just try that again
yeah there we go it's actually following us directly
um if you look closely it it's kind of like
absolutely exact it really doesn't give the player too much too much room to
try to trick them or dodge around them so
we can change that let's comment that out
now let's first grab the direction that they need to be facing towards to
get towards the player and we'll do that by taking the targets
whoops equals the targets position minus r position uh and that will give
us the look direction that we need to be
facing now we'll do transform up equals vector 3
move towards and this will take our current transform
up and then the goal will be the direction
and now we need a rotation speed variable so let's just copy this
serialized floats and we'll change this to rotation speed
and let's default this to something like 1.5
and then we'll put in our rotation speed times time dot
delta time so this will slowly move our up direction uh
towards the player so let me just explain that
back into unity uh you'll see my my my turret gun here as you can see it's
up direction uh is the way that it needs to face so all i would have to do
is turn that to face whatever direction i need to
go that is explained more in my previous video which is up here again
so now that we're slowly moving the up direction towards the uh the player
we can now slowly move towards our forward direction
so let's just actually uncomment this as we can reuse this let's just pull this
up here instead of moving directly towards the
target let's move
upwards from our current position as in our transform up position so let's take
our current position plus a transform up position
now obviously transform up position changes depending on the rotation of
uh the transform back into unity let's try that out
and yeah as you'll see you can kind of fake it now you can
you can run away from it and trick it um so that's obviously perfect for like a
homing missile or something you'd make it nice and fast with a
with a slow speed and it would kind of just orbit around the player until it uh
corrects itself properly and then also you could go into the enemy script and
you could make the rotation speed back up to something like 10
and then that would obviously directly follow the player like it did before so
with the one script you can you can have many different types of uh behaviors
and let's just try making it uh really fast let's say
oh that'll probably be too fast like 12 and then we'll put the
the rotation speed on two and if we pull that on this one because
it's probably going to fly off screen yeah there you go
not going to have too much chance of of oh
oh and i'm on fire so i hope that helped you out i'll uh
i'll leave a link to the script down below in the description
uh if you liked it give it a like subscribe
and i'll see you next time you