Welcome back, my fellow game developers.
So we've just made our game a lot more cooler.
So now when we should award,
we don't just have our bullets disappearing.
We have our bullet's going in a puff of smoke.
So when we shoot around,
you can see that we have this little cool effect that gets destroyed.
Notice right here that it gets destroyed after a certain time,
which is something that we do need to take into consideration.
So let's not waste any more time and let's get started.
Oh, so time to add some weight to our bullet impacts.
And we'll do that by adding an impact effect.
So I'm going to go into the assets and I'm going to go into the sprites folder,
and I'm going to create a new folder in here,
which will be the bullets fact.
And in here we're going to go into the resources.
And I've provided you with a couple of facts that I found.
You can use whichever one you want.
Or you can also find some on the website,
open game art.com or any other website.
So you can see right here we have three effects.
This one with a bit of smoke,
this one with a bit less smoke,
but it's a bit weird.
And this one, now I'm going to use the OH,
so you see it has the impact metal impact.
Metal impact metal.
No worries about that.
We'll just have to do with what we have.
We'll use the impact metal.
I'll use the first one. Sure.
Why not?
So how are we going to do this?
We are going to slice up the sprite.
We are going to create an animation and then we are
going to play it whenever we hit the object.
I'm going to slice this up if you want.
We can do this together at the same time and see who does it faster or better.
So first thing, we select the first 1.
First impact, make it multiple.
Apply changes.
Let's make it point.
Maybe we don't need to make it point.
Let's go ahead and Sprite Editor.
Okay, the particle of smoke or not visible,
but we are going to go ahead and slice this up by grid size.
Let's see how big is this grid.
So we have 1, 2, 3.
Okay, so I'm going to get my handy calculator.
And the way I'm going to slice this up is because we have this 960.
You can see it right here down on the right,
960 on the horizontal and 576 on the vertical.
So 576 divided by,
because there are three lines,
I believe it's 192.
So on the y-axis, it's 192.
And in here we have 12345,
so 960 divided by 5 also 192.
Great, should have seen that coming.
So we have sliced up squares.
Very nice.
Apply the changes.
There we go.
Now we have all of these.
I'm going to create a new game object in here,
and this will be the bullet impact.
So bullet impact on wall.
And there we go. Here it is.
I'm going to go ahead and add a sprite renderer to it.
So sprite renderer, and I'm just going to give it this effect right here.
Where is it?
Let's add this impact of fact right here.
Can we see it?
Is it visible?
It's behind. Should we create a sorting layer?
Sure. Why not?
Let's go ahead and create or we can make a deep layer bullets sorting layer.
The bullets. Sure, let's keep it that because obviously it's part of the bullets,
so it's logical to make it on the bullets layer that will set it in here.
Is that big enough? Maybe we want to make it bigger.
Let's go ahead and make them think 50 is a lot.
Let's make it 75 and see how big that is.
Okay, maybe we can add, make it 50,
apply that, and maybe remove the point,
make it bi-linear so it's a bit smoother.
Yeah, I like that more.
So there you go. This will be our impact.
I'm going to add an animator component in here.
And let's choose this one.
Animations actually.
Sure, Let's go ahead and create.
This will be in the assets animations.
And this will be called the bullet Impact wall.
And I'm calling it the bullet impactful.
Well, because later on we will have different types of impacts.
So in here we're going to have the bullet.
Bullet hitting wall.
Save that.
There we go. So you can see immediately because we already had the animator component,
the controller gets sent here that's moved
the animation up a bit so we can get all of these,
set them here, and now put the animation back.
Now, let's play this.
Okay, very nice.
I think it is a bit fast.
Or maybe that's perfect.
Let's see.
I'll just make it a bit wider.
Okay, great.
I think that is enough.
But I still think that 50 is a bit too much.
Maybe let's make it 18 are no,
save that. And animations.
Let's play this.
Okay, I think that is good enough.
So I'm going to save this.
I'm going to go into the assets,
go to the prefabs,
and I'm going to make this a prefix or the bullet impact now is a prefab.
It's on the visit,
the sprites are on the bullet layer and we have everything setup.
I hope you did your best and creating this or if you didn't,
I hope you followed along and I wasn't too fast for you.
So now, what are we going to do?
Now we need to instantiate this effect every time a bullet hits a wall.
The way we do that is by issuing you a challenge.
So your challenge is to instantiate the effect.
So find your own impact effect if you need.
So you can go through open game, our website,
or through HIO or simply by searching on Google.
Maybe if you're a good artist,
you can create your own and get a reference
to the impact on the bullet controller script.
And make sure to instantiate the effect before the bullet gets stroke.
So pause the video right now and go do the challenge.
Okay, welcome back.
So back in our player bullet in here,
I'm going to create a reference to the serialized field to the bullet.
Or it's going to be a game object,
the bullet Impact effect.
Save that.
And what we'll do is before we
destroy or get destroyed or before the bullet gets destroyed,
we are going to instantiate the bullet Impact effect.
And it's going to be where, where should it be?
It's going to be on the transform position,
the bullet tip, and the transform rotation of that bullet.
So I'm going to save all of that.
I'm going to go back in Unity.
I'm going to choose the player bullet.
Now we have an impact fact.
We can add it right here.
We can save that.
We going to just check the enemy to 1 second.
So what happens in here?
Why are we so confident and creating this?
And why there is a small problem.
Still, what happens is as soon as we instantiated,
you see that the default layer right here is the bullet hitting the wall.
So immediately upon entry,
we started bullet hitting walls, fact.
So let's see how that works.
Let me just remove this from here.
Yeah, I think I can remove it.
And one more thing I wanted to do,
I've noticed that these Z was on 0,
that me just reset all of these,
save that, run the game.
So now I should shoot a wall and upon impact you can see that there is an explosion,
but for some reason you can see that it's still going.
So if I have a lot of bullets,
you'll notice that we have a lot of facts and none of them are going away.
You can see it right here on the bullets impact on the wall.
So what we're going to do,
and that is because of the nature of the animation we've just created.
If I go into the animations,
if I go into the bullet,
you can see right here that we have the controller and we have the animation.
And I've told you previously a couple of videos
ago when we were making the player animations,
that there is this loop time right here.
So I'm just going to remove it,
save that, run the game.
Should the bullet.
There you go, it explodes on the wall.
There you go.
You can see right now that the impact is played only once,
but there is a slight problem and that is that the bullet impacts
on the wall are all still present and they are not being destroyed.
So how are we going to fix that?
And there is a very simple way of doing this.
And while teaching you how to do this,
I will show you an extremely effective tool inside of the animation.
So what we're going to do is we're going to go into the assets.
We're going to go to prefabs.
We'll add the bullet Impact back. And here.
And what we're going to do is we're going to go back assets, scripts,
and create in here a new C-sharp script which will be the,
let's see, impact, effect, destroyer.
And it's going to be an extremely, extremely simple script.
Let's hit Enter.
And later on, I'm going to be reminded to put
each script and a folder because as we go we'll have a lot more scripts.
I'm going to add the impact effect destroyer.
I'm going to open this up.
I'm going to remove the start and update and I'm just going to do one method.
And that is going to be a public void.
Destroy. And what we're going to do,
nope, not onDestroy, just destroy.
And what we're going to do in here is we're going to simply destroy the game object.
Great, how are we going to call this?
Now, we could call this method from here,
but that's not very effective.
Why? Because when we instantiate the bulletin here,
okay, let's just go through the logic.
We instantiate the bullet upon impact,
and then we destroy the game object,
which is the bullet,
because the player bullet controller is on the bullet.
So the impact plates.
So after we destroy this game object,
we no longer have the player bullet controller.
It is not available and we can demonstrate this.
So I'm going to create this impact fact.
So I just set the bullet created in here.
And after we destroy the game object,
I will try to get
the impact effect dot get component and I'm going to access the impact destroyer.
And from here I'm going to access the destroy.
So this is the method.
And now if I play my game,
you'll notice that we will have an error.
So I'm going to move this and make sure that you apply the changes.
So all the bullet impact on the wall have
this method and look what happens when we shoot in the console.
There we go.
What happens?
We don't even see the impact.
Why is that? Because we've been destroyed.
And if we try to do this before we destroy the game object,
let's look at this.
Run the game.
There we go. So there is no effect.
It's being destroyed before we even see it.
So I'm going to comment this out and we can keep this as it is.
That's no problem.
But I think I will remove this.
And this will be for testing maybe later on you'll
remember how we tried but didn't work out.
So now we have the destroy method in here that destroys the game object.
And how are we going to call this after we finish our animation,
this is the tricky part.
So now if I go to the animation while having the bullet impacts effect,
I can play it and I see that at the end,
you'll notice that when all the smoke is gone,
we have nothing left to do.
What we can do is in here we have this little key and here and I'm having,
okay, so add an event.
We can add an event at some point in the animation,
and this will be crucial later on as we go through the course.
I wasn't expecting to do the solo early,
but no worries about that.
We can add an event.
And when we add an event,
we see right here that we have animations,
event and functions that we can choose from.
And if we click on here,
we see that we have the straw.
Why do we have only to destroy?
This is the destroy,
which is right here.
So what happens when we add an event too,
the animation that we have on a certain object,
we can access all the methods that are present of the public methods right here.
And don't worry if you were a bit surprised on how we
created this later on in the course,
I'm going to go through every single step of creating methods.
But for now, just know that we can access
all the public methods that are available to us on the object.
So what will happen now?
As we play our animations,
we will see the explosion,
the puff of smoke.
And when we get to the last frame,
this event will be triggered,
which is the destroyer.
And what does this destroy do?
It goes in here and it destroys the game object,
which is holding the impact effect destroyer,
which is the bullet impact on the wall.
So make sure to apply all the changes and the override.
I'm going to the lead the bullet Impact.
I'm going to run the game.
And when I shoot a wall,
we should see the effect,
but we don't. Why is that?
Let's go back in here.
Did we save these changes?
Let's try this again.
I think that's because we haven't saved the script.
We run the game.
We see the puff of smoke.
And there you go.
You can see that it has disappeared from the hierarchy.
So now let's test this out a bit better.
There you go, you can see the impact effect happening around us.
And then the whole effect disappears.
And it's much more effective.
It's better than having a lot of game objects in our hierarchy,
taking up space, taking up crucial RAM,
and the clogging up the system.
So I hope you enjoyed,
I hope you are doing your best in each challenge.
I hope you're enjoying the new things that we add every single time.
I'm going to stage all of these.
I'm going to say that added bullet Impact
fact and destroyed it, destroyed afterwards.
Using events with an exclamation mark,
because this is, events are an incredible tool which you won't see very often people use.
So I hope you enjoy it and I'll see you in the next video where we will
be adding the trail effects to our bullets,
hence, making organ much cooler.