1 00:00:00,000 --> 00:00:07,000 Hello, in this video tutorial I'm going to show you a different way in which you can move your character 2 00:00:08,000 --> 00:00:16,000 Till now we have moved our character by hitting the right arrow and left arrow keys 3 00:00:16,000 --> 00:00:23,000 and using when, right arrow key pressed, point right and move 4 00:00:23,000 --> 00:00:33,000 and when, left arrow key pressed, point left and move 5 00:00:33,000 --> 00:00:38,000 and now we are going to use a different block 6 00:00:38,000 --> 00:00:46,000 instead of using move we are going to use change the x coordinate 7 00:00:46,000 --> 00:00:48,000 right? 8 00:00:48,000 --> 00:00:56,000 and instead of using when, right arrow key pressed, we are going to use if 9 00:00:56,000 --> 00:01:04,000 and under sensing, key pressed 10 00:01:04,000 --> 00:01:11,000 if right arrow key pressed, then change x by 10 11 00:01:11,000 --> 00:01:15,000 this has to be inside a forever 12 00:01:15,000 --> 00:01:24,000 and this two blocks, the forever and the if, will do the same as the when, right arrow key pressed 13 00:01:24,000 --> 00:01:27,000 why are we using this two instead of this one? 14 00:01:27,000 --> 00:01:31,000 because we want something to be included here inside the forever 15 00:01:31,000 --> 00:01:39,000 whenever this key or the left arrow key are not pressed 16 00:01:39,000 --> 00:01:44,000 so I'm going to duplicate this for the left one 17 00:01:44,000 --> 00:01:52,000 so if left arrow key pressed, change by a negative amount now 18 00:01:52,000 --> 00:01:59,000 and this is going to be under a green flag, so it happens from the beginning 19 00:01:59,000 --> 00:02:05,000 now what's happening here, we are not going to use those two now 20 00:02:06,000 --> 00:02:11,000 you may imagine something is wrong after 21 00:02:11,000 --> 00:02:15,000 this to the right and this to the left 22 00:02:15,000 --> 00:02:25,000 now, we want it to point into the direction 23 00:02:25,000 --> 00:02:31,000 point to the right and now point to the left 24 00:02:31,000 --> 00:02:34,000 and now something else is going to happen 25 00:02:34,000 --> 00:02:39,000 you may be imagining it by now 26 00:02:39,000 --> 00:02:41,000 ok, so let's check it 27 00:02:41,000 --> 00:02:44,000 going right and now going left is upside down 28 00:02:44,000 --> 00:02:50,000 always because we haven't placed set rotation style left right 29 00:02:50,000 --> 00:02:55,000 so at the beginning we set rotation style left right 30 00:02:55,000 --> 00:03:00,000 and now it's moving right and moving left 31 00:03:00,000 --> 00:03:07,000 let's say I also place the cat at a starting position 32 00:03:07,000 --> 00:03:11,000 so I will drag and drop it and use the go to block 33 00:03:13,000 --> 00:03:19,000 and now I have a motion to the right and motion to the left 34 00:03:19,000 --> 00:03:26,000 if I want to include the animation I can do it at a separate thread like this 35 00:03:27,000 --> 00:03:41,000 so from the beginning I use a forever and a next custom 36 00:03:41,000 --> 00:03:49,000 and now I can use a wait here so I can control the rhythm of the animation 37 00:03:49,000 --> 00:03:55,000 independent from the speed of the motion, let's say 0.2 38 00:03:55,000 --> 00:03:59,000 and now this is what happens, it's animated 39 00:03:59,000 --> 00:04:04,000 but then it's animated even though it's not moving 40 00:04:04,000 --> 00:04:14,000 so I want this animation to happen only if I'm pressing either one of these two keys or any key 41 00:04:14,000 --> 00:04:16,000 so I'm going to use an if 42 00:04:17,000 --> 00:04:28,000 and again under sensing key press and I will pick here any key 43 00:04:28,000 --> 00:04:37,000 so this will happen only when I'm hitting the keys 44 00:04:37,000 --> 00:04:45,000 so now it's not animated, I hold down the right arrow key it's animated 45 00:04:45,000 --> 00:04:47,000 I hold down the left arrow key it's animated 46 00:04:47,000 --> 00:04:58,000 so till now nothing new from what we accomplished using when, right arrow, key press, point right and move 47 00:04:58,000 --> 00:05:05,000 but I want this to be more realistic so I'm going to include some acceleration 48 00:05:05,000 --> 00:05:14,000 so for this to accelerate this number should change 49 00:05:14,000 --> 00:05:20,000 so for the number to change I'm going to use a variable 50 00:05:20,000 --> 00:05:28,000 I go into variables and make a variable, I'm going to call it xSpeed 51 00:05:28,000 --> 00:05:38,000 and I want it to be 0 at the beginning 52 00:05:39,000 --> 00:05:48,000 and so now instead of changing the position I'm going to change the speed 53 00:05:48,000 --> 00:05:57,000 so I will change the horizontal speed when I hit the right arrow key 54 00:05:57,000 --> 00:06:05,000 I'm going to change it by let's say 2, you can make it go faster or slower by changing this number 55 00:06:05,000 --> 00:06:15,000 and if going left I want to change the horizontal speed by a negative number 56 00:06:15,000 --> 00:06:25,000 but now I'm not changing the position so it's not actually moving it's just pointing left and right 57 00:06:25,000 --> 00:06:35,000 for the change to happen this has to be under the green flag 58 00:06:35,000 --> 00:06:45,000 so it happens from the beginning and we want the change to happen forever 59 00:06:46,000 --> 00:06:56,000 but we don't want the change to be just 10, we want it to be a different value 60 00:06:56,000 --> 00:07:03,000 depending on if we hit the left or right arrow keys 61 00:07:03,000 --> 00:07:09,000 so the change in position will be the value of the variable 62 00:07:09,000 --> 00:07:16,000 and we change the value of the variable here by hitting the right or the left arrow keys 63 00:07:16,000 --> 00:07:22,000 so at the beginning the xSpeed will be 0 64 00:07:22,000 --> 00:07:30,000 if I hit the right arrow once it will become 2 65 00:07:30,000 --> 00:07:36,000 if I hit the left one it will become, actually I hit it twice 66 00:07:36,000 --> 00:07:41,000 if I keep it hit it, it goes faster and faster, you see 67 00:07:41,000 --> 00:07:46,000 because it becomes 2, then 4, then 6, then 8, 10 and so on 68 00:07:46,000 --> 00:07:57,000 and if I hold down the left arrow key it becomes 69 00:07:57,000 --> 00:08:02,000 greater than in the negative side so it accelerates to the left 70 00:08:02,000 --> 00:08:07,000 so now the longer I keep it the faster it goes 71 00:08:07,000 --> 00:08:14,000 but then this is not realistic because it doesn't stop 72 00:08:14,000 --> 00:08:23,000 so as I said before I want something to happen when I'm not hitting the keys 73 00:08:23,000 --> 00:08:32,000 when I release my finger from the keys I want it to decelerate and stop automatically 74 00:08:32,000 --> 00:08:42,000 so how I make the xSpeed a lesser and lesser value automatically 75 00:08:42,000 --> 00:08:49,000 I'm going to multiply it by a number lesser than 1 76 00:08:49,000 --> 00:08:53,000 so I go to operators and multiply 77 00:08:53,000 --> 00:09:05,000 so if I make the xSpeed xSpeed times 0.9 it becomes a small number 78 00:09:05,000 --> 00:09:14,000 so I'm going to set the value of the xSpeed to a new one 79 00:09:14,000 --> 00:09:20,000 which is the previous one that made it smaller 80 00:09:20,000 --> 00:09:28,000 and if I put it into the forever loop it will go smaller and smaller 81 00:09:28,000 --> 00:09:37,000 so, see, we start here, it's 0, I hold down the right arrow key 82 00:09:37,000 --> 00:09:41,000 the xSpeed will go up and as soon as I release it 83 00:09:41,000 --> 00:09:47,000 it will start automatically to get smaller until it gets to 0 84 00:09:47,000 --> 00:09:50,000 there it goes 85 00:09:50,000 --> 00:09:54,000 and the same thing happens when going to the left 86 00:09:54,000 --> 00:09:57,000 accelerates and then decelerates 87 00:09:57,000 --> 00:10:05,000 so now it automatically decelerates and it takes a little time for it to happen 88 00:10:05,000 --> 00:10:07,000 so it becomes so realistic 89 00:10:07,000 --> 00:10:13,000 it accelerates and when I release the key it decelerates and stops 90 00:10:13,000 --> 00:10:22,000 why? because imagine I hold the right key until this changes from 0 to 2 91 00:10:22,000 --> 00:10:26,000 then to 4, 6, 8 and 10 92 00:10:26,000 --> 00:10:30,000 and then I release the key 93 00:10:30,000 --> 00:10:37,000 so the xSpeed is 10 and now it multiplies by 0.9 so it becomes 9 94 00:10:37,000 --> 00:10:46,000 so in the next loop the 9 becomes 9 times 0.9 95 00:10:46,000 --> 00:10:50,000 so 8.1 96 00:10:50,000 --> 00:10:58,000 and then at the next cycle 8.1 times 0.9 becomes 7 point something 97 00:10:58,000 --> 00:11:02,000 and then 7 point something times 0.9 becomes 6 point something 98 00:11:02,000 --> 00:11:07,000 and so it decreases automatically until it stops 99 00:11:07,000 --> 00:11:12,000 so, let's see what's in here 100 00:11:12,000 --> 00:11:16,000 first of all, the motion is all in here 101 00:11:16,000 --> 00:11:21,000 the position changes by the value of the xSpeed variable 102 00:11:21,000 --> 00:11:24,000 at the beginning it's 0 103 00:11:25,000 --> 00:11:30,000 any time we hit a right arrow this value increases 104 00:11:30,000 --> 00:11:35,000 so if we keep it hit the xSpeed will become greater 105 00:11:35,000 --> 00:11:38,000 and the change in x will become greater 106 00:11:38,000 --> 00:11:40,000 so it accelerates to the right 107 00:11:40,000 --> 00:11:46,000 same thing happens but in reverse when we hit the left key 108 00:11:46,000 --> 00:11:51,000 if we hold it the xSpeed will decrease 109 00:11:51,000 --> 00:11:55,000 and it will become greater than negative 110 00:11:55,000 --> 00:11:59,000 so it will move faster to the left, accelerating to the left 111 00:11:59,000 --> 00:12:03,000 and if we are not hitting any key 112 00:12:03,000 --> 00:12:11,000 then the horizontal speed, the value by which the coordinate changes 113 00:12:11,000 --> 00:12:16,000 will decrease automatically until it becomes 0 114 00:12:16,000 --> 00:12:20,000 so it will automatically decelerate 115 00:12:20,000 --> 00:12:25,000 and that's the new way in which you can move your character 116 00:12:25,000 --> 00:12:30,000 while the animation is taken care of in this group here 117 00:12:30,000 --> 00:12:41,000 and this if any key pressed will make the animation go whenever you hit any key 118 00:12:41,000 --> 00:12:44,000 and stop if you are not hitting it 119 00:12:44,000 --> 00:12:49,000 and that's what I wanted you to learn in this tutorial 120 00:12:49,000 --> 00:12:50,000 thank you