Welcome back to this section.
Looking at physics of objects, currently we're dealing with velocity that we've put on this bullet.
Now what I want you to do with it, or the shell, as we've called it here, is drag and drop a copy
of it into your asset and create an original prefab.
We can then go back and open up our tanks code.
We don't need to save this scene unless you want to write.
So let's start by writing some script that we're going to call Fire Shell.
So Go Create and we want a C sharp called Fire Shell.
And in here we basically need to instantiate a copy of the shell and also position it where we want
it to come out of.
Now, if you double click on your tank to bring it in closer, you want it to come from this position
here, not the position of the tank, because the global position of the tank or its origin for the
model is actually sitting down here.
So you don't want the bullets to come out of there, you want them up here.
So what we can do in this case is we just add in a little marker.
So we're going to go to our tank, we're going to right click it and create an empty.
And then with that empty we will call it something like turret.
And then you need to actually move it to where you want it.
So select it and then just move it like this.
Now if it is in an odd spot, then make sure you first of all, you 000 in here so it'll be sitting
at the same position of the tanks, 000 then you can actually select it and just move it up and that
will allow you to sort of put it just there.
Now, we don't want it tipped at the moment.
We want to shoot bullet in a straight direction.
So we'll just keep that going horizontally with that Z axis.
Okay.
Now before we get on to the code for firing a shell, if you remember in Move Shell that we actually
are going up the screen a little bit as well as across.
We just want to change that back to going across the screen like that and then we can open up our fire
shell code.
So let's grab that.
So in here we want to add in the bullet.
So we want a public game object bullet and public game object turret.
Okay.
Then in the start, for now, what we'll do is we'll go.
Instantiate a bullet at turret dot transform.
Thought position and turret dot transform dot rotation to ensure that it's going to be facing in the
right direction.
And let's spell that correctly then.
So just save that.
Now we're going to go back into unity.
We're going to drag and drop fire shell onto the tank in the inspector.
And then you've got this bullet.
You want to grab your shell from the assets folder, put that there, and then you want your turret
drag the turret that's on the tank across to there.
Okay.
Now when we play it, the tank will actually shoot a bullet.
So if we go play like that, you can see that it's shooting a bullet very slowly, one meter per second
across the screen, and we'll speed that up a little bit later.
Okay.
So let's just stop playing to begin with.
We're going to turn the red tank.
So just select the enemy tank and then just hit on the key and just rotate it.
So it's going to sort of move off into the distance like that.
We then want to actually edit the script that's on that tank.
So let's find the drive script, open that up.
Now, this is the code that allows you to drive it, but we actually want to sort of automate it to
begin with.
So basically the transformed translate that's down here, just make a copy of that and we will put it
down in there.
We will comment out the other one and in this translation for the Zed, we'll just do a speed multiplied
by time dot delta time and that's going to then just project that tank forward.
You might also want to comment out the rotate just so that you don't accidentally rotate it.
Okay, now if we save that and we just hit play to test it out, our red tank will go off in that direction
into the distance whilst we're shooting across here.
Okay.
A little bit fast I think for my liking.
So I'll just change this, speed down to one initially.
Okay.
So now we can go back into our fire shell and add some code in there to start shooting the bullet.
And we'll do that when we hit the spacebar.
So let's go.
If input get key down is key code dot space, then we will instantiate the bullet.
Okay, so we'll change this name of start up here to create bullet and down here we will run create.
What we want to do now is to determine the angle that the tank should be facing before it actually creates
the bullet.
So in here we'll put calculate trajectory and create up here.
Void, calculate trajectory.
To solve this problem, we must first determine a time to impact based on when the bullet and the target
will be in the same place.
The bullets path is defined by its speed.
The target's path is defined by its position plus its velocity.
When they will be in the same place is dependent on the time.
Therefore, we multiply each of these by time.
Now, after that time, the target and the bullet are in the same place.
That means they will be the same distance from the shooter.
We express this as s multiplied by T equals p plus v multiplied by t.
So what do we do with this equation?
Well, we have to solve for T and this means getting some quadratics involved and solving for a quadratic
equation.
Now, if you're not familiar with quadratics, we don't really have enough time in this video to go
through that.
And I would suggest you go and read this Wikipedia link here and maybe also brush up on that mathematics
if you have a look on YouTube at some teachings on that.
Anyway, very simply and straightforward, what we're going to do here is to solve this.
First of all, we're going to square both sides of it.
Okay.
And then that gives us the following line, which is the product of P with itself plus two P product
v, t plus v dot product with itself and t squared equals s squared multiplied by t squared.
Now quite quickly, if you do a square of a vector, it's actually the same as a dot product.
So that's where those DOT products have come from.
Now on the next line, we're basically getting the S squared T squared factor over on the other side
of the equation.
So the whole thing can equal zero and mixing that in with the values that we've already got.
Okay, so this is our quadratic equation and we now have to solve it, will do it in code.
But before we do, I just want to point out what all the variable names in the code are going to equate
to that come out of this equation.
So the general form of a quadratic equation is A X squared plus B, x plus C equals zero.
We've got our equation in that format.
So if you have a look, you can see where a x squared is.
So that's the last term there.
In this case, T is our x value because it's the value that we're trying to find.
B x is the second term in the equation.
Then C is the one at the beginning, the product of P and P.
Now to solve this and find out what T is with a quadratic equation, there's two routes to it, and
in this case we can use the formula shown here where X equals minus B plus minus square root of B squared
minus four ac over two A.
So basically we're going to take each of those different elements of that formula that equate to A,
B and C and plug them into the root there in order to solve for X or in our case, T.
And that will give us how long or the time difference until the impact of the bullet and the tank.
Okay.
So I think that's enough food for thought for this video.
What we'll do is we'll come back in the next video and actually put this into code so you can see it
working with the tank and the bullet.