Welcome back My favorite game developers to a brand new very long video.
And this one, we have created animations for the blood.
We have learned about arrays.
And now you can see in the hierarchy when we shoot our enemy,
you can see that we have different types of blood being instantiated.
So for example, now the blood to then we have the blood for,
then we have the three.
So what's happening?
We have a lot of Bloods and they are instantiating
kind of randomly because we use an array.
So let's not waste any more time and let's get started.
So time to add some herd effect to our enemies.
Let's go ahead and first of all,
create all of these effects.
So I'm going to go into the sprites,
we have the blood splatter.
I'm going to create a folder for the blood impact splatter.
Maybe we should have called this the death, but no worries,
we'll call this the impact splatters.
So we know that the France and I'm going to go into
the resources and you can see that I've provided you with more blood.
You can either use these and slice them up or you can use these five right here.
So let's choose all of them and drag them down into the folder right here.
Why are we using all of them all?
Because we want to have a bit of diversification between the 1D 2D,
3D and et cetera.
So you can see each one of them is just a little bit different than the other ones.
So I'm going to save this and I'm going to create the animations.
And it's pretty easy I think.
So I'm going to do with and use the magic of editing to do it in 1 second.
So o k, welcome back.
So how did you get all of that?
Let me show you what I did.
So first of all, I created five bloods for the
55 of blood impact that we have now they are not very different from each other,
but there is a slight difference.
So when we run this,
you can see this is the first one.
This is the second one that's a bit different.
The first one is a bit more splashy.
The third one, let's see.
You can see that it falls down more than the second one.
So you can see this is the two.
This one is three.
It's a bit more falling down.
I'm not sure what to call it.
The fourth, you can see it's very small and very concentrated.
And the fifth one you can see it's very GUI.
So what are we going to do?
We're going to save all of that.
I'm going to go into the Prefabs in here and I'm going to create a folder for
the lot backed lashes splashes and I'm going to drag the blood 1,
2, 3, 4, and 5.
I'm going to save all of that.
I'm going to delete them from the hierarchy.
And now we have the blood in here.
Now I think that we will probably have to destroy them and just a bit,
but for now, no worries about that.
We'll fix that when the problem arises.
So what are we going to do?
We want to instantiate these bloods every time the enemy gets hurt?
And the way we do that is first of all,
by obviously getting a reference to it just like we did with the death splatter.
So this will be called the damage effect.
For the damage.
The damage effect.
Let's just keep it at that or you can call the damage blood splatter whatever you want.
And now what we're going to do is we'll simply going to
instantiate it every time we get hurt.
So down here, we are going to duplicate this.
Move it up a bit.
And if you remember how we duplicate Control D,
how did we move this very nicely by holding down Alt and using the arrow keys.
So now we instantiate v.
What should we instantiate the damage effect?
And it's on the transform position of the.
So I'm going to save all of that.
I'm going to go back into our game.
I'm going to choose the enemy here we have the damaging effect,
and let's choose blue one.
Save that.
And now we are going to run the game.
And now I'm going to shoot our enemy.
There we go. You can see the blood.
Obviously it's on repeat.
Okay, so now we need to start instantiating these effects every time the enemy gets hurt.
So where are we instantiating the effect or the impact effects of the bullets?
Well, we are instantiating them in the layer bullet controller.
So you can see this is where we instantiate them.
Now what do we want to do whenever we are hitting a wall,
we have this bullet Impact effect.
But whenever we are hitting an enemy,
we want to have some kind of load effect.
So I'm going to duplicate this line right here.
And I'm going to call this the mature.
I call it the damage fact.
Because this means that there will be some kind of damage
when we have damage means there's blood means there's something different.
And what we'll do in here is every time we hit an enemy,
I'm going to open up the brackets.
And I'm going to add right here that we instantiate and instantiate
the damage effect on the transform position and the transform rotation.
But what we don't want to have as when we're
hitting the enemy and we still instantiate a bullet effect.
So what I'm going to do is I'm going an else statement here.
And I'm going to move this entire line down into that else statement.
How am I doing this?
By holding down Alt and using the arrow keys on one of the lines that I want to move.
So now what happens when the bullet
Impacts or has a collision and that collision has a tag of an enemy,
then we will have this blood splatter
and we will access the animal controller and deal damage.
If not, if it's not the enemy,
which means we are hitting,
I don't know whatever it is.
We are going to have the bullet and back effect.
So I'm going to save this.
I'm going to go back in here.
And here, I'm going to look for my bullet in the prefabs,
prefabs V bilayer bullet in here,
we should have the player,
but I should have the damage effect.
I'm going to lock this in the inspector
so you have this little login here, you can click it.
Now, whatever you press,
you don't change from the player book inspector.
And this is helpful. Why? Because we can now open this file,
choose the blood one,
and let's add it in here into the damage of fact,
save all of that, run the game.
And now we should see that when we are shooting at the wall, we have this impact.
When we shoot an enemy.
There we go.
We have the blood playing on repeat,
which means that we will have to change the animations.
And I didn't tell you that I've created animations.
Can just remove that.
And let me delete this folder.
I had it by mistake.
So splash one, remove this.
Splash to remove this of the looping of the,
oops, I turned it on by mistake. Turn it off.
Turn off. So there you go.
We have all the animations not on looping, okay, save that,
run the game now when we should the enemy,
we should see the blood splatter of blood one.
Okay, So that's fine and dandy and everything is good.
But we have five facts for a reason.
How are we going to make sure that one of them is rendered?
And not only that, how are we going to make sure that
a random number of these is rendered?
So we need multiple objects of blood in here.
Now, what we can do is we can simply go ahead and duplicate this five times.
Damage 123, etc.
So we have five damages that we can call at randomly.
That's a solution, but it's not the optimal solution because for example,
let's say we want to have maybe 17 damage effects.
Do we have to go ahead and create 17 variables?
Well, you can, but first of all, it's very cumbersome.
Second of all, it's not a good way of using the resources.
What is a good way of using resources as creating something called an array?
So what is an array?
An array allows you to store multiple objects of the same type in a single variable.
So we can go ahead and create an int.
So for example, a array of integers,
for example, which is the number of enemies.
So I can store, for example,
4, 6, 11, and 71.
And each one of these little data's in the array.
You can see that we have first of all,
the type just like an a variable,
but the only difference, we add these two square brackets.
We have the name and we have the values.
And each one of those values is or has an index inside.
So for example, the four is indexed at 0.
And remember we always start indexing at 0, not 10.
The six is at index one,
the 11 is at index two,
and the three is at index.
I mean, 71 is at index 3 and we can have as many as we would like.
So now that you have a bit of an understanding of what arrays are,
and one more thing we can determine the length of an array.
So for example, the length of this array is four because we have four values.
So out of the way, this little explanation,
let's try to implement it a little bit and you can see how that works.
So I'm going to go back and unity or actually no in here.
And how do we convert this damage effect to an array?
We simply add these two square brackets.
And now I think it's appropriate to name this not damage effect,
but damage effects with an S on the end,
because it's plural now.
So we'll use our handy tool of control or, or,
and we'll simply add an S at the end, hit Enter.
Now the damage effect here is different,
but you can see that the instantiation now is wrong.
Why? Because we are trying to instantiate an array of effects which does not work good,
but we will fix that,
or actually you are going to fix that later on in the challenge.
First of all, let's add all of the damage and see how it looks in the inspector.
So if I go to my bullet,
in the prefabs player bullet,
you can see right here, where is it?
So something is wrong.
What is the problem?
And the console? Let's double-click and see.
Oh, so let me just comment this out.
Save that sometimes when you have an error,
the script doesn't assemble properly.
So now that we fix this problem,
and we can see right here that we have damage effects,
no longer a single element,
but we can open it up and have a list of elements.
What we can do is we can say that the array size is two. Hit enter.
Now we have two elements,
or we can do it the old-fashioned way and hit the plus right here and add an object.
But I will show you an even nifty trick.
I'm going to lock the inspector in here.
I'm going to go to my prefabs.
I'm going to click on the first one,
hold down, shift, click on the last one.
So I have all of them selected and I'm going to drag them to the damaging effect.
And voila, now you have why did I use water?
Now we have the 50 bloods and you can see the elements numbered from 0,
1, 2, 3, and 4.
Now the next step is to instantiate one of them randomly.
And I can show you,
first of all, this will be a challenge.
I'm not sure which I should show you first.
Let me first of all, show you this.
So we have something called random range,
which returns a random number.
Go ahead, look at up.
Now it's time for your challenge.
And your challenge is to create a random effect.
So number one, create a random splash from 0
as the minimum value and the length of the array as the maximum value.
So now you're a bit confused.
What is Michael talking about?
Again, we go back to this one.
The minimum, the maximum.
There you go. Use this, look for it.
Back in here.
Assign that value to an integer variable that you will create locally.
So create a random integer,
assign it a random number.
Look for random latrines 0.
Okay, so I've added that.
And finally, I will give you a hint.
If you don't want the hint,
It's about how do we get the length of the array.
I'll give you the hint. If not,
pause the video, go to the challenge.
You want to have the hint, use dot length to get the length of the array.
You can even look up a couple of examples are no,
this is a bit of a hard challenge.
I would add on a scale of one to 10 as a beginner,
this is a 10 because first of all,
you've just learned arrays.
Second of all, you are using random.
I know it's hard.
Take your time, pause the video,
use your solving problems skills,
everything that you've acquired,
and don't worry about it when you finish the challenge.
If you don't know how to do it,
I'll do it in front of you, but try.
Most important, at least give yourself five minutes to try.
So pause the video right now and go do your best and the Chaldean.
Okay, welcome back.
So whenever we hit an enemy,
what we're going to do is we are going to create a random variable and locally,
so locally means inside of here,
every time we hit an enemy,
I'm going to say that we have an integer variable,
which is going to be the selected splatter.
And it's going to be equal to a random range.
So just like we have in the examples,
and Unity's documentation is going to be between 0 and the death splatter or no,
what we call it, appear the damage affects.
The damage affects dot length.
Save that.
So now what happens in here,
we have this integer.
It's equal to a random range.
And you can see right here that we have the minimum.
So why is it 0? Because we want to choose
a random number from the array that we have in here.
And the first index is 0,
and the last index is going to be the damage length.
But there is something very important that I want to point out to you.
And if we look here at the explanation,
you can see that the minimum is inclusive while the maximum is exclusive.
What does that mean? Inclusive?
That means that we can choose a random from 0 to the maximum.
So 0 is inside.
0 can be one of those random numbers.
But the maximum, which is the length, cannot.
And this is important because indexing in an array,
if we choose the length,
the length is always bigger than the last index.
Because if I go back and the example in here, let me show you.
So let's say what is the length of this entire array?
It's four because we have four points.
But if I try to access the fourth number in this array,
I will get an error because we have the index is 0, 1, 2,
3 if I try to access the fourth,
so for there is nothing,
I get an error and I start crying.
Not necessarily, but I hope you get the point.
Next thing we're going to do is we want to instantiate.
So now that we have our random number,
what we can do as from between the effects,
I can open up the brackets and I can use the selected splatter random.
So now I have a random number which is between 0 and the length minus one,
obviously because it's exclusive.
And now I can instantiate the damage effects
or one of the damage effects randomly using this number.
So I could have said the 0 V1,
but I've selected the random splatter.
So let's save that.
I hope you understand everything.
If not, just take another look at it,
maybe replay the video, understand everything.
I've tried to be as detailed as possible while being a boring.
I think this is one of the longer videos, but no worries.
I'm going to go into my game.
We're going to remove this right here is like the enemy.
And by the way,
I also made sure that all the blood you can see right here,
the sorting layer as the enemy.
So you can see it. And where's the enemy?
I'm going to give him a thousand or 10 thousand health
just so we can see properly all the blood splatters.
So I'm going to get there but close,
I'm going to shoot 12345.
So there you go.
You can see the something isn't completely right,
so I'm going to shoot.
And what is the problem with this 1, 0 case?
So I just found the problem and the blood 5 I've added right here, the blood.
Look when you play the animation,
the last one is a red dot.
So what I'm going to do,
I'm going to remove the last one. And there you go.
Now, we only have this when the blood finishes at the spears.
Save all of that.
And let me just choose the moon.
The blood 5 and a live changes.
Delete it again in the bullet. Let me see.
Maybe because we changed something, maybe prefabs bullet.
Okay. Still working, save that,
go back in our game.
Obviously we'll need to remove all of these so a shoe,
the enemy, let's see.
We have different bloods.
Nope, it's still there for some reason.
Why is that? It's very annoying.
Okay. So I discovered that the blood for
also has its last frame as this red annoying dots.
So I'm going to delete it and save that.
I'm immediately deleting it inside of the prefab.
So I simply go to the prefab,
double-click on the three.
Let's see if this one also has the little annoying red dot that you go up.
Okay, So there you go.
It's also has it save that and blood to zoom and see the animation.
Okay, another bloody work.
This video is getting out of control.
So there we go,
the last one, we also delete that.
And one thing I noticed,
I want to choose all of the prefabs and make sure that all
of their positions is at 0 so they don't get instantiated.
And some weird place.
Anyways, back to our game.
I hope this time everything should be working perfectly.
So I'm going to run the game and I'm going to shoot the enemy.
There you go.
This is the thick blood.
Sugar them again.
Again, it's a different kind of blood. You can see it's blocked.
Three.
Blood for Blood one.
There you go. You can simplify to again, blood one.
So as you can see, whenever we are shooting the enemy,
we have different types of blood's getting instantiate.
So you can see it's a random number getting shot out.
So I hope you enjoyed.
Obviously, there are a couple of things that we will need to
fix with our blood not getting destroyed at the end,
we might find a different way of fixing it once we learned some extra tools,
or maybe we can just remove them using the events and our animations,
which is also very helpful.
So I hope you enjoyed,
I hope you're enjoying this course.
I hope you're not being a bit demotivated by the length of the,
some of the videos because I go into a couple of details.
I hope you like the fact that I make mistakes and keep them in here so we can
fix them together and leave me a review if you are enjoying.
I know I've said it before.
I will maybe say a couple of times,
so make sure to do the review so you don't have to hear it anymore.
And make sure to of course, do the commits.
I'll see you in the next video where we will be giving the enemy and ability to shoot us.
So currently he is very weak.
We want to give him a way to defend himself.
So see you then.