Well game is now coming along nicely.
We can dodge the enemies, and if we hit them, we die.
But now let's step it up a notch by adding some projectiles so we can both shoot at the enemies and
they can shoot back us.
Before we start coding up this system, though, there's a little bit of prep work we need to do beforehand.
The first thing we want to do is to create some projectiles that we can instantiate into our world.
So over in our hierarchy, let's right click and create a new empty game object and call this the projectile.
Let's reset the transform.
If it didn't come in at 000 and then underneath this object, we're going to add our sprite.
We have a whole bunch of sprites in our assets pack folder.
So if we dig through into the PNG folder and then into lasers, we have a few options here.
We have a whole bunch of blue lasers, green lasers and red lasers.
But because our player is predominantly blue, I'm going to go with one of the blue options and I quite
like the look of this laser blue oh seven So I'm going to drag that underneath my projectile.
This is pretty small in our game world, so let's scale it up a bit and I'm going to go with twice the
size.
So just scale that up to two on the X, Y and Z.
And then if we head back to the root of this object, we need to think about some of the components
we're going to want to add.
Well, this projectile needs to be able to interact with our enemies in the world.
So we're going to be setting this up as a trigger collider.
This means we're going to need a rigid body, 2D, and just like our other objects, let's set that
to be kinematic.
And we're also going to need a type of collider given the shape of our projectile.
I think a capsule collider two DX will do the job and if we zoom in on our projectile, then we can
go ahead and edit the collider to fit our projectile.
So let's just drag in our handles.
And if you're wondering how I'm managing to do both sides at the same time, just need to hold out the
alt key for that.
With that looking about right, the last component we want to add is going to be our damage data because
our player projectiles are going to need to be able to damage the enemies and the enemy projectiles,
which will be very similar to this.
We'll need to damage our player.
Let's leave the default damage at ten for now.
And with all of that set up, we can go ahead and prefab our projectile.
But it's just drag that over into our Prefabs folder.
And I'm actually going to rename this prefab projectile player because now we're going to create a second
one for the enemy.
And this really is as simple as duplicating our prefab with control.
DX And then let's rename this to Projectile Enemy.
If we open up this projector, we're going to want to change our laser blue prefab to one that suits
our enemies.
So let's head back into our asset packs into the lasers folder.
And for this one, I'm going to use the same type of laser, but I'm going to go with the laser red
seven.
So let's drag that in, delete our blue prefab and then rescale our red one.
And since we duplicated our original projectile, we don't have to worry about setting up all of the
colliders and the damage data.
One thing I am noticing, though, is that the colliders should be triggers.
So let's actually go ahead and just fix that on both of our objects.
So is trigger on our enemy projectile?
And if we jump into our projectile, we can set this to a trigger as well.
So that's our projectile mostly set up.
But if we do leave it like this, we're going to run into a bit of a problem when we start instantiating
them via the code.
And to show you what I mean, let's go ahead and hit play and then pause very quickly.
And if we move our projectile around, so let's zoom out so we can see the full level.
And move all projectile out of the way of the enemies.
If we unpause our game, our projectile can actually interact and hit our player.
This deletes our projectile from the scene and damages our player.
And this definitely isn't what we want.
We only want the player projectiles to interact with the enemies and the enemy, projectiles to interact
with the player.
So how do we go about doing this?
Well, the first step is to set these objects to be on specific layers.
So if we click on any object in our scene, we can see up in the top of the inspector that they're all
on layer default.
If we click on this dropdown and then head to the bottom add layer option.
What we're going to do is we're going to set up two new layers.
We're going to sell one for the player and one for the enemy.
With those two sets up, let's click out of our inspector so that that disappears.
And now in our prefabs folder, let's select our enemy and our enemy projectile and set both of their
layers to be the enemy layer.
This will give us a warning message asking whether we want to change the children or just the parent.
Let's go ahead and change the children as well.
And then we want to do the exact same thing for our player and our player projectile.
But this time we're going to set our layer to be the player layer.
Again, we can go ahead and change the children.
And the reason we want to do this is because we can tell unity what objects should interact with each
other based on their layer.
So we can tell the player projectile not to interact with our player ship and we can tell the enemies
not to interact with each other as well.
To do this, we need to head up to edit and then down to product settings.
And on the left side, we have physics to.
Now we also have a physics option as well, which has some very similar stuff in there.
But because our project is 2D, we want the physics 2D option.
Then if we scroll to the bottom of this list, we'll find the layer collision matrix.
If you can't see that, then just expand this option and then scroll down a little bit.
And this collision matrix here tells Unity which layers are allowed to interact with each other.
And there's various different reasons why you might want to change things in here depending on the project
you're working on.
But we said that we didn't want the player projectiles to interact with our player ship and the enemies
to interact with each other as well.
So in this matrix, we want to say that the enemy can interact with the player, but it can't interact
with other enemies, and the player can interact with things on the enemy layer, but it can't interact
with other things on the player layer.
With those options set, we've also fixed another minor bug that was in our game before.
You may or may not have noticed this depending on how you set up your waves, but after we added our
damage data and our health system, it turns out that the enemy could collide with another enemy and
destroy each other.
So this will also fix little problems like that.
With that set, let's close our project settings and now let's test this out to show that it actually
works.
Let's move our projectile and hit play.
And now if we drag our projectile over our player, nothing happens.
But if we interact with an enemy, then the projectile gets destroyed and the enemy will take some damage.
Next, you may or may not have seen something odd, so let's go ahead and hit play again and notice
that when we move the projectile over the player, the projectile is on top of the player.
This looks a bit strange and we really want the projectile to be instantiated underneath our ships rather
than on top of them.
So have a very quick think about how you might go about fixing this.
Okay.
I'm not going to give you too long.
What we're going to do is in our projectiles, if we head down to our sprite, we want to make sure
that the projectiles are on our order.
Zero.
This should be the case by default, but let's double check both of them.
And to make sure that these appear underneath our ships, all we need to do is bump up the order in
layer for our ships.
So let's head into our enemy ship and change the layer in order to one and then head into our player
ship and again, change the sprite order in layer to one.
Now, if we hit play one more time.
If we zoom in on our player and move our projectile, we'll now see that the projectile appears beneath
the player.
So when we eventually get around to instantiating this in code in the next lecture, the projectile
will appear underneath the player and then shoot out and eventually hit an enemy.
So that's our projectile pretty much sets up.
We can go ahead and delete our prefab from our hierarchy because we're going to instantiate those in
code in the next lecture.
But before we finish up, there is one more thing that I want to do, and that is to just tweak our
input system ever so slightly.
So if we head over into the input system folder and open up our input actions, what we're going to
do is have a look at the firing action.
If you remember, all of this was kind of set up by default when we first added our input system and
we didn't really touch much in here.
But if we have a look at our firing action and then expand this dropdown to see which buttons and inputs
are attached to it, we'll say that we have a left mouse button option so we can fire when we click
the left mouse button.
But there's currently no keyboard binding in here.
And for our game, I think it would be nice if we had something like the space bar set up for firing
to do this.
All we need to do is click on the plus icon for our fire action, click on Add Binding and this will
add a blank binding to the bottom of our list.
Then over in the properties window we need to set up the path, which is the key that we're going to
be listening for.
And we could find it in this list in here.
We could dig through our keyboard and find the spacebar, or we can just hit and listen, press the
spacebar, and then it comes up in our list.
So let's select our spacebar for our path, and we can also select keyboard and mouse to tell our input
actions that this is to be used with the keyboard and mouse control scheme.
With that set up, the final change that we want to make is if we click on the fire action again and
look over in the properties, we can see that it's got an action type of being a button.
And the way this action type is going to work is that when we hit our spacebar to fire, it's going
to register the key down event.
However, it isn't going to register the key up event.
And for our firing action, we're really going to need that key up event so that we can tell the game
when to stop firing.
So instead, let's change the action type to be of type value.
This is the same action type as our MOVE action.
And remember that this triggers both when we press the key down and when we lift the key up.
So we can now use that information to tell if the key is down and should be firing or not without input
action sorted.
Let's go ahead and save our asset and do remember to save unless you have the autosave option checked
and then close our asset window with that set up.
We're now in a very good place to start coding up our shooting behavior.
And whilst it may seem like we didn't really get too much done in this lecture, we actually set up
a lot of groundwork and saved ourselves a lot of potential headaches and bugs going forward.
And in the next lecture, we're going to start generating these projectiles at runtime.
So I'll see you there.