It's like just a nice big one.
We're going to go through the process of actually shooting our bullets and flying across the screen,
making sure that we've figured out collision.
And when they hit the enemy, they destroy the enemy.
Let's get started.
The bullet we have floating around in our scene, we can get rid of.
We don't need it in there.
We can work directly on the prefab.
And I'm going to click on that prefab.
Let's see on here.
I'm going to add some components.
First of all, we need to get ourselves a rigid body, 2D, so that we can have this fly across the
screen or leave it as type dynamic.
We'll see how that goes.
It should be okay.
I don't want it to have gravity, although you could if you wanted to have like really kind of droop
off.
That would be funny.
I think so.
Put it zero.
You know, maybe we'll come back to Gravity and play around with it.
This is the art of iterative game design, let's say to add a component which will be some sort of collider.
So I will do a C, I think a capsule collider two D because that's roughly the shape of the player.
I'm going to click into the prefabs, I double click on the prefab.
Here, I can see the collider.
It's a bit messy in its shape.
Click on the collider, edit and then just edit in that.
So we've got something that resembles the player.
Jump back out of the prefab and we need to create ourselves a script.
So what the player is doing with the player movement, with the gun, it's saying, Hey, I brought
this bullet to life.
Now what it does, well, that's up to it.
That's its business.
So I'm going to jump over in the scripts folder.
I'm going to right click create a new C-sharp script, which I will call a bullet.
Obviously, if you have more than one type of gun, more than one type of bullet, you might have to
have different naming, different functionality.
But for us, we're keeping it pretty simple.
Yeah, we just the player can run a long jump and shoot bullets.
I think that's going to be cool for our game.
Let's double click on Bullet, clean up our script a little bit by getting rid of the comments, doing
a little bit of formatting, and to have our bullet fly across the screen, we need to get a reference
to the rigid body.
So I think I want to cash that.
So we will start by creating a rigid body to DX and I'll call this my rigid body, as is our convention
in this section and semicolon my rigid body, I think I've felt that right and then start my rigid body
equals get component.
What sort of component is it?
Well, it's a rigid body.
Two dx parentheses semicolon and in update, what we want to say is, well, make this thing fly across
the screen.
We've done this before with how we run, but this is going to be a lot more simple.
We're just going to jump into my rigid body.
What what thing do we need to do to give something, a movement across the screen?
We can use velocity.
Equals new vector two because we need the two vectors and the x and the y.
We need to tell it what's going on.
In parentheses we will say just we'll start off with some baked in parameters here will say one f comma
zero f to say we want you to fly to the right by one each frame and nothing up and down.
We'll save that and we'll make sure that it's all hooked up properly before we get much more clever
with what we're trying to do.
We'll find our prefabs.
Bullet.
We will add a component which we will call bullet or we won't call it that will search for bullet at
our bullet script on there.
That should be pretty cool.
Now we'll see when we instantiate or spawn or bring to life or birth our bullets.
What's going to happen?
Whoa.
They kind of fly and across the screen.
Good.
So functionality done.
Mission accomplished.
That's all I wanted to do.
There's our game right there.
Now we've got more to do than that.
That's a starting point.
We can do some parameter rising of these things.
So instead of just baking in there, one, we want to be able to change that up to have some sort of
tuning.
So I will call this bullet speed.
I think it's a good name for that bullet speed.
Jump up to the top.
Very first thing at the top we will serialize field and I will create a float called bullet speed.
Will initialize that at something faster than what we had just there will say 20 F now we'll save and
we'll see if that has worked.
If we can parameter rise and shoot across the screen with some velocity.
Pupu are nice and I like that it's rotating.
That wasn't my objective.
What you could do on your bullet is to find your rigid body and you could put some constraints on that
where you can freeze the rotation and we'll give that a go.
Actually, we'll see what happens when we freeze it.
I'm also going to change the collision detection to be continuous because we want to make sure these
things don't go flying through the things we want to be shooting.
So shoot.
Okay, it's going straight.
That's got a different kind of look to it.
It's kind of like a mirror of me flying across the screen.
So rotation or not rotation, it's up to you.
I'm going to have it not rotated for the moment and that might seem like mission accomplished.
However, I know one thing that's going to go astray up here.
If we're shooting right, we shoot right.
If we're shooting left, we also shoot right.
So we need to figure that out.
And I'm going to do a solution based upon what we've already got in our code.
So jump back into the script.
What I'm going to look for is a way of saying what is the current local scale of the player?
So if the player's local scale is minus one, then we want the bullets to shoot left.
If it's plus one, we want the bullets to shoot right.
The same sort of philosophy we've been using throughout in terms of flipping the player and flipping
the enemy.
So what I need to do is to get a reference to the player, and I'm going to do it in a fairly simple
way.
Well, I think it's fairly conceptually simple.
So what I need to do, first of all, is get a reference to the player.
There's a couple of ways that we've done this throughout the course.
We can tag the player as player and then look for the tag, or we can look for a component that is the
player that is only on the player.
So player inputs only on the player, player movements only on the player.
So I'm going to do it that way.
Just to show you a little bit of flavor, there's a few ways to do these things.
Let's look for a player movement object in our world, and there should only be one because there can
only be one player.
So play a movement player.
So it's a type player movement.
Therefore it's going to be the only thing we're going to be getting is the player.
And in start, I'm going to assign that by saying player equals find object of type player movement
and parentheses semicolon.
And doing a find objective type I think is okay every now and again.
If we're doing it in start, that's okay.
You don't really want to have a fine objective type in update that you're doing.
Many, many objects are doing many, many things on every single frame, but in start it's only when
you click the button.
I think this is totally fine from a performance perspective.
This is cool.
What are we going to do with this?
Well, I need to create some sort of x direction or x speed will say so x speed.
And when I say x speed, I made the speed on the x axis.
We're not worried about the y axis is going to equal player.
Remember, that's our player in the scene.
Player transform.
It's the transform component.
Dot what what are we looking for here?
What do we need to know?
We need to know the local scale.
We need to know whether it's positive or negative, to say whether you're facing to the right or the
left.
DOT We're just interested in X.
So local scale is a vector three.
We just need to know the X value because we just want this to, to be one value or one float value.
And I'll multiply that in here.
So I'll say the x speed will multiply that by the bullet speed.
So in one hit right in here, when the bullet comes to life, remember, we only need to know the direction
when it comes to life, when it's instantiated, we're going to say, What is your X speed?
It's going to be the direction of the player based upon the local scale times by the bullet speed.
Now I need to create this x speed as a variable that we can use within update.
So I will create a float x speed semicolon.
Now in our update, instead of flying along at bullet speed, we're flying along an x speed click save.
We'll see if this works.
I have a feeling it will.
Strong feeling.
Pop the player up there so I don't need to jump click on play.
We shoot the right.
Shoot, shoot, shoot to the left.
Shoot.
Excellent.
So that's exactly what I wanted to do there.
Okay.
And the last step of the process, we don't really want our bullets sticking around.
I think when it hits the wall, it should disappear.
Likewise, when it hits the enemy, it should disappear.
Probably do some damage, maybe even kill the enemy.
Let's just do killing enemy.
We won't worry about damage for now.
You know how to do damage from other sections of the course.
So we're just going to straight up hit the enemy.
You kill it.
We can resolve that in either the enemy saying if a bullet hits me, I should die, or the bullet saying
If I hit an enemy, then I want the enemy to die.
And because we're in the bullet script, I'm going to do it that the bullet owns that conversation.
If you hit an enemy, do this.
If you hit a wall, do this.
If you hit something else, do this.
So down underneath update we will void on trigger enter to DX.
So when you collide with something, the first thing I want to know is if what if the other thing is
an enemy.
Let's go have a look at the enemy and see what we can grab.
I'm going to click on the enemy.
I'm going to go to Tag.
So I'm going to add a tag and it tags I'll hit plus and we'll call this enemy enemies.
What are we doing in the past?
Call it enemies.
Call it enemy because this is a tag.
This is one enemy.
Okay, that makes sense there.
So it's just one single enemy.
And so we're mixing a few things here.
We're using tags, we're using layers, we're using a bunch of stuff, which is cool to see how these
things work.
So within here, if the what well, we know when we bump into something, we trigger something, we
get information about the other thing, the collider type collider, the other thing we bumped into.
So if the other dot tag equals equals means the same as or equals.
And in our parentheses we called it just now enemy.
Make sure you check the spelling on that.
If it doesn't work, check the spelling.
Yes, it is enemy on our tag.
Then what do we want to do?
Well, I'm just going to straight up say destroy.
What are we destroying while we're destroying the other thing?
Dot game object.
So what is the game object?
Just destroy it, get rid of it.
And then the last thing I want to do is destroy the bullet.
So the bullet doesn't hang around.
Destroy the bullet if it bumps into an enemy.
So how would we go about doing that little micro challenge for you?
We'd simply say destroy an in parentheses game object, so destroy me basically back over into unity.
And I forgot to do this before I went back to my code.
But click on the enemy change from untagged to enemy so that we know it is the enemy.
Now click on play when we get into the world.
I suspect the bullets are going to stick around if we hit the wall because they are not triggers.
Yes, they are.
That's fine.
For the moment, we shoot the enemy.
It shoots and kills the enemy.
The bullet that hit the enemy died, which is great.
And our bullets stick around.
And I think we can solve that very simply by saying, if you collide with anything, then destroy yourself.
So let's do that.
Just simply on collision into 2D, we'll just say once again destroy and we'll destroy the game object.
So a couple of destroys in here.
You trigger something important.
If it's an enemy, call and destroy yourself.
If you just bump into a wall, then destroy yourself as well.
Let's make sure that we're not accidentally bumping into the player when we shoot or anything else.
We just want to make that.
When we shoot into walls, the bullet disappears and we don't have it sticking around.
Click on play.
And it looks like the bullets are colliding with the ground.
I think.
Now let's go into play a pre-fab.
Before we do that, we'll just make sure we've applied any overrides.
I tend to just work on the instance a bunch and then every now and again apply those back.
We'll go into the prefab for this one.
We will click on the gun and move it down a little bit, back a little bit just so it's not bumping
onto the ground back out of here.
I will also click on the bullet prefab from our folders just make it a little bit smaller.
So 0.75 on the X and 0.75 on the Y.
Now we shouldn't be touching the ground when we shoot it out from us.
We shouldn't be touching ourselves.
There we go.
We can shoot, right?
We can shoot left.
It's a little bit fast, maybe, but can we shoot the enemy?
Destroy the enemy and destroy the bullet?
So we've got the behavior we wanted from our bullets.
It kind of looks a little bit funny, a little bit quicky shooting out little impressions of yourself.
I like it as a bullet.
I think it's cool.
I look forward to see what bullets you're shooting out of your player, so please do share in the discussions
and I'll see you in the next lecture.