Particles that are emitted at a certain velocity and angle that have no further force applied to them
other than gravity form the curve of a parabola.
We call this type of motion projectile motion, and it's the exact thing our tank shells are simulating.
There's a lot of mathematics and trigonometry involved in working with parabolas, and an excellent
breakdown of it is all on Wikipedia, where you can find more information if you want to delve in deeper.
But let's just go over a few of the basics.
All motion of the projectile we consider in two directions horizontally, which is its x coordinates
and vertically which are its y coordinates.
After the projectile is fired, it experiences no further acceleration in the x direction.
Acceleration remains constant though.
Wait, I hear you say or I hope you said.
That would mean that an object would never stop moving.
And indeed, in a vacuum that is the case.
Of course, in the real world there's drag and wind resistance and all other matter of things that you
could consider.
But for simplicity, we aren't going there now.
The Y direction of acceleration is always going to be our good old friend gravity.
You can see in the animation how gravity remains constant vertically, no matter what the angle of projection
is.
So acceleration matters in projectile motion are fairly straightforward and so too are the ones to do
with velocity.
If you can remember your high school trigonometry, there's a great Wikipedia page where you can brush
up on those skills too.
So the velocity or vector that the projection begins at is donated v zero or the starting velocity.
And the angle this vector makes with the horizontal is alpha.
Using this knowledge, we can construct a right angle triangle to break velocity into its y or vertical
direction and x its horizontal direction.
Using trigonometric ratios, we can further work out that v x equals v zero multiplied by cosine of
alpha and v y equals v zero multiplied by the sign of alpha minus g t where g t is the gravitational
effect at any point in time.
Remember that the acceleration due to gravity has a compounding effect.
So each game loop or time interval that we are adding another lot of gravity to is going to multiply
it.
So if we do this t number of loops, then we have to multiply gravity by T to get the correct amount.
Now from this diagram, if we know v, x and v, y, we can calculate the initial speed of the projectile,
which is the magnitude or length of our v zero vector.
And to do this we use Pythagoras theorem thus for what we want to use.
The principles of projectile motion for is to work out the angle the tank needs to aim its turret at
in order to shoot a shell with a known speed and hit its target where the target's position is known.
So to calculate this angle, the formula that can be used is ten of the angle equals the speed squared,
plus or minus the square root of the speed to the power of four minus g multiplied by g x squared plus
two y multiplied by the speed squared all divided by g x.
If you haven't used an equation with the plus minus symbol in it before, what it means is that there
can be two possible solutions.
In this case, there could be two angles that could be used to hit the same final location.
But that's the nature of parametric equations.
And you can see this in the animation shown for all the final hit points of the projectile, there can
be two starting angles.
So enough with all this mathematics.
We have the equation that we need.
Let's get back into unity and implement it.
Okay, so we're now going to concentrate on working with the green tank and the shells that are coming
out of it because it's kind of our eye character as such.
So it's the one that has to automatically calculate the trajectory for our shells.
So we need to actually create a different type of shell than the one we've got.
So get the shell prefab that you're working with that's currently on both tanks and do a controlled
D of it.
So you get a second version.
Now you can rename this as a shell, so you know it belongs to the green tank and then just select it.
And over in the inspector, what we want to do is take off the shell script that's on there so we can
just remove that.
And what we do should get rid of this mov shell script as well.
So we will make a new script to move it forward.
You can leave destroy shell on there.
That's alright.
But something you should do now that I haven't done in this video is make sure that you check.
Use gravity on this prefab as well as it will be needed later on.
Okay.
So the next thing we need to do is to edit the code for that shell.
So I'm going to create a new script and we'll have this as the shell script.
So just open that up.
Now you want to get some stuff in here from the original Shell script that we're already using, and
mostly it's to do with the collision stuff.
So grab the on collision, enter.
Now we're also going to need to bring through the explosion.
So if we just grab this and copy it and come over to the shell and then just add that to the top and
get rid of all of these things that we programmed physics with.
And in here that looks good.
Nothing else to do in here for now.
So just save that.
Let's go back into unity and now we want to attach that shell script to it.
So just drag that over and we want to add in the explosion, which is in our prefabs folder, which
is just here.
So let's just go back to our shell.
Actually, let me just put a copy of that into the hierarchy which will make it easier, then go into
the prefabs and drag over that explosion, shell explosion just into there like that.
And after making those changes, make sure that you click on override and apply all for that prefab.
Okay, so now back into our scripts.
We want to get hold of the fire shell.
So Fire Shell is the script that is on the green tank.
Let's open that up after I delete this shell because we're finished playing ramp that prefab and we'll
get to work modifying this so it will automatically do trajectories.
Okay.
So to begin with, we're going to remove the calculate trajectory stuff from this script.
If you want to keep a copy of this, then make a copy back up of Fire Shell.
Before you delete all of these things, let's come down and get rid of our calculate trajectory and
we're also going to remove that from in here.
So we'll remove calculate trajectory and we will also get rid of this AIM code and just have the create
bullet like we did at the beginning when the spacebar is clicked on, which of course later on it can
do it automatically.
Okay, so let's just bring that back in now.
Instead, we're going to use Unity's physics system for all of these things.
But the first thing we need to do is actually turn the tank around so that it's facing the enemy.
So we'll create a vector three and we'll call that direction, and that's going to equal our enemy dot
transform dot position minus this dot transform dot position.
And you can normalize that if you like because we only need the direction vector so it can be a length
of one that's fine.
So normalized.
And then we'll do a Catalonian to turn this around so I can turn in.
Look, rotation equals return in dot look rotation.
Now we could be using a look at for the transform on the green tank, but then it just snaps around
to the exact position.
This will actually sort of turn it over time, so it'll sort of look like it's moving, which is basically
using the slip that we're going to do.
So we're going to look rotation at new vector three direction dot X zero in the Y and direction dot
Z.
Now this is just in case the tanks aren't on the same level.
You don't want your tank to actually lift up off the ground as though it's sort of looking up in the
air because a tank won't actually do that.
Okay, so that is our look rotation.
Then we will go this dot transform dot rotation equals turn in dot slurp, which is a spherical interpolation
between one angle of rotation and another.
So this will be this dot transform dot rotation.
So it's current rotation and the look rotation.
And we're going to update that.
Time Delta time multiplied by a rocket speed, which is our rotation speed and then end brackets.
Okay.
Now, we don't have this defined, so we need to come up to the top of our code and put that in.
So up here, we'll just create a float for speed and let's set that to two and you can come back and
change that if you'd like your green tank to turn quicker.
Okay, so we've got that.
Let's just save it and we'll go back in to Unity and try this out so you can actually drive the red
tank.
And the green tank should turn and look at it constantly and you can see it there.
Notice how it doesn't snap immediately around actually slurps, which is a more sort of natural movement.
In the next video, we'll come back and calculate the angle that we need to have our turret set out
to shoot our projectile so it hits the red tank according to the equation that we looked at earlier
in this video.