For people looking to advance their skills in Scratch and add gravity and jumping to your 2D game. Simple to use and easily optimizable. This can work for platformers or other fun-skilled games.
-
1Why don't you find a tutorial for this? There are countless tutorials for this. – Falling10fruit Apr 20 '22 at 11:57
-
This is my tutorial. @Falling10fruit – Apr 20 '22 at 20:06
-
But he said "This can work for platformers or other fun-skilled games", what you added needs more for a platformer (ceiling & wall detection) – Falling10fruit Apr 21 '22 at 03:46
2 Answers
To emulate the effect on scratch, we would start by creating a gravity variable. Make sure you are viewing the code of the sprite you wish to apply the effect to.
Step 1: Create a new variable called Gravity.
Step 2: Change the Gravity
You are going to want the gravity to slowly speed up while your player or other object is not touching the ground, color, or other collision detected object. You can do this by creating this script.
When Flag is Clicked:
Forever:
If(not touching[object or color]):
Change(gravity) by -1
If(touching[object or color]):
Set(gravity) to 0
This will allow the gravity to slowly increase when the player is not colliding with the desired object. Now, you can apply this gravity by changing the y position of the player by gravity.
Full script:
When Flag is Clicked:
Forever:
If(not touching[object or color]):
Change(gravity) by -1
end
If(touching[object or color]):
Set(gravity) to 0
end
Change Y By(gravity)
Extra: If you want to take it to another level, you are able to add a jump effect. It is quite easy.
Script:
When Flag is Clicked:
Forever:
If(touching[object or color] and [key(up) is pressed]):
Set(gravity) to (desired jump height value)
Uhm, so assuming there are walls and ceilings in your platformer
First, you need the following variables
X_Vel (X's velocity)
Y_Vel (Y's velocity)
Alongside the X and Y of the player
And then assuming that the ground is a sprite called Level
Then you can make this for gravity
Y_Vel = Y_Vel - 1
Y = Y + Y_Vel
And then for floor collision
if (Touching Level) then
Ground <- Custom block
if (Space Pressed) then
Y_Vel = 10 <- For jumping
else
Y_Vel = 0 <- Player doesn't melt to the floor
end
end
And then for the custom "Ground" block
Define Ground
repeat (abs(Y_Vel))
if (Touching Level) then
Y = Y + ((abs(Y_Vel) / Y_Vel) * -1)
end
end
end
You might notice that the character won't actually go all the way back up if it doesn't hit the ground hard enough, and that's intentional and needed for wall collisions
You might also notice that the character might also go downwards too if Y_Vel is positive, which is also intentional and needed for ceiling collision
Moving on, we're going into the left and right code since we haven't finished the wall collision code, and because you said platformers. We're going to need velocity to move the player for the wall collisions so this is what goes next:
if (A Pressed) then <- Or Right arrow key
X_Vel = -10
end
if (D pressed) then <- Or Left arrow key
X_Vel = 10
end
X = X + X_Vel <- Move player
Feel free to edit the code above for more features like sprinting, acceleration, rolling, etc.
And then finally, wall collision
if (Touching Level) then
X = X + (X_Vel * -1)
X_Vel = 0
end
That's it... Unless you want to improve the wall collision and mimick the ground/ ceiling collision. Well, here you go!
Of course, it'd be much better if you just watch a YouTube tutorial on this problem.

- 197
- 1
- 12