OK so now that we've got a core gameplay loop working we want to add something new to our enemies.
So I want to do this because it's good to see that when you design your amnesty for game not everything
is gonna make sense now everything will work the way you expected to do we made some enemies that seem
like they made logical sense but there's a couple of things that become obvious when you start actually
playing the game and it's OK to refine on your game after you've made some levels and discovered that
something doesn't work quite the way you want to.
And the thing I've noticed and you should have noticed at this point is that your enemies are a little
bass a little bit slow to react.
Shall we say so if you are out of range and you shoot an enemy.
It just kind of stands there and gets shot.
What we would really like to happen is that if the enemy gets shot it will start chasing the player
so straight away it'll switch to his chase mode and come running to get to play.
So we're going to set up a system to do that and it's very very easy to do what we're gonna do is first
of all go to our enemy controlled.
Ashley we'll go to our test scene because this is where we test things.
So back in here.
Let's double click on the enemy here so we can just see what's going on.
And we're going to open up our Enemy Controller script and what we're gonna do is we're gonna scroll
down to the bottom here.
We're going to add a new public void
that that will call we'll call it get shot and what this is gonna do is we're gonna scroll back up to
the top and going to add a new bool variable so a previous bool that we'll call was shut and this function
is just gonna say Well first of all it's gonna mark was shot is equal to true then it's going to say
well we've been shot.
So start chasing so we're gonna say chasing equals true and then we're going to say that we should start
moving towards the player so Agent plus destination equals player controller does instance does transformed
our position.
Like so we'll just set that safe and then what we're gonna do is we're gonna call this function when
our enemy gets damaged.
So let's go back to our enemy health controller and in here whenever we take damage we're first of all
gonna check and see if there is an enemy controller attached to this game object.
So for example our targets will have an enemy health controller system but it won't have an enemy controller
that we want to actually move.
So what we're gonna do is at the top here we're gonna create a reference when I have a public enemy
controller that will just call the easy for the enemy controller and we're gonna say here is if the
Easy is not equal to no.
So basically what we're gonna do is we're not in our inspector in unity we're gonna assign the Enemy
Controller to this object if it has it.
So if it's an enemy that can move will assign us here and then if it's not empty so if that means if
it has an enemy controller then in here we're going to say okay on the Enemy Controller call the gusts
or get shots.
Function like so.
Okay so let's just jump back into unity and we'll just set that up first of all and honor enemy.
Let's go to the prefabs.
I'm going to select the prefab of the enemy and then in the enemy held controller I'm gonna drop the
Enemy Controller in there.
So that's that setup.
But then what should happen when we actually get shot.
So at the moment we're telling it to go chasing after the player but if we scroll up here.
So if we're not chasing we do all this stuff.
And that's perfectly fine.
But in here is where we handle if we are chasing.
And most importantly here is how we tell it to stop chasing.
Sorry.
No not that one there.
This one here is where we tell us to stop chasing as you can see it says chasing equals false.
One thing I just occurred to me I don't really know why I'm setting the agent destination in that function
down below because here is where we control where the enemy is going so I'm actually just going to remove
that line and delete that down there.
So it's go back up here to where our chasing is being set to false.
And this happens when our enemy is above the distance to lose.
So if we are too far away and we shoot the enemy it's still going to say oh well the players is too
far away so just say chasing is false and I'm not going to try and go there but what we're gonna do
instead is blow this we're gonna add an else statement
and in here we're gonna say we know that we just set was shot to be true.
So if we are above this distance we're gonna assume was shot is true.
And once we get within this distance late range.
So if the player gets within a certain distance to the enemy that's when we're gonna say was shot is
false.
At that point.
And the reason we do this is because within this section here we're only gonna allow ourselves to stop
chasing if was shot is false.
So if Nuss was shot.
So basically what happens is we'll come down here.
We'll get shots and we're so far away from the enemy and it doesn't matter.
Some far away from the player the enemy as we say was shot.
It's true and chasing is true.
So then it will go back up to here.
And at this point it checks the distance to the player and the player the distance should be greater
than distance to lose because it's the player so far away.
And I'll go.
Okay well I'll go in here but was shot is still true at the moment.
So I can't stop chasing and what I see.
I will put this whole thing inside the car brackets because it doesn't really matter whether we set
this or not.
So then we'll go down to here and say oh well this isn't true because this pit is already shit.
And then when the player gets within this range that's when we'll say okay at this point we can stop
marking was shot as true.
So if the player gets within this range and then falls out of this range again that's when the enemy
will stop chasing.
So we can get within a certain distance to the enemy.
And then once it gets close enough it'll say OK I know where the enemy is.
I know where the player is going and keep shooting at him.
And then when I lose sight lose distance to the player again that's when I stop chasing.
So let's see this in action.
I going to save this and we'll go back in here.
I think at the moment our enemy might be a bit too close and he'll come chasing after us anyway.
Yes.
So that's just move the enemy away from us here.
We'll put him a little bit further away there.
So he's far enough away now.
Well forget about our tired friend.
Let's take their turn house.
Now if I take a shot at our enemy here.
Well it doesn't help when I give him a nice headshot.
Let's I just disable our tires here for the moment.
So let's just shoot this guy.
Let's switch to our repeater and shoot him in the leg.
And now he's gonna come running after me and he tries to shoot me.
He keeps running against the wall there a little bit because we're stopping a couple of nav mesh issues
with this guy at the moment but that's OK.
We know how to solve that.
It's just a problem within the unity Ed..
It won't actually affect the final game but you can see he is correctly coming chasing after the player.
So if I now move out of range because he was within range he's never gone back ever range again your
way his five seconds and then he's asked back to his original position so perfect.
We now have a way to get the enemy's attention when he gets shot.
So it looks like oh no you've got my attention you're going to try and destroy me and we're pretty happy
that that is working the way we expect.