[SOUND] Hello again.
In this video we are going to make our player shoot, [SOUND] so
that we can start looking like an actual proper grown up space ship.
Okay, let's make our player shoot!
The first thing for us to do in this process is to build ourselves a projectile
and that will be your challenge, set up your projectile.
Create a laser prefab, so you'll need a sprite to use as the laser.
And then to create a prefab, you'll need to create a new variable
in the Player.cs script to store a reference to the laser prefab.
And then to connect the Laser prefab, I just noticed for
those of you out there who are sticklers for capital letters as I am, there we go.
[LAUGH] Just make that change, so it's not driving you crazy.
Okay, Laser, with a capital L prefab.
Connect the Laser prefab to the Player prefab.
So we've done this a few times, it should be straightforward.
If my description's a little bit confusing, then take your time,
have a look at it, read through it, have a think about it.
But you'll need to create a prefab.
You need to put a little bit of code, just a variable in the Player.cs, and
then to connect the two together.
So pause the video and take on that challenge now, if you will.
Okay, so we're gonna find our Laser prefab,
our Laser Sprite from in the Kenney Asset pack.
And you know what I'll do?
It bugs me that this is right in the middle of my assets here,
it's a little bit small over on the side.
I'm going to rename this.
This is a little bit clumsy, you don't have to follow this but
I'm just gonna call it z, so it's definitely end.
And I'm gonna call it Kenney Assets, call it what it is.
Call a thing a thing, I say.
And now that's just gonna be sitting down there and as I fill out my assets folder,
I know that's gonna be at the end.
I'm not referring to it, I'm just grabbing things from it whenever I need it.
And what I'm going to grab at this moment is to go into PNG's find in my PNGs in
here, a little bit more screen real estate, please.
Find the Lasers, I'm gonna go for [SOUND] laserBlue03,
drag that into my Sprites, cuz that will say, I am using this.
Close up the Kenney assets, we're good there.
And you can see in my Sprites, I have a new fancy laserBlue03, excellent.
I will drag that.
I'll first drag my window a little bit so get a bit more scene space.
Drag that into my hierarchy, it's called Laser blue.
I'll rename that to Player laser, Player laser, cool.
And that is now a Player laser.
If I look at where that is at in my scene, I'll click on W.
You can see it's right in the middle, and excellent.
Now that I've created a game object that's Player laser,
I can drag that into my assets, I don't have a prefab folder yet.
Okay, let's deliberative naming and foldering and
tidying upping at the moment.
So folder, we'll call this Prefabs, that's where our prefabs will go.
While I'm here, I'm going to drag my player, open that up,
drag my player Laser prefab.
Now, we have a prefab.
Also, while I'm here, drag the player down, make that a prefab.
Should always make everything a prefab if you can.
Drag the enemy down and drag the canvas down.
The event system we won't worry about.
And the space background tall, that's not the greatest name.
I'll call this Blue background just to rename it.
Blackground [LAUGH] background, Rick, okay.
Blue background, drag that over in as well and also the main camera.
And I can see where it's still called sample scene.
Right click on Sample scene.
Now you have to find it our assets, in scenes, right click on Sample scene,
rename and we will call this just Game, I think for now it's cool.
Okay, and then save.
So I've created some Prefabs, we have our prefab folder.
We've got our laserbeam here, we're calling Player laser for the moment,
everything is good.
Now, on to our player, we find our Player script.
Open up that Player script anything, I have Visual Studio open at the moment.
So take a moment to open that up.
At the top of our script where we have our parameters,
I'll just pop this in here, this is our configuration parameters.
Okay, you don't have to put that heading in there, but
I find it helps us keep things nicely grouped.
We'll create a variable, we'll SerializeField,
not serializable, Serlializefield.
Excellent, and this will be of type, now let's see, what type is a prefab?
A prefab is a GameObject, so GameObject, and
I'll call it laserPrefab, just to be very clear it's our laser prefab.
Save as we do.
Excellent, so we've got our laserPrefab.
The last step in the challenge I gave to you is to find the Prefabs and
connect our Prefab to Prefab.
So I don't need this player laser that's hanging out in my scene now.
I can delete that cuz I've got a Prefab and
I can very easily drop a new Prefab back into my scene whenever I like.
I will find the Player.
The Player should now have a laser prefab which is empty,
click on the selector, find our player laser and now we've hooked that up.
We're not going to do anything just yet.
But we've successfully given our Player a Laser that we can spawn.
Right, so how do we spawn it?
How do we instantiate our Laser?
Well, first of all we need to, in our update method, to create a fire method.
Fire(); while I'm here as well, I don't like this flow.
I like to be able to see my start method and
then my update method one after each other.
And we're not gonna be touching the SetUpMoveBoundaries really anymore.
So I'm gonna highlight all of this, cut, drag,
go all the way down the bottom of my scripts.
Give a bit of space.
Pace setting and setup move boundaries is down there,
happily hanging out on its own.
I can also click on the little selector thing here to close up so
I'm not looking at it.
If you mouse over the three dots, give you a little bit of a thumbnail of
what is within this method or what's within the thing that we've closed up.
Okay, just tidying things up a little bit here.
It's kinda the theme of this video, isn't it?
Tidying things up.
So we have our Fire method, right click on that to Quick actions
to Generate the method, right underneath, we'll see Fire.
Delete the placeholder throw exception, and
now we can say what happens when we fire, and how do fire?
How do we fire in the first place?
Well, this is gonna be an input from the Player.
And when we do the input, something will happen, so this sounds like a conditional,
an if condition.
If statement, so if (Input.,
we're gonna use GetButtonDown).
GetButtonDown, what's the button that will be using if I
get back to Unity up the top edit.
Project settings and
input as we do before without axis when we are doing horizontal and vertical.
You see there is a Fire1 option in here.
Basically, saying we can Fire option number one,
there is also a Fire option number two and three.
We use Fire1, at the moment the button,
we've got a positive button which means do the thing, left control.
While I'm here, I'm gonna change this to space cuz I wanna shoot with my space bar.
We can also have an alternate positive button.
Here we have a mouse, so at the moment, so you can use the keyboard and the mouse.
We look for our other Fire1 down here, It's set up to use the joystick button 0.
So at the moment, we have it set up for keyboard,
mouse and joystick, which is cool.
And I just want to change from Ctrl to Space for the button that I use.
I like Fire1, this is the name of our axis copy back over into our script and
within GetButtonDown quotations and then paste in Fire1.
So what happens when we're pushing down Fire1, in our case
Fire1 is the space bar or the left mouse button, well, give ourselves extra line.
Open our curly braces, we want to instantiate.
So we want to create something.
We're going to create or instantiate our Prefab, our laser that we just created.
So we'll say instantiate, and
then we can see that there's a ton of options for how we instantiate.
What we'll do is say, what's the thing we're instantiating?
The laserPrefab, and then where do we want it to be?
We want it to be currently where the player is, so transform.position.
We might need to offset it a little bit.
If were zoomed in closer,
you might see that it's shooting from the middle of the player.
But for now, we'll see how it goes.
It's probably moving pretty quickly and we won't notice that.
So we don't have to add an offset just at the moment.
And then comma, and this is where we need to put in the rotation.
Now we're putting in Quaternion, Quaternion.identity,
which basically is saying, just use the rotation that you have.
We don't need to apply any rotation over the top of that.
That's how we say, just no rotation,
thanks very much, Quaternion.identity and then semicolon.
Let's save, jump over into Unity, and see what that's gonna do for us.
Click on Play.
Okay, give a bit more space scene over here, as I move around when I tap down,
if I hold, nothing happens.
If I tap, it instantiates these laser beams, cool!
Now, we haven't told it to do anything.
We haven't said, go flying up, [SOUND] into the screen.
We also haven't told it to make a [SOUND] noise just yet so
that won't be happening just for the moment.
If we click on our Laser, the first thing we need to do is to give ourselves
a mechanism with which to push it around the screen.
That mechanism is a rigid body.
Rigid bodies are important if we want to move something around,
so it's a rigid body, I already have typed a rigid body 2D that we apply.
Okay, and then back over into our Player.cs,
underneath our Instantiate, we need to do something with our laserBeam.
Well, at the moment when it's instantiated,
it's instantiated as an object.
We want it to be a game object so that we then have a handle or reference or
way to do something more with the thing we've just instantiated.
So we're going to create our handle by saying creating a new local
variable of type GameObject which we will call Laser= this instantiate.
And at the end we want to say as a GameObject, to make it very clear that
we're creating this way, constructing this as a GameObject.
Now, this is a big old lines.
What am I gonna do is put each of this parameters on its own line.
You can do it however you want, we've talked about this before.
Unity doesn't mind, doesn't mind about the white space.
But this I think, as on that other one, nah we'll put it here.
That's fine, yep, okay.
So we just don't wanna go all the way over to this side of the thing,
it gets a little bit too long.
And if I was to zoom in a bit, you guys, wouldn't be able to see it at all.
Save that up.
So we now have, we changed this so that it is a game object that we're instantiating.
We also need to then say let's move this GameObject, and
the GameObject is laser, but we won't do that.
Well, first, will have to get the component, and
the component our interested in is the Rigidbody2D, correct.
What do we need to do with the Rigidbody2D?
We've done this before, we need to give it some velocity.
Velocity is gonna be what?
Well, it's gonna be a new Vector2 And
then the Vector2 is going to be looking for an X and a Y.
The X is the left-right.
We don't need to do anything there so 0.
And then the Y, we want to give it some sort of speed that we can tune.
So we'll call this a new variable that will be called projectileSpeed?
Yeah, projectileSpeed I think is good.
We haven´t created that yet so I'm going to copy this.
Jump up to the top where we have our configuration parameters serialized,
not serialize.
Why do I do that every time, apologies.
SerializeField, this will be of type float and
paste into the projectileSpeed;.
So this is our projectile speed, I'm gonna initialize this at something so
it´s not just sitting there at 0.
I will initialize this at 10f.
No idea, just having a guess.
Put the f on there cuz it's a float case.
Save this all up, just have a look to make sure this looks cool.
We're instantiating and then we're telling it to have some velocity.
Save, back over into Unity,
click on Play and whatever we got.
Okay, as I shoot on pushing my space bar, we can see of course these kind of I'm
lobbing some granites not exactly what I want to do.
So let's have a look at the projectiles rigid body,
there are two ways to resolve this.
If we wanted to leave the body type as dynamic, so
that we could have some physics applied to our laser beams.
You might want to do that, then let's have a look.
We've got some linear drag, we'll put that to 0.
We don't want any drag, and also we dont want any gravity.
So this is where I'm gonna override the global gravity and
say no gravity on our lasers, thanks very much.
And arguably the better way to do this would be just to change the body type to
kinematic.
That way we won't have any gravity apply to our rigid body at all.
Say, you just changed your body type to kinematic,
now click on Play it should go [SOUND].
Nice and well I say we're not doing a sound effect just yet.
So it might go [SOUND] or just go silently off in the distance.
That looks very good, one last tip I'll do, click on my player and
find the projectile speed.
Increase that to 20 for now.
You can see that it's bold, that means it's not applied to the prefab.
We'll apply that back to the prefab, click on Play,
this should go flying off the screen.
Nice, I think that's a good speed for our laser at the moment.
Now you notice if you hold down space, or whatever you've told you want
the fire to be, It fires just once, cuz it's on key down just once.
Now, if we want it to shoot lots we have tap, tap, tap.
Back in the day, back when I was a youngster,
this is how we had to play video games.
Tap, tap, tap, tap, cuz someone thought would be fun to do.
It's not fun anymore to do that, we want the player to be hold down and
have our lasers fire as if we're tapping.
And that will be the topic of the next video.
So great work in this video and
I will see you when we're talking about coroutines in the next video.