Scratch Platformer Game Series — Part 3: Scratch Platform Collision Detection
In Parts 1 and 2 of our Garden Glitch Scratch platformer, we taught Byte how to move left and right, jump with the space bar, and fall back toward the ground using gravity. Now we’re adding Scratch platform collision detection so Byte can recognize platforms, land on top of them, jump again, and fall normally when he walks off an edge.
Now we are ready for the part that actually starts making this project feel like a platformer.

In Part 3, we are adding platform collision detection. Byte needs to recognize when he lands on a platform, stop falling, stand on top of it, jump again, and fall normally if he walks off the edge.
Along the way, we are also going to clean up our Scratch code with custom blocks. Breaking a larger program into smaller jobs makes the code easier to read, test, debug, and expand as our game gets more complicated.
Play Garden Glitch Part 3
Use the left and right arrow keys to move Byte and press the space bar to jump.
Try jumping onto the platform, moving across it, jumping again while standing on it, and walking directly off the edge.
Open Garden Glitch Part 3 in Scratch >
What We’re Learning in Part 3
This part of the Garden Glitch series focuses on several important game-development concepts:
- platform collision detection
- determining whether a character is falling
- resolving sprite overlap
- tracking whether a player is on the ground
- detecting when a player walks off a platform
- organizing code with custom blocks
We are building directly on the movement and gravity systems from Parts 1 and 2, so you will want those working before continuing.
Start with the Garden Glitch Part 2 Project
At the end of Part 2, Byte could move with the arrow keys and jump using the space bar.
We also created a yVelocity variable that controls his vertical movement.
Positive yVelocity values move Byte upward. Gravity continually decreases that value until it becomes negative and Byte begins falling.
That gives us a jump that looks much more natural than simply telling a sprite to change its y-coordinate by a fixed amount.
For Part 3, we are going to keep that system and add surfaces that Byte can land on.
Add a Platform Sprite
Our greenhouse backdrop already contains shelves, ledges, counters, and other objects that look like possible platforms. However, Scratch cannot automatically understand that those objects should behave like solid surfaces.
Instead, we created a separate sprite called:
Platform
For this lesson, the platform can be extremely simple. We used a horizontal line placed where we want Byte to stand.
Keeping the collision sprite simple makes it much easier to understand what Scratch is actually detecting.
Later, a game could use invisible collision shapes that line up with detailed artwork, but we don’t need to make the system that complicated yet.
Why Use a Separate Platform Sprite?
A backdrop is just an image unless we write code that interprets its colors or other properties.
A Platform sprite gives us something very specific to test:
touching [Platform]?
Scratch can now ask whether Byte and the Platform sprite overlap.
There is one important limitation: Scratch checks the entire Byte costume. His hand, head, foot, or body can technically count as touching the platform.
For this beginner platformer, that is completely fine. More advanced games sometimes use smaller hitboxes or separate collision sensors, but that would add complexity we don’t need in Part 3.
Organize the Player Controller with Custom Blocks
By this point, Byte’s controller was starting to become fairly large.
Instead of keeping every movement, gravity, and collision block inside one enormous forever loop, we reorganized the code into custom blocks.
Our main script now reads almost like a list of jobs:
when green flag clicked
set rotation style [left-right]
go to x: (-175) y: (-50)
set [onGround] to (1)
set [yVelocity] to (0)
forever
movement
gravity
platformCollision
floorCollision
checkPlatformEdge
end
This doesn’t fundamentally change what Scratch is doing. It simply makes the program easier for us to understand.
Breaking a program into smaller parts like this is sometimes called decomposition.
Instead of solving one giant problem, programmers divide it into smaller problems that each have a specific job.

Part 1 Code: Create the Movement Block
We moved the arrow-key code from Part 1 into a custom block called:
movement
The block contains:
define movement
if <key [right arrow] pressed?> then
point in direction (90)
change x by (5)
end
if <key [left arrow] pressed?> then
point in direction (-90)
change x by (-5)
end
The right arrow increases Byte’s x-coordinate, moving him toward the right side of the Stage.
The left arrow decreases his x-coordinate and moves him toward the left.
We also change Byte’s direction so that his costume faces the direction he is traveling.
Part 2 Code: Create the Gravity Block
Next, move the gravity system into another custom block:
gravity
Inside it, place:
define gravity
change y by (yVelocity)
change [yVelocity] by (-1)
This is the same gravity logic we created in Part 2.
Byte’s y-position changes by the current yVelocity, and then gravity reduces yVelocity by 1.
If yVelocity starts at 12 during a jump, the values gradually move through:
12
11
10
9
...
2
1
0
-1
-2
-3
Positive numbers send Byte upward.
Negative numbers send Byte downward.
That becomes especially useful for collision detection because we can tell whether Byte is currently rising or falling just by looking at yVelocity.
Create the onGround Variable
Part 2 used Byte’s position on the greenhouse floor to decide whether he could jump.
That no longer works once we add elevated platforms.
Byte might be standing well above y = -50 and still need permission to jump.
Create a variable called:
onGround
Make this variable for this sprite only.
We use:
onGround = 1
to mean Byte is standing on a valid surface.
We use:
onGround = 0
to mean Byte is airborne.
At the beginning of the game, Byte starts on the greenhouse floor, so we initialize:
set [onGround] to (1)
Update the Jump Code
The space-bar script becomes:
when [space] key pressed
if <(onGround) = (1)> then
set [yVelocity] to (12)
set [onGround] to (0)
end
This is a much better rule than asking whether Byte is at one particular y-coordinate.
Scratch is now asking:
Is Byte standing on a surface?
If the answer is yes, he can jump.
As soon as the jump starts, onGround becomes 0 so pressing Space again in the air cannot immediately start another jump.
How Scratch Platform Collision Detection Works
Our Scratch platform collision detection system needs to answer three questions before Byte lands: Is he falling? Is he touching the Platform sprite? And is he positioned above the platform rather than hitting it from underneath?
Next, create a custom block called:
platformCollision
Our finished collision test looks like this:
define platformCollision
if <<(yVelocity) < (0)> and <touching [Platform]?>> then
if <(y position) > ([y position] of [Platform])> then
landOnPlatform
end
end
There are several important checks happening here.
First:
yVelocity < 0
means Byte must be falling.
We don’t want touching the underside of a platform while jumping upward to automatically count as landing.
Second:
touching [Platform]?
checks whether Byte overlaps the Platform sprite.
Finally, we compare:
y position
with:
[y position] of [Platform]
The first value is Byte’s position.
The second reporter reads the Platform sprite’s y-position.
This helps confirm that Byte is generally above the platform rather than approaching it from underneath.
Reading Another Sprite’s Position in Scratch
The [property] of [sprite] block is found in the Sensing category.
Select the Platform sprite in the second dropdown, and the first dropdown can then be changed to:
y position
That gives us:
[y position] of [Platform]
This is better than typing the Platform’s coordinate directly into our code.
If we move the Platform later, Scratch automatically reads its new position.
Resolve the Platform Collision
Detecting a collision is only half the problem.
Gravity can move Byte several pixels during a single loop. That means Scratch may not notice the collision until part of Byte is already overlapping the platform.
We need to correct that overlap.
Create another custom block called:
landOnPlatform
Our code is:
define landOnPlatform
repeat until <not <touching [Platform]?>>
change y by (1)
end
set [yVelocity] to (0)
set [onGround] to (1)
For this custom block, select Run without screen refresh.
That setting is important.
Without it, we could visibly see Byte moving upward one pixel at a time as Scratch pushes him out of the platform.
Running the custom block without screen refresh makes that correction happen essentially instantly.
The sequence becomes:
fall → detect collision → move out of overlap → stop falling → stand on platform
This process is called collision resolution.

Why Change Y by 1?
You might wonder why we don’t simply move Byte to a specific coordinate.
Moving one pixel at a time means our code doesn’t need to know exactly how far Byte penetrated the platform.
Scratch simply keeps doing:
change y by (1)
until:
not touching [Platform]?
becomes true.
That makes the correction adapt automatically to different collision depths.
Keep the Original Greenhouse Floor
Byte still needs somewhere to land when he isn’t standing on the elevated Platform sprite.
Create another custom block called:
floorCollision
Add:
define floorCollision
if <(y position) < (-50)> then
set y to (-50)
set [yVelocity] to (0)
set [onGround] to (1)
end
The greenhouse floor is still located at approximately:
y = -50
If Byte falls beneath that position, Scratch moves him back to the floor and stops his vertical movement.
We also set:
onGround = 1
because Byte is once again standing on a surface where he is allowed to jump.
What Happens If Byte Walks Off a Platform?
There is another situation we need to handle.
Suppose Byte lands on the elevated platform.
Our collision code sets:
onGround = 1
Now imagine Byte simply walks off the edge instead of jumping.
If we don’t change onGround back to 0, Scratch still thinks Byte is standing on something. That could allow him to press Space and jump again while already falling.
We need to check whether the platform is still underneath him.
Create the checkPlatformEdge Block
Create another custom block:
checkPlatformEdge
Add:
define checkPlatformEdge
if <<(onGround) = (1)> and <(y position) > (-50)>> then
change y by (-1)
if <not <touching [Platform]?>> then
set [onGround] to (0)
end
change y by (1)
end
This block only runs the platform-edge test when Byte is above the main greenhouse floor and currently marked as standing on something.
Scratch temporarily moves Byte downward by one pixel.
Then it asks:
touching [Platform]?
If the answer is no, there is no longer a platform directly underneath him.
Scratch sets:
onGround = 0
and Byte begins behaving like an airborne character again.
Finally, we move him back up one pixel.
This tiny movement is essentially a quick test:
Is there still a platform underneath Byte’s feet?

Why Check y Position Greater Than -50?
We don’t need to perform this test when Byte is standing on the main greenhouse floor.
That surface already has its own floorCollision logic.
So:
y position > -50
helps limit this check to elevated platforms.

Test the Platform Collision System
Testing Scratch platform collision detection from several directions helps us catch problems that aren’t obvious when Byte simply jumps straight onto the middle of the platform.
Once all of the blocks are connected, try several different situations.
First, jump straight onto the platform.
Byte should land on top instead of falling through it.
Next, hold the left or right arrow while jumping.
Byte should continue moving horizontally while gravity controls his vertical motion.
Jump again while standing on the platform.
Because landOnPlatform sets onGround to 1, Byte should be able to immediately start another jump.
Then walk directly off the edge without pressing Space.
Byte should begin falling, and pressing Space while he is already airborne should not create another jump.
Finally, try approaching the platform from underneath.
Because we only resolve platform collisions while yVelocity < 0, jumping upward toward the platform should not behave the same way as landing on it from above.
See Byte’s Platform Collision Code in Action
Watch what happens as Byte lands on the red platform, moves across it, jumps again, and walks off the edge.
His movement, gravity, collision detection, and onGround state are all working together to create the behavior.
Debugging Scratch Platform Collisions
If Byte falls through the platform, first confirm that your Platform sprite is actually touching his costume as he falls.
A very thin platform may also be easier for a fast-moving sprite to pass through, so making the line slightly thicker can help.
If Byte becomes stuck inside the platform, check the landOnPlatform block.
You should have:
repeat until <not <touching [Platform]?>>
with:
change y by (1)
inside the loop.
If Byte can jump in midair after walking off the platform, check checkPlatformEdge.
The important line is:
set [onGround] to (0)
when Byte is no longer touching the Platform after the one-pixel test.
If Byte cannot jump after landing, make sure landOnPlatform ends with:
set [onGround] to (1)
Why Byte’s Hand Can Sometimes Count as a Collision
Scratch’s touching sensing block checks the visible pixels of the entire sprite costume.
That means Byte’s hand or another part of his body may occasionally touch the Platform sprite before his feet do.
For Garden Glitch Part 3, we are intentionally keeping the collision system simple.
Professional and more advanced games often use separate hitboxes, invisible collision shapes, or sensors near a character’s feet. Those techniques give programmers more precise control over which parts of a character can trigger collisions.
That would be an excellent upgrade later, but it isn’t necessary to understand the core platform collision concepts we are learning here.
Why Custom Blocks Matter
The custom blocks in this project aren’t just about making the Scratch editor look cleaner.
Each block now has a specific responsibility:
movement
gravity
platformCollision
floorCollision
checkPlatformEdge
landOnPlatform
That makes debugging easier.
If Byte isn’t moving correctly, we know to inspect movement.
If he isn’t falling correctly, we investigate gravity.
If he falls through a platform, we look at platformCollision or landOnPlatform.
This is one of the reasons programmers divide larger systems into smaller reusable pieces.

What We’ve Built So Far
Garden Glitch now has a surprisingly capable player controller.
Part 1 gave Byte horizontal movement and introduced x-coordinates.
Part 2 added jumping, y-coordinates, velocity, and gravity.
Part 3 added platform collision detection, collision resolution, ground-state tracking, edge detection, and code organization with custom blocks.
We’re no longer just moving a Scratch sprite around a Stage.
We are beginning to build the systems that make an actual platform game work.
Try This Scratch Coding Challenge
Before moving to the next tutorial, experiment with the Platform sprite.
Try making it wider or narrower.
Move it higher or lower in the greenhouse and see whether Byte can still reach it.
You could even duplicate the Platform sprite and experiment with several ledges, although our current collision system is intentionally designed around the single-platform lesson.
Pay attention to how changing the world affects the code.
That kind of experimentation is one of the best ways to learn how game mechanics work.
What’s Next in Garden Glitch?
Now Byte can move, jump, fall, and land on platforms.
That means we’re ready to start giving him something to find.
In Garden Glitch Part 4, we’ll add collectibles and a score system so Byte can move through the greenhouse collecting items while Scratch keeps track of the player’s progress.
That introduces another major game-development idea: using variables to represent game state.
You’ll Also Like
Garden Glitch Part 1: Scratch Player Movement with Arrow Keys

Start the series by coding Byte’s left and right movement using keyboard input, x-coordinates, and sprite direction.
Garden Glitch Part 2: How to Make a Sprite Jump in Scratch with Gravity

Build Byte’s jump using the space bar, yVelocity, y-coordinates, gravity, and floor detection.
Scratch Chase Game: Make a Cat Chase a Mouse

Practice sensing, distance, variables, custom blocks, random movement, lives, and game logic in another complete Scratch game.

Meet Toni, the Maker Mom behind Our Family Code
Hey there, I’m Toni! I’m a software engineer and Maker Mom that finds my joy in unleashing my children’s curiosity by exploring STEAM concepts with my fantastic five!
When I’m not chasing toddlers or raising tweens, you can find me tearing things up and putting them back together over here at Our Family Code.
I am the owner and content creator of multiple educational websites designed to increase access to STEAM & STEM education with a focus on teaching computer science and coding to kids of all ages!













