Get Kids into Coding with Scratch

# Moving with Velocity

## Steve Kinney

Temporal

### Check out a free preview of the full Get Kids into Coding with Scratch course

The "Moving with Velocity" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates adding velocity to movement by implementing acceleration and deceleration to the sprite's horizontal movements.

Preview
Close

### Transcript from the "Moving with Velocity" Lesson

[00:00:00]
>> Okay, so we have this concept of jumping up and down. So first, we only went down, then we went up and down. Which directions have we not traveled yet?
>> Left and right.
>> Left and right.
>> I've traveled left and right.
>> I've traveled the left and rights before.

[00:00:19]
[LAUGH] I know them well. And in doing that, let's look at like a slightly different mechanic for that as well. And then again, you can combine any and all of these as you want. You might choose to use all of them. There's going to be one shocker where I started to pull them all together at one point.

[00:00:37]
But, let's see what that looks like. So, go ahead I'm just gonna make sure that's saved, and I'll publish it in a little bit, and we'll go to a new one, just I'm gonna swap this out for, I don't know something easy. Okay, so, when we had our left and right motion earlier, we basically said, if holding right, go right.

[00:01:06]
If holding left, go left, right? But there's no idea. You ever run really fast? You all have run before, right? You ever run really fast? And then can you stop all of a sudden? No, right? You gotta slow down, right? You can try to slow down real fast.

[00:01:23]
If you hit the brake on a car, you're not just gonna immediately stop. If somebody hits a brake too fast, like how you feel it, but most of the time you're running and then in the same way you accelerate, you can decelerate, right? And so we could have the same mechanic where maybe whatever your character is running towards the edge and they can slow down, especially like on ice levels where it can get even a little bit more nuanced.

[00:01:50]
But we can try that out as well. Inside this little ball, cuz I'm gonna do left and right. I'm gonna have you up and down. Cuz remember our taco chaser? That wasn't necessarily a platform or that was like top down, right? And we might wanna have it go in every direction.

[00:02:03]
So I'll do left and right, you do up and down, and then we'll kind of take a look at some other options there as well. So we'll go with this loop here. Same thing we've seen before. I should put these in my backpack. Hint, hint, I'll say, right arrow is pressed.

[00:02:35]
We'll start by, and again, you could tweak the mechanics to have an acceleration and a deceleration. Somebody earlier did a terminal velocity. You could start out by accelerate to a certain speed and then decelerate. I'm just gonna show you deceleration and I'll leave you acceleration as sort of play around with this the same basic idea.

[00:02:56]
Maybe I'll do, it we'll see how I feel. And we'll say we'll start the same thing we had before we'll start by have a sprint gonna have an x speed, we've got about velocity or y speed. Now we're gonna do x speed. So make a variable X speed.

[00:03:20]
Cool, and when the game starts we'll set that to 0, great. And when the right arrow is pressed, we will set it to 1. Set to 1. Let me get rid of that my variable. All right, so we'll set it to 1. What should we do in the left IRS press, pop quiz?

[00:03:52]
Negative one.
>> Negative one.
>> Negative one for the win. All right, we'll set it to negative one. Now that's not gonna do much different than we've seen before. I mean, it will do something that we don't want it to do. Prepare, so we'll do change x by the speed so that's either 1 or negative 1 as you can see.

[00:04:15]
And away we go. So X speed, change X by X speed. X speed is zero. I never put the ball back to where it's supposed to go. That'll do it. Zero. And so we go, this feels real sad, but I can't stop the ball. Once it's going, it's going, right?

[00:04:55]
So let's actually set maybe these to 10 because that was like molasses. We back and forth kinda like Pacman. But what I wanna do is I wanna hit the button. I want it to go, but then I want it to slow down. So I go, and then, if they're not holding it then I wanna do is like come to a stop.

[00:05:16]
And that's not happening right now. So now it's time for our good friend multiplication. If you multiply by less than one, you start to slow down slowly. So what we'll do is, instead of at every loop, we're gonna set the x-speed To the current x speed times, like 0.8.

[00:05:48]
So 80% is fast. So each each tick is gonna be 80% as fast, a little bit slower every time until we get to zero and the nice part is we don't have to even check because zero times 80% will give you zero. So we'll go ahead and we'll just pop in.

[00:06:15]
What, negative 10, sorry. I used a plus. [LAUGH] So that's why I kept trying to go always to the right, cuz I used plus. And I don't want to tell you how many times, standing this far away from my screen, I can't see the difference between those two.

[00:06:32]
Maybe you're younger than me. All right, pop that in. Now we should get the effect that we want. So I hit the button, it kinda come you can see that I can hold it. It slows down a little bit. Now it feels a little bit more natural in the way I would move a character in a game, right?

[00:06:55]
Rather than just going one at a time. It kind of goes, slows down,

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses