1
00:00:03,110 --> 00:00:07,430
In this lecture we're setting up so that when we run, we change to our running animation state, and

2
00:00:07,430 --> 00:00:10,640
then when we stop and idle, we're back into our idling animation state.

3
00:00:10,640 --> 00:00:15,200
Just a reminder, in case you've forgotten, the animated window is where we can see the animator information.

4
00:00:15,200 --> 00:00:19,340
And if we click on play, you can see that the character is probably just going to be idling, I think.

5
00:00:19,340 --> 00:00:20,420
Idle, idle, idle.

6
00:00:20,420 --> 00:00:20,720
Yeah.

7
00:00:20,780 --> 00:00:25,100
They see the idle is going backwards and forward as the animation loops through and that character is

8
00:00:25,100 --> 00:00:30,800
just idling on the spot we set up before and is running bool that if it's true then see our character

9
00:00:30,800 --> 00:00:34,850
is running and then if is running is false then goes back to idling.

10
00:00:34,850 --> 00:00:37,310
So that's the essence of what we're trying to do in our code.

11
00:00:37,310 --> 00:00:41,870
We want to say if there's a particular condition met, then turn this ball to be true.

12
00:00:41,870 --> 00:00:42,680
What is that condition?

13
00:00:42,680 --> 00:00:46,880
Well, if we're running, of course, and that's going to come from whether we're pushing the controller

14
00:00:46,880 --> 00:00:49,190
or the keyboard, right and left.

15
00:00:49,190 --> 00:00:54,260
So over in our code, the very first thing we need to do is set up a reference for our animator or cash

16
00:00:54,280 --> 00:00:55,250
reference the animator.

17
00:00:55,250 --> 00:00:56,930
So little micro challenge for you here.

18
00:00:56,930 --> 00:00:59,870
Pause a video and cache a reference to the animator.

19
00:01:02,760 --> 00:01:04,290
Underneath the mirror your body.

20
00:01:04,290 --> 00:01:10,530
I'm going to create a variable which is of type animator and I'll call this my animator using the my

21
00:01:10,530 --> 00:01:15,450
convention here, my animator, my rigid body, etc. Now within start I'm going to copy the previous

22
00:01:15,450 --> 00:01:15,840
line.

23
00:01:15,840 --> 00:01:18,180
I don't know if that's going to save me a lot, but we'll see.

24
00:01:18,300 --> 00:01:21,590
Instead of my rigid body, it's going to be my animator.

25
00:01:21,600 --> 00:01:26,280
If I can spell animator, get component is going to be animator.

26
00:01:26,310 --> 00:01:26,760
So there we go.

27
00:01:26,760 --> 00:01:30,780
We've cached a reference, the animator that we can use in any place throughout this particular class.

28
00:01:30,780 --> 00:01:34,830
Now, where's the logical spot for us to have the animation play for running?

29
00:01:34,830 --> 00:01:39,000
Well, it's in the run method, so give ourselves a little bit of space in here.

30
00:01:39,000 --> 00:01:42,690
We'll start by doing this in a simple way and then we'll improve it after that.

31
00:01:42,690 --> 00:01:48,690
So the simple way my animator dot set bool, that's how we can change the ball from true to false.

32
00:01:48,690 --> 00:01:51,960
And in parentheses, the first part of this is the name of the ball.

33
00:01:51,960 --> 00:01:57,300
So I'm going to jump back over into my animator just click on is running a couple of times copy that.

34
00:01:57,300 --> 00:01:59,640
So I get the exact spelling this string references.

35
00:01:59,640 --> 00:02:04,350
We don't really like string references, but sometimes we use them just because we got two and then

36
00:02:04,350 --> 00:02:08,699
comma and we'll say true that will set our animation to be is running.

37
00:02:08,699 --> 00:02:09,780
Let's see if that works.

38
00:02:09,780 --> 00:02:14,550
First of all, click on play and instead of idling you can see our character is running as evidenced

39
00:02:14,550 --> 00:02:17,460
by our animation looping through the running state.

40
00:02:17,460 --> 00:02:17,910
Excellent.

41
00:02:17,910 --> 00:02:19,620
So that's the first part of what we wanted to do.

42
00:02:19,620 --> 00:02:24,390
But we need to set something up so that we're only running if we're actually moving.

43
00:02:24,420 --> 00:02:26,790
The challenge is to finish the run state.

44
00:02:26,790 --> 00:02:32,130
So make the is running ball true if we're running and false if we're not and if that's enough for you

45
00:02:32,130 --> 00:02:34,950
to go on, if you're ready to dive into that challenge, take that on.

46
00:02:34,950 --> 00:02:37,260
Now, if you want a hint, I'll give you a hint.

47
00:02:37,260 --> 00:02:43,260
And that hint is going to be to use the code we created for flipping our sprite.

48
00:02:43,260 --> 00:02:48,750
So somewhere within flipping our sprite, we're saying if the player is actually moving, then do this

49
00:02:48,750 --> 00:02:53,280
thing so you can grab a line from that previous code we created to see if our character is moving.

50
00:02:53,280 --> 00:02:55,110
So pause the video, take on that challenge.

51
00:02:55,110 --> 00:02:56,220
See you back in when you're done.

52
00:02:58,870 --> 00:03:01,750
What is this mysterious line I speak of?

53
00:03:01,750 --> 00:03:02,170
So.

54
00:03:02,200 --> 00:03:04,610
Ball player has horizontal speed.

55
00:03:04,630 --> 00:03:09,130
If our velocity is greater than zero or epsilon.

56
00:03:09,130 --> 00:03:14,200
So I'm going to copy that pop that just above our my animator set.

57
00:03:14,230 --> 00:03:19,700
Ball so we now have a ball within the run method that knows if we're moving and instead of my animated

58
00:03:19,870 --> 00:03:24,760
set ball is running true instead of true in here we want this to be true if you're running false if

59
00:03:24,760 --> 00:03:30,490
not so we can just use the same ball player has horizontal speed because this will resolve as true if

60
00:03:30,490 --> 00:03:32,050
you're moving and false if you're not moving.

61
00:03:32,050 --> 00:03:34,330
So save on that back into unity.

62
00:03:34,360 --> 00:03:38,800
Click on play so you can see on the screen the character is running pretty little, skipping along when

63
00:03:38,800 --> 00:03:39,220
we're running.

64
00:03:39,220 --> 00:03:43,000
And also you can see the states are flipping backwards and forwards when we're running and not running

65
00:03:43,000 --> 00:03:46,060
and our character Sprite is still flipping in the right kind of way.

66
00:03:46,060 --> 00:03:46,840
So everything's good.

67
00:03:46,840 --> 00:03:51,490
This is just the first state running we'll have climbing and probably a dire state and a couple of other

68
00:03:51,490 --> 00:03:52,090
things later on.

69
00:03:52,090 --> 00:03:57,420
But I just wanted to show you the fundamental mechanism of changing our animation state in our code.

70
00:03:57,430 --> 00:03:57,970
Good stuff.

71
00:03:57,970 --> 00:03:59,020
I'll see in the next lecture.

