Just as velocity was the rate of change in position over time.
Acceleration is the rate of change of velocity over time, and it is represented like this.
Acceleration is used quite a lot in physics systems as objects in real life don't move with constant
velocity, at least not if you have to slow them down or speed them up.
Any collisions that occur will involve changes in acceleration.
Anything being simulated in the real world will also constantly be under the effect of the most famous
of the known accelerators called gravity.
So acceleration is quite difficult to escape.
In 1687, Sir Isaac Newton published his famous Three Laws of Motion.
The second and more prolifically remembered is F equals MA or force equals mass times acceleration.
And it's this equation we are going to put into practice with our tanks and shells to get a real feeling
for how they work.
So to begin with, we're going to go back into our drive script and modify it back so you can drive
the tank around, but also so that you can actually lift up the turret with some keys to rotate it like
that.
Okay, so let's do that.
Open up your drive script.
Now, this is how it was last time we used it where it was self driving.
We're going to get rid of the self driving code.
I'm just going to comment that out and I'll put these two translates and rotates back in.
We also need a link to the turret of the tank so that we can move it around.
So public game object and we'll just bring in the transform for that.
So that'll be the trans gun like that.
Then down inside of our update will use the T and G keys because they won't interfere with the get access
code up here.
So if input dot get key key code dot t, then what we're going to do is go trans gun dot rotate around.
Oh, and I meant to actually make this a transform up here, not a game object that allows to get straight
into the rotate around.
So rotate it around its position.
So trans gun dot position will rotate around the axis of trans gun dot right.
And we're going to rotate it by minus two degrees.
Okay.
Now we'll come down and we'll put the same in for else if the G key.
So just grab a copy of that and put it in here.
Make this into the G key and then make it rotate in the other direction.
Okay, so save that code, switch back into unity.
What we're going to do now is come to our enemy game object with the drive script.
Now you've got trans guns sitting in here.
Drop down the tank renderers and find the turret and actually put that down into that position.
Okay.
So if we press play at this point, we'll be able to move that turret up and down with the T and the
GCS like that.
Now attached to the turret.
We want to add in the point where our bullets are going to spawn from.
So we'll right click on it and create an empty.
Now that's going to let's be called turret.
You can call it gun or something.
It doesn't matter.
It's just a marker.
Hit the key so you can see where it is over in the same view.
I want to move around a little bit, so we're going to lift it up and we're going to bring it to the
end and maybe just out a little bit, because that's where we want our bullets to spawn.
And then you want to actually rotate it.
So we're going to hit the E key and then rotate it up a little bit.
And if you're wondering how to align it, probably get this ring that's going around the middle here
for Y rotation to kind of align with the actual tank, which will then help make sure you only rotate
it on the red axis because you want to rotate it around the x.
So this is going to be the spawn point for your bullets and you can adjust that later if you want.
We'll go back into the drive code and actually bring that through.
So up here we'll put in public transform and let's call it the gun.
Okay.
Then we can actually spawn a bullet from the gun position.
Okay.
So we're going to do this in the same way as our other tank does it.
This is a starting challenge for you.
What I'd like you to do is program in the B key so that the red tank fires a bullet.
So pause the video.
Now go into that.
And when we come back, I'll step through it with you.
Welcome back.
How did you go putting that together?
Well, we need to do this in the drive script, so we'll open that up.
Now at the top, we have a transform for the gun, the transform for actually moving the turret bit,
which was up here.
And what we're going to add underneath that is to bring in the bullet.
So we need a public game object bullet and we'll call that the bullet.
OBJ Now down in our update, we've got other key codes coming in.
We're going to put an else in here.
So else if input dot get key down and we're using key down so that we only get like one bullet.
Where is key get key actually will hold it down and you can keep rotating the turret.
But in this case, if you do that, you're just going to get a whole stream of bullets which we don't
want.
So key code B, so if that, then we're going to instantiate a bullet object at the position of our
gun.
So gun position and gun rotation.
So gun rotation.
Now the bullet still has its own code sitting on it to project it forward.
So just save this and we'll go back into unity from here.
We're going to find our shell prefab, which is up here in my top assets folder.
Select the enemy.
We want the shell to be the bullet object there.
And then you want the gun, which is our little turret we put on the end.
This one here to go into the gun like that.
Okay, so we can test this out now.
So let's press play now.
Don't forget, you can use your T and g keys to use.
Move your turret and then we hit the B key and we'll get a bullet.
Now notice how it fires wherever we're actually facing because we're using the transform that we've
got sitting on the end.
Now, these bullets are going to go forever and ever and ever off into space, so we won't keep firing.
Otherwise we'll overload the system.
But instead what we'll do when we come back in the next video is deal with destroying them, as well
as look at putting some gravity and acceleration onto them.