In the previous lecture, we added in the ability to drive the red tank around and the turret here would
actually move up and down based on where it calculates it would need to be in order to shoot the shell,
given the shells, velocity or speed.
But as you can see, our shells don't have a velocity or speed at the moment, so we need to put those
in to complete this exercise.
Let's go into fire shell.
The shells are being created up here in Create Bullet.
Now, before the shells had movement code on them in the shell script, we've replaced it with the shell
script, which has no movement on it whatsoever.
We want to rely on part of the physics system and unity to actually push our shells through the air
for us.
So I'm going to create a game object here called Shell that actually grabs hold of the shell being created.
Then once we've got it, we're actually going to set its velocity of its rigid body to the speed that
we're wanting to set it to.
So we're going to say in here, shell dot get component, rigid body dot velocity equals speed multiplied
by a direction to make it into a velocity.
And in this case, the direction will be the way that that turret base is rotated.
So turret base dot forward.
So that'll be the forward vector or the z vector of that turret base to set that initial speed.
Okay.
So let's try this out, save it and we'll go back into unity.
And once that's loaded, remember, we're still firing the green tank with the spacebar.
Now notice that it's not perfectly accurate.
The further I get away, the better the hit rate is.
Might happen to get even further out here, I guess.
Now, the reason that that is happening is because of the way that we're calculating the distance between
the tanks, because we're calculating the distance of the tanks sort of from here to here, rather from
the bullet to there.
So what she can do is put a little bit of a tweak factor in there if you can't get the bullet starting
from the exact position of the tank and if we come back into the code, we can come down into calculate
angle where x remember that is our distance between the two objects, which is the magnitude here.
You can actually make that a little bit smaller.
So put like a fudge factor in there of one and save that.
Let's come back into unity and press play and have a look at the effect of that now.
So we're actually hitting the tank.
And I must say only just it almost just goes over the top of the tank.
Now, remember, the first lot of bullets you saw just then didn't hit the tank because the tank was
actually still turning, okay?
Because it's got a sort of slow turn rate.
So it might start shooting in the wrong direction before it's actually lined up.
Now, what if we use the other angle?
Remember, got two angles, got a low angle, and we have a high angle and we've chosen in this case
for true let's put false in here, which will use the high angle that gets calculated.
So let's save that.
Go back into here and we'll have another go.
So let's press play now the turret, look at that angle on that.
That's a high angle and the bullets are going to go all the way up and all the way down and then hit
our tank.
Let me just move out a bit now.
I can also get out of the road, which is a good thing, I guess, if you're playing against an eye
character.
Okay, now the bullets currently do not go in the direction that they are travelling and they should.
So let's also fix that up.
Now that's going to be in the eye shell code.
The shell has that rigid body on it and it's the rigid body's velocity that we are setting back in fire
shell here so we can grab that velocity and make the bullets body or I guess modelled body turn in that
same direction that it's travelling.
So back in here we're going to add in a rigid body
bait and we're going to link that up to the actual rigid body on the bullet so we don't have to keep
going, get component, get component, get component every basic loop that we do because it's very
quite process a heavy to do that.
Okay so we'll go this dot get component rigid body like that.
And then in the update we will say that this dot transform dot forward equals r b.
Don't.
Velocity, so it'll point in that direction.
Okay, so save that and we'll switch back into unity and press play.
And now.
You can see that the bullets go up and they'll turn around and when they come down, they're actually
facing in the right direction.
All right.
To finish off this section on projectiles, we're going to have a little bit of fun with our tank.
So what I've got here, you can see a continually shooting tank.
Now, some of those bullets aren't particularly accurate because don't forget, as they're getting instantiated,
they can collide with each other, but most of them seem to hit the target.
What I'd like to do, though, is to program this so that if the tank gets out of range and there's
no angle that can be calculated, that the little green tank will go after the red tank and actually
move.
So even though it's not hitting its mark there, it knows it's close enough that if it turns around
correctly, it can.
But then if I move all the way over here and it gets out of range, as far as distance goes, it will
stop shooting and then it will come after us again.
I can't get far enough away on the screen that you won't be able to see the red tank anymore from here.
Oh, there we go.
Okay, so once I come back close, he'll start shooting again, right?
So let's go into our fire shell script and add the code in that will allow us to do this.
First of all, we're going to add in a movement speed for our tank.
So float move speed and we'll set that to say three meters per second.
Now we need to test whether we've got an angle to determine whether we can move or whether we can fire.
And that means that this rotate turret is going to have to return our angle here.
So let's put this as a returning float question mark and then under here we will return angle.
So this will come back into our update.
Down in the update, we're going to create a float.
Angle equals that.
And then instead of get key down, we're going to go.
If angle does not equal null, then we can create a bullet.
Else we can move the tank.
Now we can move the tank quite easily with a translate.
So this dot transform, dot translate.
Because remember, we're turning the tank to face the enemy anyway.
So all we got to do is just give it a little bit of a push forward.
So zero zero and in the Z direction, time dot delta time multiplied by our move speed that we've got
there.
Okay, great.
So in fact, before I actually show you this in action because you've just seen it in action, I just
want to change our speed up here.
Let's turn that down to like one.
So it's a bit slower to catch up, but five, so it's quicker to turn around.
Okay, so save that.
Let's go back into unity and try this out.
So we'll press play and then just start driving my tank around.
So now that I'm out of range, he'll take a long time to catch up to me.
But notice he's still turning towards the red tank.
Now, soon as I get back into range, even though he might not be rotated correctly, he's still trying
to fire and eventually he will rotate around to the point where he can fire at me.
Okay, great.
Right.
Well, that ends this section on projectiles, but there's so much you can do.
And I hope this is sort of inspired you to have a look into the mathematics of this and a whole variety
of other types of projectiles that you can produce.
And now you should know how to put together a really simple little A.I. character that can fire at its
enemy.