1
00:00:03,030 --> 00:00:05,040
In this lecture, we're going to flip our character.

2
00:00:05,040 --> 00:00:09,120
So running right makes our character point right and running left makes our character point left.

3
00:00:09,120 --> 00:00:13,410
Just very quickly, I'm going to give myself a little bit more platform here so I can make my character

4
00:00:13,410 --> 00:00:15,750
run around without having him fall.

5
00:00:15,750 --> 00:00:17,160
And yet, there we go.

6
00:00:17,190 --> 00:00:17,330
Okay.

7
00:00:17,400 --> 00:00:20,580
Just a little bit more space to run, run, run to do what we're doing in this lecture.

8
00:00:20,580 --> 00:00:25,290
Now, I'm going to take you through a little bit of the logic, our flip and logic for this lecture

9
00:00:25,290 --> 00:00:30,480
haha that joke and we have our character facing right and we're flipping our character and our character

10
00:00:30,480 --> 00:00:31,590
is facing left.

11
00:00:31,590 --> 00:00:34,860
So if the player is pushing right the velocity will be positive.

12
00:00:34,860 --> 00:00:40,350
Yeah, because we know from our control input it's going from minus one to plus one and then we're applying

13
00:00:40,350 --> 00:00:42,120
our run speed multiply to that.

14
00:00:42,120 --> 00:00:44,790
So if it's left it's negative, if it's right, it's positive.

15
00:00:44,790 --> 00:00:46,800
If we're moving right, we want to face right.

16
00:00:46,840 --> 00:00:51,270
If we're moving left, we want to face to the left so we can change the facing direction to the right

17
00:00:51,270 --> 00:00:55,680
of the left by changing the local scale using positive or negative values.

18
00:00:55,680 --> 00:00:58,230
So let me just show you that bit of our logic very quickly in here.

19
00:00:58,230 --> 00:00:59,040
Click on the player.

20
00:00:59,040 --> 00:01:01,290
We've done this before into the transform.

21
00:01:01,290 --> 00:01:06,340
If we look at the scale on the x axis plus one and minus one, it will change our character.

22
00:01:06,340 --> 00:01:08,850
I was zooming a little bit here so you can see him doing a little pirouette.

23
00:01:08,850 --> 00:01:13,260
We have minus one, minus ZP minus one and plus one.

24
00:01:13,260 --> 00:01:16,680
So that's basically what we're going to be doing in code depending upon whether you're running to the

25
00:01:16,680 --> 00:01:18,060
right or running to the left.

26
00:01:18,060 --> 00:01:23,130
And then the last bit of our logic is to only change the facing direction if you're actually moving

27
00:01:23,130 --> 00:01:28,470
so that the weird things don't happen when our velocity is zero, I might leave them in for our first

28
00:01:28,470 --> 00:01:30,090
point and then we'll see what weird things happened.

29
00:01:30,090 --> 00:01:34,710
And then we'll put in a little bit of an if statement to say only flip if you are actually moving at

30
00:01:34,710 --> 00:01:35,280
the time.

31
00:01:35,280 --> 00:01:37,140
And the other bit of set up, I'm going to take you through here.

32
00:01:37,140 --> 00:01:39,810
We're going to be using some different math F methods.

33
00:01:39,810 --> 00:01:42,510
We're going to be using math for abs, which is absolute.

34
00:01:42,510 --> 00:01:47,040
It's not like rock hard abs of Steel six pack, it's a abs in absolute.

35
00:01:47,040 --> 00:01:51,720
And that is going to allow us to say, what's the absolute value of our number?

36
00:01:51,720 --> 00:01:55,740
If you're not familiar with absolute value, it means no matter what sign is on there, so positive

37
00:01:55,740 --> 00:01:58,110
or negative, we make it the positive value.

38
00:01:58,110 --> 00:02:03,030
So if -10.5, you can see in the example here becomes 10.5.

39
00:02:03,030 --> 00:02:05,670
So it's a way of us just cleaning away the sign.

40
00:02:05,670 --> 00:02:08,699
So we just get the absolute value irrespective of the science.

41
00:02:08,699 --> 00:02:10,530
That's math for abs.

42
00:02:10,530 --> 00:02:12,300
And then we also have math sign.

43
00:02:12,300 --> 00:02:12,810
What does that do?

44
00:02:12,810 --> 00:02:19,020
Well, that allows us to return a value of one when F is positive or zero and minus one when F is negative.

45
00:02:19,020 --> 00:02:25,590
So no matter what our value is, if we're pushing to the right or we've got a thumbstick on a gamepad

46
00:02:25,590 --> 00:02:31,860
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.

47
00:02:31,860 --> 00:02:36,690
Whether it's positive or negative, we're going to return a value of one or minus one just so we can

48
00:02:36,690 --> 00:02:37,500
get the sign.

49
00:02:37,500 --> 00:02:40,170
And what do you think we're going to do with that one and minus one?

50
00:02:40,170 --> 00:02:44,220
We're going to apply it to our player on the local scale.

51
00:02:44,220 --> 00:02:49,410
And the last math F type thing we're going to use is epsilon, which is the smallest, a tiny floating

52
00:02:49,410 --> 00:02:51,090
point value, the smallest, most, most value.

53
00:02:51,120 --> 00:02:54,300
We try to use math for epsilon instead of saying zero.

54
00:02:54,300 --> 00:02:59,550
Sometimes we get some weird results if we say equal to zero because technically it's 0.000001.

55
00:02:59,550 --> 00:03:01,320
So then we get some confusion around that.

56
00:03:01,320 --> 00:03:07,380
So it's good practice to say greater or greater or equal to math after epsilon rather than to zero.

57
00:03:07,380 --> 00:03:09,150
So they're the three things we're going to be using.

58
00:03:09,150 --> 00:03:11,640
Now, let's get into our code and use some of these things.

59
00:03:11,640 --> 00:03:14,040
First of all, we need to create ourselves a new method.

60
00:03:14,040 --> 00:03:18,480
And I'm going to start by calling the method and I'll name this flip sprite.

61
00:03:18,480 --> 00:03:19,380
We don't have that yet.

62
00:03:19,380 --> 00:03:20,610
Parentheses, semicolon.

63
00:03:20,610 --> 00:03:22,020
So we're going to go and create that.

64
00:03:22,020 --> 00:03:23,160
I'll create that underneath.

65
00:03:23,160 --> 00:03:28,560
Run void flip sprite parentheses.

66
00:03:28,590 --> 00:03:29,520
Curly braces.

67
00:03:29,520 --> 00:03:30,780
What are we going to do in here?

68
00:03:30,810 --> 00:03:35,970
The first part of this is we're going to access the transform local scale.

69
00:03:35,970 --> 00:03:41,310
Local scale of our object equals vector two.

70
00:03:41,310 --> 00:03:43,950
And we need to put the new key word in front of that.

71
00:03:43,950 --> 00:03:50,850
Now in here I will start off with math f dot sign because I want to get the sign of a particular thing.

72
00:03:50,850 --> 00:03:53,550
I want to know, are you a positive or are you a negative?

73
00:03:53,670 --> 00:03:55,560
What do you think is a little micro challenge for you?

74
00:03:55,560 --> 00:03:58,170
What do we need to get the sign of?

75
00:03:58,170 --> 00:04:02,070
Just take a moment to think about that and add that in if you think you've got the answer.

76
00:04:03,910 --> 00:04:05,280
We've got a little bit of a hint up here.

77
00:04:05,280 --> 00:04:09,570
We've got my rigid body velocity y that's on the up and down.

78
00:04:09,570 --> 00:04:14,750
So let's use my rigid body dot velocity X.

79
00:04:14,760 --> 00:04:16,529
So here's the velocity on the x axis.

80
00:04:16,560 --> 00:04:17,540
Is it positive or negative?

81
00:04:17,550 --> 00:04:20,959
Because we need that sign and then we don't want to change anything on the Y axis.

82
00:04:20,970 --> 00:04:21,899
Leave it as it is.

83
00:04:21,899 --> 00:04:26,100
So we're just going to put one in there for our vector two that we're returning and that we're putting

84
00:04:26,100 --> 00:04:28,800
into our in here, into our scale.

85
00:04:28,800 --> 00:04:29,790
Just leave that as one.

86
00:04:29,790 --> 00:04:30,930
We don't need to go and change that.

87
00:04:30,930 --> 00:04:32,430
One is fine.

88
00:04:32,610 --> 00:04:33,960
Let's save and see if it's working.

89
00:04:33,960 --> 00:04:34,560
At this stage.

90
00:04:34,560 --> 00:04:38,370
There's more code for us to do, but it might maybe be working thus far.

91
00:04:38,400 --> 00:04:39,330
Like on play.

92
00:04:39,990 --> 00:04:40,200
Right?

93
00:04:40,230 --> 00:04:40,650
Right.

94
00:04:40,650 --> 00:04:41,250
Running left.

95
00:04:41,250 --> 00:04:41,650
I'll do that.

96
00:04:41,650 --> 00:04:42,030
He flips.

97
00:04:42,030 --> 00:04:42,990
Running right, running left.

98
00:04:43,000 --> 00:04:47,880
Now, as I indicated, when he turns left and then I let go, he flips back to the right again.

99
00:04:47,880 --> 00:04:53,130
That's because zero when you've got no velocity, all at all is being treated as a positive.

100
00:04:53,130 --> 00:04:54,660
So that's the next bit of our logic.

101
00:04:54,660 --> 00:04:59,550
We're going to jump back into Flip Sprite and create ourselves a ball that says you're either moving

102
00:04:59,550 --> 00:05:00,510
or you are not.

103
00:05:00,510 --> 00:05:02,940
So ball player what should we call this?

104
00:05:02,940 --> 00:05:06,480
Player has horizontal speed.

105
00:05:06,480 --> 00:05:09,840
I know it's a bit of a mouthful, but when we look back at this in six months time, we'd be like,

106
00:05:09,840 --> 00:05:10,850
Oh, I know what that means.

107
00:05:10,860 --> 00:05:11,520
Makes sense.

108
00:05:11,520 --> 00:05:12,630
Now, how do we find this?

109
00:05:12,630 --> 00:05:16,080
Well, we need to say, is your movement greater than nothing?

110
00:05:16,080 --> 00:05:22,620
But if we say movement greater than zero, that kind of thing, if we're currently moving to the left,

111
00:05:22,620 --> 00:05:24,450
our movement is going to be negative.

112
00:05:24,450 --> 00:05:29,190
And that's going to give us a bit of a mistake, because a negative movement is still movement.

113
00:05:29,190 --> 00:05:31,650
So we want to use our absolute value in here.

114
00:05:31,650 --> 00:05:39,780
So we want to use our math f dot absolute, our rock hard abs salute in there and we are going to pass

115
00:05:39,780 --> 00:05:42,480
into this my rigid body dot.

116
00:05:42,480 --> 00:05:44,100
What, what are we passing here.

117
00:05:44,670 --> 00:05:47,550
Well the same thing is just below velocity dot x.

118
00:05:47,550 --> 00:05:50,220
We need to know what's the absolute value of your movement.

119
00:05:50,220 --> 00:05:52,530
And then we're going to say is greater than.

120
00:05:52,530 --> 00:05:53,370
And I gave you a hint.

121
00:05:53,370 --> 00:05:54,660
It's greater than what?

122
00:05:55,140 --> 00:06:00,240
Greater than math F dot epsilon, which is another way of saying pretty much zero, but a little bit

123
00:06:00,240 --> 00:06:01,290
technically cleaner.

124
00:06:01,290 --> 00:06:02,390
So we'll save that.

125
00:06:02,390 --> 00:06:03,360
And we now know that.

126
00:06:03,360 --> 00:06:11,010
And within our actual flippy flippy in here, we're going to say if a player has horizontal speed.

127
00:06:11,010 --> 00:06:14,850
So in other words, if that is true, then we want to flip.

128
00:06:14,850 --> 00:06:15,810
Okay, we'll save all that up.

129
00:06:15,810 --> 00:06:17,970
Hopefully this explanation is clicked for you.

130
00:06:18,180 --> 00:06:21,060
It's a little bit tricky using math, words and concepts in there.

131
00:06:21,060 --> 00:06:24,480
Any time we play around with math, I know a lot of people kind of glaze over and say, Oh, what are

132
00:06:24,480 --> 00:06:24,900
we doing?

133
00:06:24,900 --> 00:06:30,300
But I think we've got it right, left, any staying facing left because when we're stopped, we don't

134
00:06:30,300 --> 00:06:31,500
need to go and flip it again.

135
00:06:31,500 --> 00:06:31,920
Excellent.

136
00:06:31,920 --> 00:06:32,400
So there we go.

137
00:06:32,400 --> 00:06:37,710
We've now flipped our character Sprite using a little bit of mathematics and a couple of extra methods

138
00:06:37,710 --> 00:06:38,310
in unity.

139
00:06:38,310 --> 00:06:38,850
So great work.

140
00:06:38,850 --> 00:06:39,780
I'll see you in the next lecture.

