Cookies   I display ads to cover the expenses. See the privacy policy for more information. You can keep or reject the ads.

Video thumbnail
Hey, everyone welcome to episode 7 so today. We're just going to be
Tying up some loose ends
So let's go into the enemy class first of all and I'd like to I'd like to detect when the Target has died
So we can stop chasing the Target
So to do that. We'll use the living entity on death event we created earlier
So let's get a reference to the living entity component of our Target which we can call
Target entity so down here where we actually?
Find our Target. We can say Target entity is equal to Target dot get component and
We just get the living entity component, then if we create a little void method
Let's call it on Target
death
Then we can just subscribe that method to the on death event over here
So target entity dot on death plus equals on Target death
Great, so if we if we now create a bou
called hesitate
We can say once the target dies
We no longer
Have a target so that equals false
And we can also set our current state equal to state dot idle since the enemy will now have nothing to do
All right
Let us not assume
That an object exists with the tag player
Since of course the player might have already died by the time that this enemy spawned into the spawned into the game
so let's first do a check if
This if this player object exists so we can say if that's not equal to null
Only then will we go ahead and execute this block of code
All right, and we can say once we found the target as target is now equal to true
Okay, so that's nice. We can use this head target variable in a few places for example in the update method
We don't need to check for attacks and all that sort of thing if we don't have a Target
So only do this if as target and also in the in the update path code in
We were saying while target is not equal to null and it's much more elegant to just say while we have a Target
Okay, great. So the next thing we want to do is to actually take damage from our Target
Sort of during the attack so we'd want to take damage at the point where the enemy reaches the attack position
so that will be when interpolation is equal to 1 and
As defined by our little equation over here that will be when % reaches 1/2
So let's create a boo
Has applied damage so we can keep track of that you can set that to false?
initially and we'll say if the percent is greater than or equal to 1/2 and
We've not yet applied damage
then first of all we say has applied damage now equals to true and
We'll want to say target entity dot and we want to take a hit from our Target
But the problem is the take hit method is asking for damage
Which we can give it
But it's also asking for a raycast hit variable which since we're not very casting we can't actually supply
So let's go into the take into the eye damageable interface and let's create another method
I'm just going to copy that Gonna call this avoid
Take damage
This is basically. It'll be a simplified version of take it
It's going to ask for damage, but it's not going to ask for a rake a state variable
so in unity of course we're going to get
Errors because living entity doesn't implement this new method so we can say public void take
Take damage
sticks and a float damage and
We can basically just cut all of this code from here put that in there and the take it method can now essentially
redirect to take damage and pass in the damage
You might wonder what's the point of this take it method now. That is just simply redirecting to take damage
so later on we'll
Do some stuff?
here with the with the hit variable
so for example we could use the raycasthit variable to detect the point at which the projectile hit the enemy and
then we could
instantiate a a particle effect at that point
so this this ticket method will still be useful later on alright, so let's go into the enemy and
We can now say target entity and sort of take it. We'll just use the take damage method and we can pass in
Damage which is a variable we haven't created yet. Let's go up and do that
float damage, I'll set this equal to one for now and
Let's let's test. If that's working so I'm going to go into my player I
Think I'll give him a starting health of 10. I'm just going to change that down to 3 so he dies a little bit quicker
one hit two hits three hits in Row dead, so that's working nicely and
The enemy is not giving us any errors. So it looks like we've handled the
Sort of Target death case correctly
So that's nice
The next thing I'd like to do is when we're when we're shooting
our
Our bullets sort of just carry on forever
so
Let's go into the projectile class. Where's that over here and
Let's let's specify a lifetime float
lifetime
so give that a lifetime of maybe at three seconds, and then if we create a start method
We can just say destroy
Want to destroy this game object after its lifetime has expired. So now if we shoot some bullets
In a couple of seconds, we should see all of those disappearing from the Hierarchy so that's working
Ok so one problem at the moment with our
Projectile Raycast Collision Detection is that if the project house starts inside of an object?
It won't actually register that collision since that's just how ray casts work
So for example if an enemy comes close to me, and I'm shooting these projectiles are being Spa inside of the enemy
And they're just continuing out the back with
without doing any damage, so
to fix this let's go into our projectile class and
we want to start off by just
Seeing if we're inside of any colliders so we can do that by using the overlap sphere method
So we get an array of Colliders. We can call this may be our
initial
Collisions this is equal to physics dot overlap sphere, so this just asks for a position
So that would be the position of our projectile
Transformed our position it wants a radius which can
Can just make small something like point one, and then we can also give it our Collision mask
So now we've got a array of all the colliders that our
project Al is intersecting with
so we can say if the initial collisions dot length is
greater than zero, so if we are actually colliding with anything, then we'll take damage on the first one and
Destroy the project also
We we basically want to use the on hit object method, but this is asking for a ray cast hit
so let's make a
alternate version of this method void on hit object that takes in
simply a collider
You can call C
and
This will basically do the same thing as the on hit object method
except
Here with a C. Dot yet component and
Instead of using the take hit method we use the take damage method that we set up earlier
Okay
So over here. We can just say
On hit object and we can just give it the first thing that collides with so initial collisions zero
Now this should be working
Cool. That's much better
One last thing that that's sort of a bit troubling about our
Collision Detection at the moment is that if for example?
we've got an enemy and a bullet and say dong ding dong here's here's one frame and
In this one frame the enemy moves slightly forward
overlaps the overlaps the bullet now the raycast is once again starting from inside the enemy and
It's not going to detect the collision. So what we want to do to solve this is
at the moment the length of our of our array is exactly the distance of how far the
Of how far the projectiles moving in that one frame
But we actually want to add a little bit on to that which a common to compensate for the fact that the enemy is also
moving
so
Let me delete both of those
Quite a float over here. We can call this sum. I'm never sure what to call this variable I usually go for
Skin width which is a slightly weird name I know
But say set that equal to 0.1, and then when we check our collisions
We say that the length of our array is move distance plus this
skin width variable
okay, and
Now even if the enemies are moving at high speeds that that Collision Detection should work fine
and if if it's if it's not working fine if the enemies are moving really fast
You can always just increase that skin width variable, and it will fix the problem
cool, so that's most of the odds and ends tied up in the next episode we'll start generating our levels and
Yeah, I think that's gonna be pretty fun so see you then cheers