In this lecture, we're making our enemy move and we're making our enemy flip direction.
When they fly and bump into the wall, they'll head the other direction.
So let's get started.
Let's kick off by creating a new script in our scripts folder.
Right click, create a C-sharp script and we'll call this C sharp script.
Enemy movement, open up enemy movement.
Just get rid of the stuff we don't need the couple of comments that we're absolute legends by now,
so we don't need to know those comments, just wasted space.
What we do need to do is to access the rigid body, because a rigid body is how we move things around
in the world.
So as we usually do, we're going to create a reference for rigid body 2D, which will be type and we'll
call it my rigid body semicolon.
While we're here.
I'd also like some way of us being able to have a variable to serialize the speed of the enemy.
So I'm going to create a new float and I'll do a serialized field before that.
Actually, that'd be wise, serialized, filled float.
We'll call this move speed and I'll initialize that.
And just straight up one F as a starting point, let's just get the enemy moving across the screen,
just just going from left to right, doing a bit of a move.
So an update we will access my rigid body dot velocity.
We've done this before with the player and that's going to equal what a little micro challenge for you
here.
It's going to equal new vector two and in parentheses we want move speed because this is on the x y
axis not move input, move speed x y excess comma zero because we don't want it to be doing any ups
and downs.
So we'll save that back over into unity.
We better add that script to the enemy so it knows what's going on.
Enemy and then just drag the enemy movement script over onto the enemy.
There we go, click on play actually to make this a little bit more hilarious because I think I know
what's going to happen.
We'll put the enemy over on the left, but the player on the right, the enemy should be moving from
left to right of screen.
It's not.
We've done something wrong.
Oh, goodness me.
My apologies.
If you knew this and you were yelling at me, yelling at the screen, I could edit out my mistake,
but.
Oh, why not?
We'll leave Rick as being a bit of a numpty.
So what have we missed in here?
We've missed getting the component.
My rigid body equals get component.
Rigid body to dx parentheses semicolon.
There we go.
That's a step that we missed.
Thank you.
And apologies if you knew that already.
If you didn't spot it.
Not to worry, I didn't spot it.
Moving too quick, moving with too much velocity.
So click on play now.
We'll see if our hilarious thing happens with the enemies bouncing across the screen.
Gets the player, hey player, let's go for a little bit of a ride.
Our center machine camera's moving along with it and he's going to say, Let's just drop you off the
cliff.
Not a problem.
I thought it was funny, but in hindsight it actually wasn't overly hilarious.
But let's move to the next stage, which is to get our enemy running down there, hitting the wall and
then turning around.
That was a pretty slow journey.
So we'll put the enemy really close for this and we want to make sure the enemy is touching right on
top of the ground.
Now, there's a little technique.
I was going to give this to you as a challenge, but it's a bit sneaky because it's a tough one to figure
out.
Or at least it took me a while to figure out.
I'm going to show you how we'll make the enemy turn.
And then if you're ready to add the code yourself, you can.
What we need to do on the enemy is add another component, yet another component which will be a box
collider 2d a little bit the way the player had feet in addition to this, this capsule that's going
to be the capsule is going to be for when it's player to enemy collision.
But this box collider that we're about to add is going to be for should I turn around yet so box collider
two DD We'll get in there and edit that a little bit using the edit collider What I want to do is make
a reverse periscope.
What does that even mean?
Well, you know, in submarines, how they have a periscope that sticks up out of the ground and you
can see something above the water.
We're doing it the other way around, which is this is going to be underground underneath here.
Put it back a little bit.
We want to have the we want to have it fairly narrow.
So what's going to happen here?
Let me explain.
Is that goober just w to movie?
It's going to bounce along.
Bounce, bounce, bounce, bounce.
We're going to use on Collision Exit, which means currently there's collision on the ground here.
There's collision on the box.
There's going to be collision on the wall as well.
And when we get there, don't, don't, don't at this point right here, he's exited all the collision.
This collision box is no longer touching our collider that is related to the ground.
At that point, we're going to say, hey, flip around and then he'll flip and go back the other way.
And we know how we flip is on our X scale.
We just hit negative.
And then when this is no longer colliding, we'll flip again.
So it's a little bit sneaky.
We're using on trigger exit.
So if that's enough for you to go on, if you want to jump in and work on the code for having it flip,
then pause the video and do so.
There'll be another challenge, a bit of a shorter challenge when I'm halfway through the code, but
if you're a bit more of a pro, you're ready for that.
Then go ahead and do that.
Hopefully.
My telescope, my periscope, my reverse periscope.
Did I call it a telescope before?
Probably did.
It's one of those lectures where I'm getting a few things wrong.
It's a reverse periscope, Rick.
Now we'll jump back over into our code.
Stop talking to ourselves.
We need to say what's going to happen when we do our on trigger trigger exit.
So similar to on trigger enter but it's void on trigger.
Exit to DD if we've got one of those.
There we go.
It's going to add in a whole bunch of extra stuff I didn't ask for.
Void on trigger exit to DD.
Great.
Now, the first thing I'll do in here is say move.
Speed equals what?
Move speed equals minus move speed.
Which is another way of saying whenever you exit your your reverse periscope exits the thing instead
of heading in a positive direction, head in a negative direction.
If you're currently heading in a negative direction, i.e. your x value is is negative, meaning you're
heading from right to left, then switch it by adding a minus.
And just a reminder on that, if you have a plus and you combine that with a minus, you get what?
You get a minus.
So in other words, adding a minus will actually be subtracting.
If you subtract a minus, that's the same as adding.
So that's just a little maths reminder there jump into our maths for games course if you want a reminder
on all these maths fundamentals as well.
It's a great cause for a lot of fundamental concepts in there.
So move speed equals minus, move speed.
And then we're going to do one more thing in here, which is to flip enemy facing parentheses semicolon.
This is a method we haven't created just yet, but I'm going to give that to you as a challenge to see
if you can figure out how to flip the facing direction of the enemy, which is important to make sure
our reverse periscope is the thing at the front that bumps into the wall.
So finish the flip and code haha there's a joke for you.
Finish our code to make the enemy flip directions.
There's a hint.
Look at the player movement script for some clues on how to do that.
So that's all I'll give you for this pause video.
Take on that challenge.
So you back in when you done.
Now.
What was I talking about?
Now play a movement code.
Well, if we scroll around and find where we're flipping the player, it's even called Flip Sprite in
here.
You can see we've got this transformed local scale equals new vector to blah blah, blah and we're using
math sign.
That's one of our clues.
I'm just going to copy this line in here.
This is a good practice.
I think in coding you want to have a look at what you've coded before or figured out before.
So I will create our flip enemy facing method, put in our curly braces, and within here I'll paste
the line that we had before.
Okay.
So we do want to transform the local scale and it does need to be a new vector.
Two We've got our math f sign my rigid body.
So this is going to say right here, it will say, what direction are you facing?
Our code is a little bit different to the player code because the player code is reading inputs.
So we know whether you're pushing a direction on the controller or on the keyboard.
So we're using that to read what's going on.
That's why that code is a little bit different here.
It's simply a case of saying whatever you're doing at the moment, just flip it the other way around
when this method gets called.
So in other words, when you use the on trigger exit to DX, so what we're going to do in here is put
a negative in front of a math sign.
I need another set of parentheses to wrap around that.
So basically we're saying whatever your current sign is, in other words, whatever your current scale
is, just flip that when we tell you to do flipping and that's on the x axis, we don't need to do anything
on the y axis.
That's why y axis is one f.
And just before I save and head back into unity, what have I missed in my method here?
I'm going to pretend I did this deliberately.
As a little bit of a micro challenge for you.
There's something I've missed in here, which means it won't work at all.
What I've missed is a return type, so avoid flip facing enemies.
What we need in there and back over into unity.
There's one last main thing we need to do in here.
Hopefully you figure this out.
When you were adding a code and actually getting your character to flip, we need to play around with
our collision matrix.
So currently the enemy is on the default layer.
I'm going to add a new layer which we will call enemies.
Enemies I think is a good layer, and then click back on Cuba and assign that layer to Cuba and then
over into edit project settings, physics 2D.
Hopefully you're starting to get the knack of this now with the layers and with the matrix, the enemies.
What do we want them to collide with?
Pretty much just the player, isn't it?
Not really anything else.
So I'm going to click all the other things.
Don't, don't, don't, don't, don't.
Just to be sure.
Background.
No bouncing.
No.
Do we want the enemies to bump into each other?
You may do if you've got a couple of enemies on a thing or you want them to pass through each other.
So passing through we would turn that off.
If you want them to bump, then you would turn that on.
So we'll turn that off for now.
Oh, and of course, we also need the enemy to collide with the ground or else we don't know when our
on trigger exit is happening.
So ground and player, probably not the climbing.
We don't want it to bump into ladders and we'll see if we've got some success here.
Just move you a little bit away from the wall so we can get a bit of a run up at it and bounce off it.
So here we go.
Bounce, bounce, bounce hits the wall.
Nothing happens.
And that's probably, I think because we need this box collider to DH to be a trigger because we were
using what on trigger exit.
Now when we click on play hopefully we've got some success here should run up to the wall, bounce into
the wall and head over in the other direction.
Excellent.
And so the play is probably going to mess up the enemy.
If we run into the enemy, bounce into it.
We'll set up some functionality in the future where the enemy hurts.
The player probably causes the player to die.
But for now, this is the functionality exactly that we want.
So well done.
Hopefully you did well in this lecture and I'll see you in an upcoming lecture.