This lecture was starting the process of shooting, and I'm going to be shooting a bullets that look
like myself, because that's the sprite that I had in my library and I didn't have a bullet.
And I think it's going to be pretty cool and funny for now.
We're just instantiating the bullet in this lecture, so let's get started.
Originally, I wasn't planning on putting shooting into this project, but I thought, you know, it
would be cool shooting.
So hey presto, we're going to do some shooting.
Hopefully that's okay with you.
What I'll do, first of all, is have a look at the player.
I'm going to go into the player prefab for this design.
I'm looking in the prefab.
What we need to say is here's a spot where the bullet will be spawning or instantiated and where it
will be shooting from.
You could do it just in the middle of the player, but sometimes you want to have a gun, so we'll do
it the the quote unquote proper way of giving it a gun.
So when we click on the player, right click Create Empty, that will create an empty game object as
a child should be zeroed, which is good.
And I'll rename this from game object to be Gun.
You could also call it bullet spawn point, but you know, that's kind of what a gun is, isn't it?
A bullet spawn point?
So I'll click on the gun and I'll just move it a little bit to the right and a little bit down in the
spot where I think the bullet should be flowing out.
The character doesn't have a gun.
It's not that sort of visual I'm looking for.
I just want it to be starting a little bit away from the player.
So I think that's fine.
Now back out of the prefab, click on the little arrow next to the prefab back into our main area.
I'm going to create a bullet.
And for this we need a prefab so that we can instantiate it time after time when you push the trigger.
So I didn't I don't actually have a bullet in here, so I'm going to do something a little bit dumb
and a little bit silly.
I'm going to use one of the player's sprites as the bullet.
So you kind of shooting little projections of yourself.
So let's have a look and see which one we want to use.
You could shoot little projection and it could animate as well if you wanted to be super clever about
it.
There's a little extra challenge for you, but I'm going to find let's see, I think this one is fine.
Number 22, just a little character running across the screen.
This one's a good number, 19.
So I'm going to grab number 19 and a drag, just that single number 19 into the hierarchy.
I'll rename it to B Bullet.
We need to give it a particular sorting layer so we can see it.
I'll put it on the same layer as the player.
That makes sense.
You can see the player's bullet at the same layer is the player.
We need to give it some sort of collision as well.
So I'll add another layer and I'll call this one bullets.
Click back on bullet, change this to be on the bullets layer and then jump over into project settings.
So edit project settings just so we don't forget later on.
So I don't forget later on.
It's more the case.
Why we want bullets to collide with ground.
Yeah, probably.
I want them to bounce off the walls and stuff.
That's cool.
And off the ladders.
Off the ladders player.
No background.
No bouncing enemies.
Yes.
Hazards.
Yes, might as well.
And bullets.
Yes, you can.
Bullets can collide with bullets.
I think that's fine.
Okay.
X out of that.
So we've got the first step now we need to turn our bullet into a prefab.
So click over in the prefab folder.
We've just got the player I'm going to drag bullet down into there.
Now we have a bullet prefab.
It looks like the player.
So what I'm going to do on my sprite renderer for the bullet in the in the prefab down here in the folder,
I'm going to change the color to be some sort of red kind of color.
And here we go.
So you're shooting out little red bullets of yourself, so it's nice and clear what's going on.
Okay.
And I'll probably need to change the scale.
We'll come back to that later on after we've actually started shooting it and flying it across the screen.
So I've got some bullets.
We've got the player next.
We need to have a look at our input system to see how we're going to trigger shooting.
If I double click on my input system, the little roadmap thing there and I've got in here already in
action, which is fire and you can see the controls for us on our keyboard.
I've got left Mouse, so I'm just going to use left mouse for now.
Might do something later on.
I might change it to a certain key on the keyboard.
That's fine.
For now.
We're sitting in our keyboards working on this project, so that's cool.
We use the mouse and we need to jump over into our player movement.
So there's a few ways we could do this, but the way I'm going to do it is the simplest conceptually
where we're just going to say on fire.
Then we instantiate a bullet.
So here is a challenge for you to spawn our bullets, specifically use instantiate to spawn our bullet
at our player's gun when the player clicks the trigger, or in other words, we are calling on fire.
So there's your mission.
If you choose to accept it, I'll see you back here when you've given it a shot.
Ryan.
I just realized I made a really accidentally terrible pun just there.
Give it a shot.
I didn't mean to say that, but I guess subliminally, my dad brain just never shuts up, does it?
So.
Ha ha.
Now, let's see.
What have we got in here?
Update on.
Move on, jump.
I'll put this at the top so it's nice and clear.
So it's going to be on fire.
And in parentheses we need to grab ourselves an input value as we've done before.
We've got clues just down below ourselves here input value.
And this will be value and be very similar to the jump because it's just tapping a button, right.
So very, very similar to that.
Get our curly braces as we've done before.
We just need to have our if you're not alive, then you can't do these things.
We'll pop that in there.
So if not alive, then return.
If you are alive, then we want to instantiate a bullet.
So instantiate.
What are we instantiating?
Well, it's going to be bullet.
We need to go and create a reference to that.
So up at the very top we will serialize a field.
This will be of type.
What is this?
I think this is going to be a game object that makes sense.
Game object.
And we'll call this just straight up bullet.
So that's how we need to instantiate something.
We need to have a reference to it, and we're going to drag that into the inspector so we know exactly
what that's going to be.
I'm going to pair up the bullet with the player and then also while I'm here, I'll get to this in a
moment.
Now instantiate method, but we also need a place to instantiate it from.
I know that's coming in a moment.
So I'm going to serialize field a transform.
Call it bullet point.
You know what?
Let's just call it gun.
Only called a bullet spawn.
But what is it?
It's the gun.
Crazy.
So now instantiate.
We need to know what we're spawning.
Where are we spawning it?
That'll be gun dot position.
And when do we want it?
Now.
That's the wrong song.
So what?
Where and rotation.
So rotation would simply say transform rotation.
So whatever rotation the bullet has when it comes to life, that's a rotation.
We're going to use parentheses.
Our on fire squiggle should go away.
If I've done it right, it's not going away yet.
That's because Rick has forgotten the return type.
Sorry about that.
Forgotten that a few times in this section.
Void on fire is what we're looking for.
Save back over into unity.
I'm not looking for it to fly across the screen just yet.
In this first step, we're just looking for a to come to life back over onto our player.
We need to assign those couple of fields that we are serialized.
So you see down here play a movement.
We've got Bullet and we've got Gun Bullet.
We need to grab the prefab, remember, because the prefab is going to be spawned from nothing into
existence and the gun will be on the player.
So we find we open up the player and we say, here is the gun that's on the player.
Now I'm going to get rid of all my errors.
That was from me clicking play prematurely before I'd done those things.
Now I'll click on play No Errors anymore, and if we click on Trigger, we instantiate some little versions
of ourselves.
Excellent.
That's going to be funny and interesting.
Trust me, when we actually shoot it across the screen for now, it looks a little bit dumb, but I
think it's kind of nifty.
Feel free to have an actual proper bullet in there and not be spawning a sprite of yourself because
you didn't actually have a bullet like I did.
So there we go.
We've got the first starting point of creating shooting.
In the next lecture, we'll give it the meaning of flying across the screen.