Welcome back to part three of this set of tutorials on acceleration.
We're going to start with a little bit of a challenge now.
I said to you in the last video that we are constantly adding acceleration to the speed and therefore
the bullet actually speeds up with a bullet, though it usually starts with a particular acceleration
when it's fired from the gun and then it doesn't have any more propulsion added.
So how would you modify this code?
So it only gets a push at the start of its life and then continues on with that particular speed.
Pause the video now and try that out.
And when you get back, I will show you how to do it.
Welcome back.
How did you go with that challenge?
Well, basically, we have a start method here.
And this Dartmouth is going to run when the bullet is instantiated because the shell script is on the
bullet.
That means we can take this calculation out of here and put it up into here to actually work out what
it's going to be.
Now, the acceleration in this case won't be at time.
Delta time.
You want the acceleration at the beginning and not even at the beginning, because at the beginning
the speed is zero.
So when this starts, we're going to basically have it accelerated after one second.
Now, you might have also realized that previously we had multiplied time delta time by speed twice
with down in here.
But now that we've got rid of that, that's no longer an issue.
If you wanted to leave this code down in here, then you wouldn't need the two lots of time, delta
time that we had before.
Basically now with acceleration set to multiply by one second brings us to the question of what are
the units of force and mass?
Well, in unity it uses metric units.
If you're interested in all these different unit types, you can have a look at this link that's now
on the screen, which will take you to all the standard units that are used in force calculations.
Force is basically Newtons.
So how many Newtons are behind the force, which is the mass and the acceleration units put together?
So you've basically got kilos and multiplied by the acceleration, which is in seconds squared actually.
So up here with our mass and our force that we're initially putting in a mass of ten kilos for a bullet,
that is a cannonball.
Well, maybe not that big, but that's a big bullet.
So let's reduce these down to proper units now.
Now, how much force do you want to put behind it?
If you think about simulating a real bullet, even a kilo is a lot, isn't it?
But a real bull.
You can't see it now.
We can simulate it so we can't see it.
But then there's really no point.
What I'm going to do instead is bring this force down to one.
So basically when we push the bullet out, the speed will end up being one divided by one, which will
give us a speed of one meter per second.
However, don't forget we've got speed set up the top here at three.
I'm going to get rid of it as being a public because we're not setting it in the inspector anyway.
And let's put this down to zero initially because that's what the starting speed will be.
Okay?
So with all of those in force, let's save it and we'll go into unity when we run it.
We'll now have these bullets flying off at one meter per second.
Okay, so let's hit the B key.
And now we can actually see these bullets flying off, living for 3 seconds, which essentially, if
you think about it, they have kind of lived for three meters across there.
So they'll go one meter every second because we're quite zoomed out on the sizes of these things and
those bullets are huge.
So now what are we going to do if you want to speed this up?
Well, of course, you could add a little bit more force to it.
So if we come back into our script, we can come up and make this a force of two, which should give
you twice as much sort of oomph when you start off.
So save that.
Let's come back, press play and have a look at the effect.
We need it to be going decently fast so that we can actually see it slow down.
Okay, well, that's not too bad.
You could make it a bit faster if you wanted.
The next part of simulating this bullet is to actually slow it down so it's going to get slowed down
when it's in the atmosphere due to drag or air friction.
So we need to add in a value for this.
We'll put it up here underneath our other force.
So put float and drag and set that to be, let's say, one in this case.
Now, you might also have notice.
I've increased my mass of my bullet.
That was so I could slow down my simulation so that you can see it, because with my screen recording
software, it keeps changing speeds on me.
And it was really, really slow before.
But since then I actually restarted everything and now it's much faster.
So if you want to see what you're doing, you have to sort of manipulate these values.
But we just want to see the actual effect of drag.
So if we come down into our late update, we're going to update speed in here.
So speed and this is multiplied by equals and it's going to be one minus the time dot delta time.
Multiplied by drag.
And of course, then you can also increase that drag amount.
So with that in there, let's save it and have a look at how that slows down the bullet.
So press play.
And let's shoot a bullet.
Now you can see that it comes out and it slows down.
So that's some pretty thick air that it's trying to travel through.
The next thing we need to add is some gravity because the bullet isn't going to sort of get to that
point and stop.
It needs to sort of, as it's coming out, create an arc because it's gravity on earth that creates
that arc for us.
So back into our shell, we're going to come up to the top and add in a force for gravity.
So we'll put float.
Gravity and gravity is -9.8 meters per second squared on Earth.
So we'll actually use that value in here.
Make sure you put an F now we'll need a float for gravity acceleration, which is basically a deceleration,
which we'll calculate.
And we also need another speed.
So at the top we will put float and this will be our y speed for moving in the Y direction and make
that zero f.
Okay.
So now we need to calculate the actual deceleration or the gravity acceleration.
So in here after this, I can put in G Excel equals gravity, which is f force divided by mass again.
And then in our late update we can add our gravity factor to our speed.
Why speed plus equals g excel multiplied by time delta time.
And then that value can go in here as our y speed.
All right, so we've got that in place.
Let's now save it and try it out.
So come back into unity.
Now, if you want the bullets to be destroyed when they hit the ground, you could tag the ground which
is there.
It's got a collider on it.
So we could actually tag it as tank or you could tag it as ground and put that in your code as one of
the conditions for doing that.
It doesn't matter which way you do it, but for us right now, it doesn't really matter because we're
not going to continue using this project like that.
Okay.
So I'm going to lift up my turret.
Now I'm running and I'm going to hit the B key and you'll see those bullets are now coming out and nicely
going down with gravity.
And if I change the angle now, they're coming out.
And actually because of the gravity hitting that box collider around the tank at that particular angle
that I had it as, okay, great.
So at this point you could give them a little bit more force depending on how they're flying for you.
So starting force, if I come up to the top, I might just increase this by two or you could increase
the drag if you want to slow it down or decrease the drag if you want to speed it up.
There's all these sorts of forces involved.
And let's play again with those values just to see what it looks like on my tank.
So I've got it to go a little bit further.
At this point, you probably want to turn the bullet so it faces in the direction it's going.
But believe me, you don't want to do it with the code that we've got because of the way that we're
actually doing it, because we're pushing the bullet along.
It's basically its up axis and its axis here.
If we try and turn the bullet, then it will affect this translate line here.
And because the bullet can be looking in any direction, we get all confused and a lot of complicated
maths to do with local positions and world positions and things like that.
So we're not actually going to do it in here.
Instead, what we're going to do is come back in the next lecture and look at some other ways to do
trajectories, because this was just to make you aware of all the mathematic elements that are involved.