Hello again.
In this lecture we're using on collision, enter within our code to know when we bump, bump into something
and we can make our code do something.
In this case, we're just printing to the console that one object bumped into another object.
So let's jump in and get started.
First thing we need to do in this process is create ourselves some code that says we know when things
bumped into other things and we're interested in saying, when did the player bump into anything else
in the world?
So I'm going to add a script to the player that says, Let me know when I bumped into something.
And basically you could add all of this code into the one script you've got already.
You could have a whole ton of different functionality in there, but that's bad practice because then
you don't know what is going on.
It makes this script really long.
There's a lot to wade through, so it's better practice for each functionality that you need to make
a new script.
So let's right click in our assets area, create a C-sharp script.
I'm going to call this collision hit enter and then open up collision from within here.
We're not going to need start or updates, so I'm going to highlight all of those things from the comment
up above.
Start all the way down to the second last curly brace you see in here and delete that.
So now we just have the open and the closed curly braces that say, what would you like to have in this
class that's called collision.
Okay.
Well, tab in one little bit just there.
And I'm going to use another in-built method that unity has similar to start and update.
We've got something called on collision enter and we need the 2D version.
Just like in the last lecture where we're talking about sometimes we use non 2D, sometimes we use 2D.
When we're working in a 2D project with sprites, we need to use the 2D version of things.
And so I clicked on that to complete it and we get a whole bunch of other stuff in here as part of the
completion.
So in general I delete the word private from my methods.
Basically, private is a way of saying this can only be used in this class, it can't be used somewhere
else.
We can't update or change or modify or call this from somewhere else.
That's what private does.
But if we remove the private key word, it has exactly the same impact.
So I remove it just to make things cleaner.
But you can absolutely leave it there if you want that private keyword.
And now what I also do is I fix up my curly bracket there so that I have it nice and neatly aligned.
You can totally leave it on the line that it's on like that.
That's totally fine because whitespace doesn't matter in C sharp in unity.
So I could have it like that if I wanted to because the compiler is just going to ignore all that stuff.
It doesn't care about.
It doesn't care about whitespace.
We can have our lines like that.
We could do it like this if we wanted.
We could have a whole space in between there.
I think you get the picture.
Riding it like this is totally valid.
It'll work, but it looks a little bit ugly, I think, and a little bit messy.
So I'm just going to undo all these things.
And for me, what I like is to have my curly braces formatted like this.
So I do that any time we get this kind of auto completion a lot, reformat it like that.
And we also have some information that's being passed in collision to the other.
All you need to know for now is that if we bump into something when we have a collision event, we can
get information about the thing we bumped into.
We can find its name, we can find details about it.
We can change it if we want, says kind of cool.
We don't actually need that for the little exercise we're doing now.
So we could absolutely delete that.
We don't need to have that information in there, but I'm going to leave that in, just undo and leave
it in there because there's no harm in having it.
But I just want you to know that this does a thing.
We just don't need to use that thing just yet.
So I have a little challenge for you here.
This is something that we did in the very first section of the course.
We printed something to the console.
So the challenge I've got for you is to print something to the console using debug log print, a witty
message to the console when we collide with something.
So pause the video.
Give that a go now.
Okay.
So we need to add this within our code block.
So when we collide, we'll add something, we use debug log parentheses and then quotation marks.
What am I going to say?
I'm just going to say, ouch.
Is that witty?
I don't know.
But I think it's kind of funny myself.
Ouch semicolon save now back over into unity.
So there's one last step in this process.
Hopefully you figure this out when you're going through the challenge.
But if not, what's the last step we need to do to print out to the console?
Well, we need to click on our player and we need to add that script as a component so I can do it a
couple of ways.
One, I can click in here, grab collision, drag it in, or I can click on Add component and I can
add collision.
And you see our first one here is Collision, the script.
Click on that and we have a collision script as a component on our vehicle.
Now we'll click on console again and if we've set this up correctly and I click on play, turn towards
my object, charge, blink, and I've bumped into it and it says Ouch.
And you can see here in the console because we've got collapsed turned on, we've got a number all the
way over in the right hand side that says how many times we've called that and printed that to the console.
So if I back up and bounce back up and bounce back up and bounce, you can see that the number is going
up to four.
Now, what I could do is click on Collapse and then every time I bump into it, it will make a new line
in the console.
I've kind of pushed him all the way off the screen and see if I can push him back in again.
Might need to cheat by looking at the scene window.
Yep, yep.
Okay.
Push back into the world and you can see it'll add a new line.
If I click on Collapse, it'll collapse it all so that we see how many is in there.
Just a little trick there.
In terms of printing the console, it's really handy to use debug statements printing to the console
when you're doing things, particularly with collisions.
So, you know, collisions actually working and you're not sitting there scratching your head because
it's saying, Yep, I bumped into a thing which is really handy to do.
So that's the fundamentals of using collision to make things happen.
In our game.
I have a rigid body on at least one of these objects.
In this case, we have a rigid body on both.
But you need to read your body on at least one, and we need to have our colliders on both of these
objects.
Both objects need to have collider and now we can say when we bump into something, we can make our
code do particular things.
For the moment we're just printing out to the console, but we can do more interesting things in the
future when we bump into things within our world.
So there we go.
That's using on collision.
Enter within our game.
I'll see you in the next lecture.