Welcome back to the second part of calculating trajectories.
Let's go back into our fire shell script, where also, while we're in here at the top of this file,
going to add in a float for the speed of our bullet.
And we'll set that to 15 initially because we're going to be doing the speed with the physics system
in unity rather than calculating it like we did before this.
We have a different value up here to start with.
Okay, time to calculate the angle that we need to put our turret at.
So we're going to create a new method and we'll create a float and we're going to create a float with
the question mark on it like that for return, which will allow us to return a null.
Remember our formula that we're about to do?
Well, it can return angles, a high angle, a low angle or find absolutely no angle whatsoever, which
means it's not possible to get an angle that will hit the target.
Now we're going to call this calculate angle and we'll pass through a bool for low to say whether we
want to get a low or a high angle back from our calculation, because we could get to values as we'll
see in a moment, just like our quadratics that we were looking at previously, where you can have two
solutions.
We've got the same kind of thing going on here.
In fact, this stuff is all coming from a quadratic anyway.
And if you have a look at this solution that we're currently working with, it looks very much like
the make up of the root of a quadratic equation.
Therefore we've got that plus minus in there.
It's going to give us two possible values, one value or no values.
Okay, so let's go vector three, target d, i r.
So we need the direction to our target, which is our enemy.
So enemy dot transform dot position.
Minus this dot transform dot position.
Float y equals target d r dot y.
And then we'll grab that and set it to zero, because we're only interested in the direction of the
target on the x z value because it's on a flat surface.
And as we did with our look rotation, we didn't actually lift the tank up off the ground.
Okay, so target dear dot y equals zero.
If so, let's just zero that out.
Okay.
So float x equals target di r dot magnitude.
And these are all variables going into our equation.
Float gravity.
Can you remember what gravity is?
From our calculations we did before 9.8 meters per second squared and I'm actually going to have it
in positive this time to save us, negating it later on which it is.
So float s squared equals the speed of the bullet multiplied by the speed of the bullet.
So speed squared.
And then we can calculate the equation that is under the square root in there.
So we want to float and then we want under the square root equals.
And this is your challenge.
Can you put together this formula?
So we only want what's sitting under that square root in that equation to go under.
This equals here, pause the video now give it a go.
And when we come back, I'll go through it with you.
Okay to begin, let's go s squared multiplied by S squared minus gravity multiplied by in brackets gravity
multiplied by x multiplied by x plus two multiplied by y multiplied by s squared.
Okay.
How did you go with that?
Fair bit in that equation.
That's all that's sitting underneath the square root.
So now that we've got that in a variable, we can do the other calculations.
So we want in here.
If under the square root is greater than or equal to zero F, then float root equals math f dot square
root of under the square root because you can't get a square root of a negative number.
Well, you can, but that's going into imaginary numbers, which is not where we're going right now.
So float high angle equals square root s squared plus the root and then float low angle equals s square
minus the root.
Then if lo.
So if we want the low angle, then we'll return math f dot a10 squared.
So we actually do the inverse tan of the result that we got, which is our low angle and gravity multiplied
by X, which will be in radians.
So we'll multiply that by math f radians two degrees else we will return math f 18 squared, high angle
gravity multiplied by x and again multiply by math f rad two deg.
Otherwise, if we've got a negative under that square root sine, we're going to return.
No.
Okay.
Let me just fix up that spelling mistake and we're done.
So there is our calculate angle formula.
Now that we've got our angle, we need to set our turret to that.
Now what you'll need to do is bring the turret base in.
Remember, the turret on the green tank is actually where the bullets are coming from.
You'll need to add this, which I've just done right now.
So add in a public transform for the turret base and we want to save it, flip back into unity and select
the green tank and then get the parent object that's holding on to your little turret marker that you
created and drag and drop that into the turret base.
So it's basically the same bit that we programmed in the red tank that could move up and down with the
T and G keys.
Okay.
So now we can go in and actually program that to move.
So we're going to create a new method under here and to create bullet, we'll call it void, rotate,
turret.
And inside of here we'll call that calculate angle.
So we want a float question mark angle equals calculate angle and we'll say yes, we want the low one.
And then if angle does not equal null, then inside of here we'll go turret base dot local euler angles
equals new vector three 360 f minus the 360 F minus float the angle 0f0f.
So we're basically rotating around our x axis, only taking into consideration the rotation that is
already on that.
Okay.
So that's pretty much all we need there.
We just need to call rotate turret.
So let's come down into our update after we're sort of looking and put rotate turret in there so we
can save this now and actually try it out.
So come back into unity and we will press play.
Move your red tank around and.
Watch the turret of the green tank change to actually face or adjust itself for shooting at the red
tank.
When we come back in the next video, we'll put the final touches on this, which is to get the bullets
travelling far enough that the whole system kind of works together so that the bullets will hit the
tank based on the angle that they come out of the turret.