1 00:00:04,070 --> 00:00:04,730 ‫All right. 2 00:00:04,730 --> 00:00:07,910 ‫So let's go ahead and see how we can start the game. 3 00:00:07,910 --> 00:00:13,730 ‫So far, we simply started by starting the game in Unity, but we want to start the game within the 4 00:00:13,730 --> 00:00:17,330 ‫game so the player can start the game by pressing the space key. 5 00:00:17,570 --> 00:00:20,540 ‫So let's do that by creating a new script. 6 00:00:20,540 --> 00:00:27,440 ‫So let's go to scripts, create C-sharp script, and I'm going to call it Game Manager. 7 00:00:27,590 --> 00:00:29,810 ‫That one is going to control our game states. 8 00:00:29,870 --> 00:00:34,400 ‫So let's create a game style game manager game object in here as well. 9 00:00:34,400 --> 00:00:41,360 ‫So Game Manager and let's drag that script right into our game manager. 10 00:00:41,360 --> 00:00:48,740 ‫I'm going to reset the position of the game manager just so that it's cleaned and into at the zero position. 11 00:00:48,920 --> 00:00:49,370 ‫All right. 12 00:00:49,370 --> 00:00:51,230 ‫So let's have a look at our game manager. 13 00:00:51,230 --> 00:00:57,020 ‫As you can see, it already gets a different kind of icon in Unity because the game manager is a very 14 00:00:57,020 --> 00:01:01,630 ‫special script, which, as I said, will take care of our game states. 15 00:01:01,640 --> 00:01:04,100 ‫So the game manager will be very simple. 16 00:01:04,100 --> 00:01:07,370 ‫We won't need to start or update method for now. 17 00:01:07,370 --> 00:01:12,830 ‫We will just need a boolean which is the state whether our game is started or not. 18 00:01:12,830 --> 00:01:22,640 ‫So public bool game started and I'm going to create a method public void start game and I'm going to 19 00:01:22,640 --> 00:01:29,210 ‫make it public so that I can access it or I can use it from outside of the game manager itself. 20 00:01:29,210 --> 00:01:38,810 ‫So I'm from another script and in here the only thing I do is I set game started to true whenever the 21 00:01:38,820 --> 00:01:42,860 ‫start game method is called, the only thing that happens our game started. 22 00:01:43,040 --> 00:01:44,690 ‫Boolean is set to true. 23 00:01:45,140 --> 00:01:51,770 ‫So now in our controller what we need is we need to access our game manager because what we want to 24 00:01:51,770 --> 00:01:58,310 ‫do is whenever the player press the space key, the fixed update method should, should recognize that 25 00:01:58,310 --> 00:01:59,810 ‫and should start the game. 26 00:01:59,990 --> 00:02:01,700 ‫So let's go ahead and do that. 27 00:02:01,940 --> 00:02:09,680 ‫So let's create a private game manager and let's call it game manager. 28 00:02:11,090 --> 00:02:17,810 ‫So now in the Awake method I need to initialize it, otherwise our game manager will be null and we'll 29 00:02:17,810 --> 00:02:18,470 ‫get an error. 30 00:02:18,470 --> 00:02:25,910 ‫So let's solve that find object of type and let's call that one game manager because we're just searching 31 00:02:25,910 --> 00:02:32,090 ‫for the object that has or that is of type game manager and we only have one and that's our game manager, 32 00:02:32,090 --> 00:02:32,480 ‫right? 33 00:02:32,480 --> 00:02:37,130 ‫So we want to have that one and it contains a script and we want to access it. 34 00:02:37,130 --> 00:02:43,310 ‫So what we can do now is we can check if the game has not started. 35 00:02:43,310 --> 00:02:52,340 ‫So game manager, not game manager, that game started, which means the boolean game started is false. 36 00:02:53,030 --> 00:02:54,530 ‫Then return. 37 00:02:55,070 --> 00:02:56,540 ‫So don't do anything. 38 00:02:57,620 --> 00:03:00,350 ‫So let's save that script and let's go back to Unity. 39 00:03:00,470 --> 00:03:01,490 ‫Check it out. 40 00:03:02,720 --> 00:03:03,890 ‫Let's run our game. 41 00:03:06,060 --> 00:03:08,730 ‫And we can see the player is not moving. 42 00:03:08,740 --> 00:03:09,990 ‫The game has not started. 43 00:03:10,260 --> 00:03:12,360 ‫The animation, however, is still there. 44 00:03:12,360 --> 00:03:16,980 ‫So our player is still animating and we should solve that because that's not a nice state. 45 00:03:16,980 --> 00:03:19,710 ‫We have the player here and he's fake running. 46 00:03:20,130 --> 00:03:22,350 ‫That's not fun, so we need to change it. 47 00:03:22,350 --> 00:03:24,420 ‫Actually, it's funny, but we need to change that. 48 00:03:24,420 --> 00:03:29,370 ‫So let's create a new state because we have only the two states here running and falling so we can either 49 00:03:29,370 --> 00:03:34,710 ‫run, which is great, or we can fall, which is not so great, but we can also just be idle. 50 00:03:34,710 --> 00:03:42,060 ‫So let's create an empty state and let's call that one idle and you guess it, right? 51 00:03:42,060 --> 00:03:45,960 ‫There is a motion in our assets for that which is called idle. 52 00:03:45,960 --> 00:03:50,940 ‫So we can just use that motion, which is an animation for the player when he's idle. 53 00:03:50,940 --> 00:03:56,700 ‫And now we have to move that transition empty to our idle state. 54 00:03:56,850 --> 00:03:57,990 ‫And how do we do that? 55 00:03:57,990 --> 00:04:00,150 ‫Well, you can't simply drag it from here. 56 00:04:00,150 --> 00:04:03,630 ‫What you have to do is set as layer default state. 57 00:04:03,630 --> 00:04:09,270 ‫So now our idle state is going to be the layer default state, which means this is where it starts from. 58 00:04:09,270 --> 00:04:12,960 ‫So the entry and then it goes directly to our idle state. 59 00:04:13,020 --> 00:04:14,190 ‫Well, that's great. 60 00:04:14,190 --> 00:04:16,470 ‫But now we don't get to run animation. 61 00:04:16,470 --> 00:04:18,870 ‫Let's have a look how this will look like. 62 00:04:19,670 --> 00:04:21,410 ‫So you see, our guy is moving. 63 00:04:21,410 --> 00:04:22,370 ‫He's breathing. 64 00:04:22,880 --> 00:04:26,060 ‫He's moving just a little bit, if you look precisely. 65 00:04:26,060 --> 00:04:30,740 ‫So now what we need to do is make a transition from idle to run. 66 00:04:31,220 --> 00:04:36,530 ‫And of course, we need to have a behavior here or a condition here as well. 67 00:04:36,530 --> 00:04:41,150 ‫So there is this transition from Idol to run and we need to condition. 68 00:04:41,150 --> 00:04:42,680 ‫So let's add a condition. 69 00:04:42,680 --> 00:04:46,540 ‫But so far we only have this following condition which is not the one that we need. 70 00:04:46,550 --> 00:04:51,860 ‫We need to know whether the game has started or not because if the game has started, we want the run 71 00:04:51,860 --> 00:04:53,510 ‫animation to start. 72 00:04:53,510 --> 00:04:57,350 ‫So in parameters we create a new parameter. 73 00:04:57,380 --> 00:05:00,170 ‫So you might have an idea of how to do that. 74 00:05:00,290 --> 00:05:01,880 ‫You can do that with a trigger again. 75 00:05:01,880 --> 00:05:06,200 ‫So let's create a new trigger game started. 76 00:05:06,230 --> 00:05:13,310 ‫So I'm going to call that trigger and now we can open up the transition and now move or choose the game 77 00:05:13,310 --> 00:05:15,020 ‫started transition here. 78 00:05:15,020 --> 00:05:18,980 ‫Now we can click here and choose the game started trigger here. 79 00:05:18,980 --> 00:05:25,970 ‫So that's a condition the game has been has to be started and of course we can set that up here as well. 80 00:05:25,970 --> 00:05:37,070 ‫So if the game manager game started is not false, which means it's true, then we want to use the anim 81 00:05:37,070 --> 00:05:41,960 ‫dot set trigger with game started. 82 00:05:42,890 --> 00:05:48,710 ‫So that means we will start our game in here and we will start the animation in here. 83 00:05:49,730 --> 00:05:51,860 ‫So let's save that script and check it out. 84 00:05:54,650 --> 00:06:00,200 ‫And we still have the problem that we can't start the game, we can press enter and then our players 85 00:06:00,200 --> 00:06:04,040 ‫rotating, but we don't start the game with the game manager yet. 86 00:06:04,040 --> 00:06:05,420 ‫So let's do that. 87 00:06:05,420 --> 00:06:08,600 ‫Let's go to our game manager and add an. 88 00:06:09,910 --> 00:06:13,810 ‫The private void update method. 89 00:06:14,650 --> 00:06:17,290 ‫And in here we just check for an input. 90 00:06:19,000 --> 00:06:29,740 ‫So if input dot get key down and key code let's say is return. 91 00:06:29,740 --> 00:06:33,220 ‫So whenever somebody presses return, we want the game to start. 92 00:06:33,250 --> 00:06:41,140 ‫Then go ahead and call the start game method, which means means just set the game started boolean to 93 00:06:41,170 --> 00:06:41,830 ‫true. 94 00:06:42,970 --> 00:06:44,560 ‫So let's test that. 95 00:06:45,190 --> 00:06:48,370 ‫Let's pause the game and let's run it again. 96 00:06:50,820 --> 00:06:54,900 ‫So now if we start that, we will see that our player. 97 00:06:54,930 --> 00:07:01,320 ‫Well, let's go ahead and press enter and as you can see, our player only after a long period starts 98 00:07:01,320 --> 00:07:04,050 ‫to run, even though he's floating over the air already. 99 00:07:04,470 --> 00:07:05,730 ‫And that's not great. 100 00:07:05,730 --> 00:07:07,700 ‫And we can change that very quickly. 101 00:07:07,710 --> 00:07:10,170 ‫So let's go to our idle animation. 102 00:07:10,470 --> 00:07:17,940 ‫And in here, let's go to the transition and we can choose now to have an exit time. 103 00:07:17,940 --> 00:07:22,770 ‫So we don't want to have this exit time because now our animation will be finished. 104 00:07:22,770 --> 00:07:30,300 ‫And only after this idle animation, which takes almost 4 seconds, is over, we will go over to the 105 00:07:30,300 --> 00:07:34,920 ‫run state and we don't want that so we can deactivate has exit time. 106 00:07:34,920 --> 00:07:41,160 ‫So that means it will directly go to our run animation and as well we want to move our run animation 107 00:07:41,160 --> 00:07:47,640 ‫or our run state here just a little bit so to to the right so that it goes without any transition. 108 00:07:47,640 --> 00:07:50,400 ‫And if we just move the run, we'll always go back. 109 00:07:50,400 --> 00:07:57,120 ‫So we need to move this piece here towards the end of the Idol state and then we go to the run state. 110 00:07:57,450 --> 00:07:57,810 ‫All right. 111 00:07:57,810 --> 00:08:02,430 ‫So now let's test that again and let's see what happens as soon as we press enter. 112 00:08:02,700 --> 00:08:06,300 ‫As you can see, it goes directly to the running state. 113 00:08:06,330 --> 00:08:11,400 ‫Now you can do the same thing with this has exit to your following state. 114 00:08:11,400 --> 00:08:12,600 ‫So let's have a look. 115 00:08:12,600 --> 00:08:18,720 ‫Let's press enter and we fall and we still run two or three steps and then we do the animation of falling. 116 00:08:18,720 --> 00:08:20,910 ‫So you could do the same thing here. 117 00:08:21,210 --> 00:08:25,620 ‫Let's go ahead and change the exit time and go over to. 118 00:08:26,540 --> 00:08:27,800 ‫Following directly. 119 00:08:28,250 --> 00:08:29,660 ‫So now let's do that again. 120 00:08:29,690 --> 00:08:30,650 ‫Let's have a look. 121 00:08:30,920 --> 00:08:35,870 ‫If we fall directly or not and we can see we fall directly. 122 00:08:35,870 --> 00:08:38,990 ‫So as soon as we stop falling, well, we can still rotate. 123 00:08:38,990 --> 00:08:43,010 ‫But as soon as we stop falling or start, we will start with the falling animation. 124 00:08:43,250 --> 00:08:45,080 ‫All right, so that's about that. 125 00:08:45,410 --> 00:08:47,210 ‫Now we can start the game in the next video. 126 00:08:47,210 --> 00:08:48,980 ‫We will see how we can restart the game.