[SOUND] G'day and how you going?
That's how we say hello in Australia, speaking Australian.
So how are you doing?
I hope you are doing well and fired up for a good, quick lecture in this one.
This will be a very quick conversation where we're going to find a problem and
then come up with a solution and I'm going to give you a cool challenge.
So here's the problem, when we shoot our laser beam,
[SOUND] it goes flying off into the distance forever and ever, our
hierarchy fills up with a gazillion clones of the instantiated game object and our
world has these lasers going off into the distance for infinity distance, way off.
So this is not a good situation to be in.
We want to make sure that our laser beams don't stick around beyond their use.
We wanna destroy them once they get past the play area.
So that's the problem, here's the challenge I have for you,
is to create a laser shredder.
So something that we put up here, an object,
a collision that we put up here that can shred our lasers.
So, before I show you that, I'll say, if that makes sense, if you know what to do,
if you know how to go about doing this, then pause the video and
jump into the challenge now.
Now if you want some hints, here they are, here are the clues.
First of all, create an empty GameObject that's called Shredder,
that has a trigger box collider on it, it's important to make sure it's trigger.
And then to create shredder.cs and attach that to the Shredder GameObject.
And when something collides with Shredder, destroy that GameObject.
And so, here's the last hint, I kinda gave it to you a little bit early.
If you know what's going on from all that, then dive in and
take this challenge on now.
If you still want a little bit more information, then here it is.
The hint I've got for you is the use OnTriggerEnter2D(Collider2D collider) and
then this collider here, we can use that
to destroy the GameObject that has bumped into us.
And then if you want one more last little hint, this is a sneaky hint.
There's one more thing needed for our laser itself.
If you're like me, then you'll potentially forget that and look at your code and
look at everything for five minutes and
then go like uh-huh, I forgot to do a thing for my laser.
So, there's your challenge, jump in and take that on now.
I'll see you back here when you're done.
Right-e-o, let's go through the steps in our hierarchy.
Right click Create Empty.
I will call this Shredder.
And then on our Transform, you just reset.
It's good to have it to reset the transform,
right clicking on Transform there.
Now, we need to add a component, this will be a box collider 2D.
The box collide 2D, at the moment, you can see it's quite in the middle of our screen
over here, I would drag it up, so it's above our place space.
And then I'm going to click on Edit Collider,
drag the handles to the right and to the left, make it a little bit bigger.
The height of it doesn't matter so much, we just neet to make sure we catch any
of our lasers that are being shot up above the screen here in the y direction.
Okay, next thing add component, we'll call this shredder and
this will be a new script.
Click and create and add.
Now you probably have this already but I want to anchor this and
reinforce this just in case you haven't, is we are creating a script and
adding it straight away to this game object which is the shredder game object.
But that doesn't mean that this script lives on the game object and
that's the only place we can use it.
As you know, it's over here in our assets directory, this is the script right here,
which I'm gonna drag into my scripts folder.
And we know that on our shredder because we created it as a component over here,
it's already added it, so it's added this as a component to this game object.
But it is this script just here that we're going to open up and if we want,
we can put that script, we could attach that or
add it to any other game objects that we wanted in the game.
Okay, visual studio opened up my other monitor for some reason.
Here we go, this is our Shredder.cs, I'm gonna highlight our Start method and
Update method.
Delete those, we don't need them.
We will start off by saying void OnTrigger, OnTrigger,
this is the important thing to not make the mistake OnTriggerEnter2D and
that autofills for me Collider2D and collision.
This Collision will give us some information about whatever has
collided with Shredder.
So we can say he destroy, what will we destroy?
Well we'll destroy collision to get the information about the thing
that bumped into us, .gameObject, so destroy the whole game object.
So we don't wanna destroy just the collider,
we wanna destroy the whole instance of the game object.
Click save back over into unity.
Now if we try to do this, it won't work because there is one more step,
that's the sneaky hint that I gave you.
We have a look at our prefabs, find out play a lazer.
What are we missing on our play a laser, we are missing collision,
so we need to add a box collider.
I'm gonna make a capsule collider 2D because it's kinda capsuley shape.
So capsule collider 2D,
drag one of these into the scene to make sure it worked okay, play a laser.
There it is, double-click on that to zoom in.
Okay, it looks like a nice, neat, tidy collider around.
No need to change that, so I'll delete the player laser cuz we don't want it sitting
in the middle of our scene.
And where are we?
Back onto our player.
Okay, so we have our player, we have our laser, we have our shredder,
let's see if it works, click on Play.
Shoot a few times and then yes, there they are.
Okay, well this is really cool.
Boing, they kinda hit and then go flying.
That could be a cool effect but not what we're going for.
What have we missed?
One last step that we've missed in here on our shredder, our box collider 2D.
We haven't made it a trigger.
And this is one of those things, if you know things stop or bounce,
then it's because it's a collider, not a trigger, so click on trigger.
Now, when we pass through that, the lasers won't be bounced away.
They'll actually be able to be triggered by that trigger volume.
So again, we shoot a bunch of times,
I don't know if you can see it on my screen but I'll zoom in a little bit.
You can see over there, they're being instantiated and
then they're being destroyed and removed.
Excellent, so we've created ourselves a shredder.
Hopefully, that all made sense to you in this quick little video and
we'll jump in straight away for the next content.
See you soon.