Welcome back to part two of this tutorial on acceleration.
What we're going to do now is prepare the shell for destroying itself as well as hitting things and
being able to move.
So we're going to start by selecting the shell prefab that we've been using all along and we will come
over into the inspector.
We have the Move Shell script attached to it.
We're actually going to turn that off and not use it.
We're also going to add to it a collider.
So we want a capsule collider to sit around the shell and we also want to add a few scripts that we've
already got in our starter package.
So if you go into scripts, I've moved the ones I was working on before, which was Fire Shell and Move
Shell in to here, you'll find that there's a shell script and a destroy shell script.
Grab the shell script and the destroy shell script and put them on to your shell.
So let me just make the shell in the world there.
And if we just zoom in on it, we can see where our capsule collider went.
So it's not quite fitting.
You might want to just rejig it a little bit.
So it probably needs to just work with the radius.
I think it needs a direction of the Z axis, so it's the other way.
And then we can work with the.
Radius and height to get it to fit.
So there's the height and bring that radius down and then bring the height down just a little bit.
I mean, it doesn't have to be that accurate.
Then you might want to move it in its Z direction back along the shell a little bit.
Okay.
So we've got that now with that shell there in the hierarchy, we can open up our scripts and drag and
drop the shell script onto the shell and then also the destroy shell script onto the shell.
Now select that shell and find the shell script.
You'll notice that there's an explosion just here.
We're going to select the explosion in the Prefabs folder, which is called Shell Explosion.
Drag and drop that on to that explosion script.
Now that we've finished with the shell, we're actually going to go to the overrides up in the inspector
to make sure that we save everything back to the prefab.
Now you can't see it because it's off the edge of my screen, but there's an apply all button at the
end which will write that back to the prefab and we can actually delete the shell.
Now let's have a look in Destroy Shell.
Let's open that up.
What it's simply doing is destroying your shell after 3 seconds.
So if your shell doesn't hit anything, this will get rid of it.
That's all it's doing.
Now, if we go back into our shell code, this is the one we're actually going to start working within.
So we've got our explosion coming through.
We have an on collision with an object called Tank, which we need to check if our tanks are actually
tagged with that.
And then we have a start and a light update where we're working mostly in this late update.
So before we start, as I said, we should actually put a tank as a tank.
So we're going to come in, select our green tank, which is this guy here.
Now, it's got to be the object that has the collider on it, all the objects that have the collider
on it.
Now, in this case, none of these objects have a collider on it, and therefore we need to add a collider.
So just add in here a simple box collider.
It doesn't really matter too much.
It doesn't have to be too accurate.
Then we'll just have a look at our tank and see if we can see where that went to.
We might just have to rejig it a bit.
Move it up.
And just make it a bit bigger in the Z direction and the X and the Y.
Okay.
And we also want one around our enemy tank, which is already tagged as Tank and has got a box collider
on it.
So that's good.
So this green tank has to be tagged with the tank tag like that.
Now, if you don't have the tank tag, feel free to make it and then attach it to the tank and the enemy,
the top parent of both of those objects.
Now, I'm just remembering before we actually stripped the shell, prefab from everything, before we
started playing with it.
What I'd like you to do is actually select it and make sure that you add a rigid body because it's the
rigid body that's going to register, whether it's hit something and also have used gravity on it,
as you can see there.
Now you can change that with it just sitting in the assets.
You don't have to make a copy in the hierarchy.
Okay.
So with that done, we can actually press play.
And if we hit the B key for the red tank, you'll see that it's now producing shells and they're exploding.
Now, why are they exploding?
Because they're actually hitting that tank.
If you hit the space bar, this one will be shooting cells.
What you don't want your shells to be doing is being hit by the object that they're coming from.
Okay.
So what we can do with that instead is actually just modify.
Let's stop playing their turret position.
So on the red tank, if we just come in closer here and hit the W key, we can actually move that out
to about there.
And then we'll also go to our green tank, which has our tank turret that we added previously.
Now it's not based on the rotation of that tank, if you remember, but we could just move it up like
this and then hit the key and rotate it around its x axis.
You'll need to check that that is not inside of this or make this a little bit smaller.
That's the collider on your tank.
So I'm going to make the collider just a little bit smaller in the Z direction like that.
And then we'll go over to the enemy tank.
Let's just double check that.
Let's change its box collider in the Z.
Okay.
So now if we press play, we can check those bullets out, make sure that they don't, and now they're
not hitting the tank.
They're also not going anywhere because we haven't put any code in there.
So let's go back into our Shell script and add that.
So what I want you to do for a challenge in here is to project that shell forward along its Z axis.
We've already done that before and you've already got the code to do it, make sure that it's going
at one meter per second, or that you can set the speed of it and put that inside of this late update.
So pause the video now give it a go.
And when we come back, I'll go through it with you.
Okay.
Welcome back.
What are we going to do is, of course, first of all, define our speed.
So float speed equals one.
So that's if you want one meter per second and that's what you would have.
Let's let's make it three.
And we could also make it public so we can change it in the inspector like that.
And then inside of our light update, you're going to have this dot transform, dot translate zero in
the extraction, zero in the Y direction and speed multiplied by time, dot delta time in the Z direction
so that it'll move forward in the Z.
Okay, so we'll save this.
We'll go back into unity and we'll have a look at the result of that.
So we'll press play.
Let me just zoom out a little bit in this same view so we can see if they go further afield and then
we'll use the B key and have a look at where they go.
Now, notice that they come out and there's a little bit of a curve in the trajectory.
That's because the gravity is on that rigid body.
So the rigid body is moving with the Unity's physics system to add gravity, but it's moving forward
with our transform translate.
So if we go into our shell just for now, there's the shell we want to turn off the gravity.
So down here in news, gravity, turn that off there, because we're actually programming our own acceleration
movement, not using the physics system in Unity's, although you could do that.
But that's not the point.
Okay, so we've got that.
Now, what we're going to do back in our shell script is calculate the speed based on acceleration.
So we're going to now add in some variables to do with our physics of acceleration.
We want to mass, we'll set it to equal ten, then we want a float of force, set that to 1000, then
we need a float for acceleration and we've already got our speed value up there.
Okay.
So let's come down and use that in our late update.
So first we calculate acceleration.
Can you remember what the formula for acceleration is?
Well, it's acceleration equals force divided by mass.
This is the F equals M a formula that we looked at in the previous lecture from acceleration.
We can work out the speed.
So the speed is determined by the acceleration.
How much acceleration over time.
So multiplied by a time dot delta time.
So now our speed is not put in as a set value.
It's going to be based completely on acceleration.
And if you think of the way a car works, it is based on the acceleration.
Without any acceleration, there is no speed.
So save this.
Now we'll go back into unity and we will press play and have a look at the effect when we press the
B key of the Shell's programming case.
So if I hit the beak, it's going to go very long way because we are putting a fair bit of force on
there.
Notice how much force is coming out of that bullet and it just speeds up because we're constantly adding
that force over and over and over and over again to the bullet.
We're not actually just adding it once.
So it's like that bullet is getting pushed through the air, which unless it has a rocket ship behind
it, blasting out some kind of propulsion, it's not in real life going to go very well.
It would have a little boost and then want to just fall to the ground.
Okay.
So let's come back in the next lecture and I look at those particular mechanics.