Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » X velocity and Y velocity
- KeenanG4
-
Scratcher
75 posts
X velocity and Y velocity
So, making a platformer is hard, but I dont understand the velocity x and y, it kinda means nothing to be more exact.
- -ShadowOfTheFuture-
-
Scratcher
1000+ posts
X velocity and Y velocity
Using velocity variables makes it easier to create smoother and more realistic looking movement.
Let's say you're using a script like:
For this example, I'll specifically refer to x velocity.
While this script is enough to move a sprite horizontally, the motion looks pretty janky. When you press the arrow key, the sprite instantly jumps to its fastest possible speed, and when you release it, the sprite instantly stops moving. There's no acceleration or deceleration - or, more accurately, I suppose, there's infinite acceleration, and that's not how things naturally move.
In real life, when things start moving, they usually start from rest and accelerate gradually until they reach their maximum speed. Then, when they have to stop, they decelerate gradually until they're no longer moving.
If you're looking for the simplest possible way to move a sprite horizontally, that script will probably work fine for you. However, if you want your movement to feel much more smooth and polished, you can do something like:
Where previously you were stuck using a constant speed, introducing the x velocity variable lets you keep track of the object's speed separately and change it whenever you want. This gives you a lot more flexibility.
In this script, when you press the arrow key, you're not directly changing the sprite's position. Rather, you're changing it's velocity: you're accelerating it at a constant rate. The change x by (x velocity) is what changes the sprite's position, and set (x velocity) to (x velocity * 0.9) is responsible for slowing it down once you've released the key.
Overall, this results in more realistic-looking (and, in my opinion, better-looking) motion.
If you want a more detailed explanation, this project and this wiki article may be helpful.
Let's say you're using a script like:
forever
...
if <key [right arrow v] pressed?> then
change x by (10)
end
...
end
For this example, I'll specifically refer to x velocity.
While this script is enough to move a sprite horizontally, the motion looks pretty janky. When you press the arrow key, the sprite instantly jumps to its fastest possible speed, and when you release it, the sprite instantly stops moving. There's no acceleration or deceleration - or, more accurately, I suppose, there's infinite acceleration, and that's not how things naturally move.
In real life, when things start moving, they usually start from rest and accelerate gradually until they reach their maximum speed. Then, when they have to stop, they decelerate gradually until they're no longer moving.
If you're looking for the simplest possible way to move a sprite horizontally, that script will probably work fine for you. However, if you want your movement to feel much more smooth and polished, you can do something like:
forever
...
if <key [right arrow v] pressed?> then
change [x velocity v] by (1)
end
set [x velocity v] to ((x velocity) * (0.9))
change x by (x velocity)
...
end
Where previously you were stuck using a constant speed, introducing the x velocity variable lets you keep track of the object's speed separately and change it whenever you want. This gives you a lot more flexibility.
In this script, when you press the arrow key, you're not directly changing the sprite's position. Rather, you're changing it's velocity: you're accelerating it at a constant rate. The change x by (x velocity) is what changes the sprite's position, and set (x velocity) to (x velocity * 0.9) is responsible for slowing it down once you've released the key.
Overall, this results in more realistic-looking (and, in my opinion, better-looking) motion.
If you want a more detailed explanation, this project and this wiki article may be helpful.
- 09878901234321
-
Scratcher
500+ posts
X velocity and Y velocity
So, making a platformer is hard, but I dont understand the velocity x and y, it kinda means nothing to be more exact.
Here’s something that should work
define Move X (Joystick X)
change [ Speed X] by ((0.9) * (Joystick X))
set [ Speed X] to ((0.9) * (Speed X))
change x by (Speed X)
define Move Y (Joystick Y)
change [ Speed Y] by ((0.9) * (Joystick Y))
set [ Speed Y] to ((0.9) * (Speed Y))
change y by (Speed Y)
when green flag clicked
forever
Move X (<key [ D] pressed?> - <key [ A] pressed?>)
Move Y (<key [ W] pressed?> - <key [ S] pressed?>)
end
Edit over a year later, I didn’t realize they said platformer I think.
Last edited by 09878901234321 (June 12, 2023 13:17:45)
- KeenanG4
-
Scratcher
75 posts
X velocity and Y velocity
Thanks for helping me with the scripts! Now I understand, how to use them
- sonicIsAwesome8999
-
Scratcher
3 posts
X velocity and Y velocity
pleeeeeeeeeeeeeeaaaaaaaaaassssssssssseeeeeeee help my y velocity keeps sending my sprite to the bottom of the screen can you tell me why
https://scratch.mit.edu/projects/842079584/editor/ is the url to my project so can you help anyone
https://scratch.mit.edu/projects/842079584/editor/ is the url to my project so can you help anyone
- 09878901234321
-
Scratcher
500+ posts
X velocity and Y velocity
It’s probably because you either aren’t resetting your Y velocity when the green flag is clicked, or you have no ground collision. Also, avoid posting on old topics, it’s better to make your own.
- ooh__
-
Scratcher
2 posts
X velocity and Y velocity
when [ space] key pressed
repeat (10)
change y by (10)
repeat (10)
change y by (-10)
- Mazeman4
-
Scratcher
61 posts
X velocity and Y velocity
How do I make an X velocity cap tho?typically the deceleration by multiplying the speed by 0.9 caps it around 10 or 15 I think? but if you want to manually set one you can use something like this:
if <(x velocity) > [cap value]> then
set [x velocity v] to [cap value]
end
- KeenanG4
-
Scratcher
75 posts
X velocity and Y velocity
uh guys imma just close this, i left scratch and i dont need it anymore-
- Discussion Forums
- » Help with Scripts
-
» X velocity and Y velocity