In this lecture, we're going to flip our character.
So running right makes our character point right and running left makes our character point left.
Just very quickly, I'm going to give myself a little bit more platform here so I can make my character
run around without having him fall.
And yet, there we go.
Okay.
Just a little bit more space to run, run, run to do what we're doing in this lecture.
Now, I'm going to take you through a little bit of the logic, our flip and logic for this lecture
haha that joke and we have our character facing right and we're flipping our character and our character
is facing left.
So if the player is pushing right the velocity will be positive.
Yeah, because we know from our control input it's going from minus one to plus one and then we're applying
our run speed multiply to that.
So if it's left it's negative, if it's right, it's positive.
If we're moving right, we want to face right.
If we're moving left, we want to face to the left so we can change the facing direction to the right
of the left by changing the local scale using positive or negative values.
So let me just show you that bit of our logic very quickly in here.
Click on the player.
We've done this before into the transform.
If we look at the scale on the x axis plus one and minus one, it will change our character.
I was zooming a little bit here so you can see him doing a little pirouette.
We have minus one, minus ZP minus one and plus one.
So that's basically what we're going to be doing in code depending upon whether you're running to the
right or running to the left.
And then the last bit of our logic is to only change the facing direction if you're actually moving
so that the weird things don't happen when our velocity is zero, I might leave them in for our first
point and then we'll see what weird things happened.
And then we'll put in a little bit of an if statement to say only flip if you are actually moving at
the time.
And the other bit of set up, I'm going to take you through here.
We're going to be using some different math F methods.
We're going to be using math for abs, which is absolute.
It's not like rock hard abs of Steel six pack, it's a abs in absolute.
And that is going to allow us to say, what's the absolute value of our number?
If you're not familiar with absolute value, it means no matter what sign is on there, so positive
or negative, we make it the positive value.
So if -10.5, you can see in the example here becomes 10.5.
So it's a way of us just cleaning away the sign.
So we just get the absolute value irrespective of the science.
That's math for abs.
And then we also have math sign.
What does that do?
Well, that allows us to return a value of one when F is positive or zero and minus one when F is negative.
So no matter what our value is, if we're pushing to the right or we've got a thumbstick on a gamepad
and we're pushing halfway to the right, and it's plus 0.5 or plus 0.7 or it's -0.3 or -0.8.
Whether it's positive or negative, we're going to return a value of one or minus one just so we can
get the sign.
And what do you think we're going to do with that one and minus one?
We're going to apply it to our player on the local scale.
And the last math F type thing we're going to use is epsilon, which is the smallest, a tiny floating
point value, the smallest, most, most value.
We try to use math for epsilon instead of saying zero.
Sometimes we get some weird results if we say equal to zero because technically it's 0.000001.
So then we get some confusion around that.
So it's good practice to say greater or greater or equal to math after epsilon rather than to zero.
So they're the three things we're going to be using.
Now, let's get into our code and use some of these things.
First of all, we need to create ourselves a new method.
And I'm going to start by calling the method and I'll name this flip sprite.
We don't have that yet.
Parentheses, semicolon.
So we're going to go and create that.
I'll create that underneath.
Run void flip sprite parentheses.
Curly braces.
What are we going to do in here?
The first part of this is we're going to access the transform local scale.
Local scale of our object equals vector two.
And we need to put the new key word in front of that.
Now in here I will start off with math f dot sign because I want to get the sign of a particular thing.
I want to know, are you a positive or are you a negative?
What do you think is a little micro challenge for you?
What do we need to get the sign of?
Just take a moment to think about that and add that in if you think you've got the answer.
We've got a little bit of a hint up here.
We've got my rigid body velocity y that's on the up and down.
So let's use my rigid body dot velocity X.
So here's the velocity on the x axis.
Is it positive or negative?
Because we need that sign and then we don't want to change anything on the Y axis.
Leave it as it is.
So we're just going to put one in there for our vector two that we're returning and that we're putting
into our in here, into our scale.
Just leave that as one.
We don't need to go and change that.
One is fine.
Let's save and see if it's working.
At this stage.
There's more code for us to do, but it might maybe be working thus far.
Like on play.
Right?
Right.
Running left.
I'll do that.
He flips.
Running right, running left.
Now, as I indicated, when he turns left and then I let go, he flips back to the right again.
That's because zero when you've got no velocity, all at all is being treated as a positive.
So that's the next bit of our logic.
We're going to jump back into Flip Sprite and create ourselves a ball that says you're either moving
or you are not.
So ball player what should we call this?
Player has horizontal speed.
I know it's a bit of a mouthful, but when we look back at this in six months time, we'd be like,
Oh, I know what that means.
Makes sense.
Now, how do we find this?
Well, we need to say, is your movement greater than nothing?
But if we say movement greater than zero, that kind of thing, if we're currently moving to the left,
our movement is going to be negative.
And that's going to give us a bit of a mistake, because a negative movement is still movement.
So we want to use our absolute value in here.
So we want to use our math f dot absolute, our rock hard abs salute in there and we are going to pass
into this my rigid body dot.
What, what are we passing here.
Well the same thing is just below velocity dot x.
We need to know what's the absolute value of your movement.
And then we're going to say is greater than.
And I gave you a hint.
It's greater than what?
Greater than math F dot epsilon, which is another way of saying pretty much zero, but a little bit
technically cleaner.
So we'll save that.
And we now know that.
And within our actual flippy flippy in here, we're going to say if a player has horizontal speed.
So in other words, if that is true, then we want to flip.
Okay, we'll save all that up.
Hopefully this explanation is clicked for you.
It's a little bit tricky using math, words and concepts in there.
Any time we play around with math, I know a lot of people kind of glaze over and say, Oh, what are
we doing?
But I think we've got it right, left, any staying facing left because when we're stopped, we don't
need to go and flip it again.
Excellent.
So there we go.
We've now flipped our character Sprite using a little bit of mathematics and a couple of extra methods
in unity.
So great work.
I'll see you in the next lecture.