Welcome back to this two parter on calculating a trajectory.
Now we've got our calculate trajectory method in the fire shell script.
Now we just have to translate the formulas that we looked at in the previous video into here to calculate
where that tank is going to be down the track so that the bullet can be calculated with its speed to
be in the same spot at the same time.
Okay.
So let's get started on this code.
First of all, I want to change this to return a vector three and then that vector three will actually
set in the update.
So this dot transform dot forward equals the vector that we return.
Okay.
So let's start by defining all of the inputs for these formulas.
So we need a vector three P, which is the position of the enemy.
So that's going to equal enemy dot transform position.
Now it has to be relative to the shooter.
So we'll take away the shooter's position because all of this stuff is being calculated relative to
the shooter, which means you kind of get to get all the coordinates of the enemy into your shooters
local space.
Then we need our vector for v.
So three v which is our forward vector and speed of our enemy.
So enemy dot transform dot forward multiplied by the enemy's speed.
So enemy dot get component and we're going to get the drive script and then grab the speed out of there
and then we are going to get the speed of the bullet.
So float s equals bullet dot get component move shell and dot speed there.
All right, now I'm having a little bit of a problem remembering whether I got you to put enemy up at
the top peers.
We have to pass through the enemy so we can use the value.
So just check if you've got it.
If you haven't, just put it in there.
Okay.
So now moving on, let's go and create A, B, C and D from our quadratic formula.
So float a will equal vector 3.. of V and V minus s.
Multiplied by.
Yes.
And you can see where that is in the formula.
Then we need float for B and we need a float for C.
Okay.
So this is a challenge for you is to come up with the formulas for B and C.
Pause the video now give it a go and we'll come back.
We'll keep typing through this.
Okay.
How did you go with those?
Well, they're actually both dot products, so it'll be a vector 3.. and B is P and V and then C is
going to be a vector 3.. product.
For P and p.
Okay.
Then we can add in D, so I float D and that's going to equal B times B or B squared minus C times A.
And in the formula it's actually got C, A, times C, this is inside of your square root like this.
So we can actually take advantage of there being fours and twos in those and cancel those values out,
which is what you can do if you're deriving these values from quadratic formulas.
So we really need, first of all, to figure out what the value of D is.
If D, which is what's under the square root is going to be a small number.
Okay, then the bullet isn't actually going to be fast enough to catch up, in which case we can return
like zero or something.
So we'll go.
If D is less than 0.1 F, then we can return vector 3.0.
Okay.
Now if we're continuing on with this, then we need to calculate what the actual square root element
is.
So float square root equals massive dot square root of DX.
Okay.
Now we found out previously from our root equation for our quadratics that there are two possible values
because there's a plus value in a minus value, so they can be two.
So we're actually interested in what those two values are, and then we want to pick the best one.
So we want float time one is going to equal and the first one will be minus B, minus the square root
divided by C and float t two is going to equal what time for another challenge?
What will this second root from the quadratic become force video.
Now give it a go.
And when we come back, I'll keep typing through it.
Right.
How did you go?
Well, the formula is negative B plus or minus the square root, blah, blah, blah.
So this one will be negative B plus the square root divided by C.
Okay.
So now we have these two values.
We can figure out which one we actually want to use.
So let's create a float for the actual time that we'll use and we'll set that initially to zero.
Then we're going to say if t one is less than zero and T two is less than zero, then there's no acceptable
solution.
So we'll just set t equals zero in this case.
Okay?
Else if t one is less than zero, then t will equal t two.
Else.
If PT two is less than zero, then t will equal t one else.
We will use the maximum value that we're getting out of there.
So we'll put t equals math, f max, new float and then t1t2.
So the math f max takes a array of floats and gives you back the maximum value which you can then use
a bit quicker and easier than coming up with an if then else statement that does that.
But of course you can do that too.
Now, after we get through this little bit of logic, we're going to go return T multiplied by P plus
V.
So basically we're going to use our time to multiply out our values in order to calculate where the
bullet and the enemy are going to cross paths.
And when they do, we'll actually use that as the vector that we're sending back to the tanks forward
direction so that it can aim in that way.
Okay.
So I think we're ready to save this and try it out.
So let's save it.
Let's go back into unity now.
I've changed my grand color to black so that you can see what's going on.
If we go over to the tank, let's remind ourselves of our fire shell script and what needs to go in
the inspector.
You'll have a shell, the turret, and then you want your enemy.
So drag and drop your enemy from the hierarchy into there.
The enemy itself.
We haven't changed anything on that.
I have increased the speed to three just to make it go a bit faster when we're testing.
Also, I've gone down into the shell prefab and changed its move shell script to three as well down
there.
Okay, so let's try this out press play and as soon as it gets going, I'm just going to hit the spacebar
and we'll see whether we intercept the tank.
Yep.
Now, at some point, you can see here that things have kind of fallen over because we can't calculate
an actual trajectory.
If I press play again and actually shoot lots of bullets, you'll find that they will arc across the
screen and as the tank moves, it will move through all of them until a point where our math types of
breaks down.
Well, it doesn't break down.
This is when we're going to get very, very small and inaccurate values.
So that one there is where it first broke down.
If we come out of here, these bullets are going to be the last lot that actually intersect with this
tank.
The further and further it goes out there.
Now, that can be a bit of a problem.
So we probably don't want that happening.
We've also at this point, because we're returning zero when we get values we're not interested in and
we're trying to set the forward vector of the tank to that.
We're getting a look rotation viewing vector is zero error.
So let's just go back into our code and fix that up.
Okay.
So we're returning zero up here, which is where our issue is.
Let's come down into the update and we'll actually store the returned value first.
So we'll go vector three, aim at equals, calculate trajectory and then we can test it if aim at equals
vector 3.0.
Or I should say if it doesn't equal vector three zero, then we will actually use it to set our transformed
forward.
So aim at down there.
Now in addition where we're setting T to zero, we're not really interested in that either.
So instead of doing that, we'll just go up here and we'll return zero vector in there.
Okay, let's save this and have one more shot at it.
Okay?
So start hitting the spacebar and then when I get around as far as I can go, it will actually stop
sending out bullets.
But you can see as the tank arcs around, it actually will go through and hit all those bullets.
Okay.
So that's a simple.
Yes, simple.
That's probably one of the simplest ways of calculating projectile hitting that doesn't actually involve
acceleration and gravity, which becomes a far more complex.
So that is the start of it.
And you can just imagine that the math from here for more complex things gets even more complicated.
And when we come back in the next, we'll start looking at acceleration before we look at more trajectories.