Aller au contenu

How do I make collisions in Scratch?

Ce contenu n’est pas encore disponible dans votre langue.

🎮 Need help with collision detection in your game? 🚀 Get Expert Help

WC

WinterCat18

Posted on July 27, 2025 • Beginner

🎯 Need help with collision detection

Hey everyone! I’m working on my first platformer game and I’m stuck on something that seems basic but I can’t figure out - how do I make collisions work properly? 😅

Specifically, I need my player character to collide with the ground and platforms so they don’t fall through them. I’ve tried a few things but nothing seems to work right.

Any help would be super appreciated! Thanks! 🙏

GP

GameDev_Pro

Replied 30 minutes later • ⭐ Best Answer

Great question @WinterCat18! Collision detection is fundamental to game development. Let me break down the most effective methods for different types of collisions! 🎮

🏗️ Understanding Collision Types

flowchart TD A[🎮 Collision Detection] --> B[Basic Collisions] A --> C[Advanced Collisions] B --> D[touching sprite?] B --> E[touching color?] B --> F[color touching color?] C --> G[Pixel-Perfect Detection] C --> H[Hitbox Systems] C --> I[Predictive Collision] D --> J[Simple & Fast] E --> K[Platform Games] F --> L[Precise Control] G --> M[Most Accurate] H --> N[Performance Optimized] I --> O[Smooth Movement] style A fill:#e1f5fe style B fill:#f3e5f5 style C fill:#e8f5e8 style J fill:#fff3e0 style K fill:#fff3e0 style L fill:#fff3e0 style M fill:#ffebee style N fill:#ffebee style O fill:#ffebee

🎯 Method 1: Basic Player-Ground Collision

Perfect for platformer games - here’s the most reliable approach:

    // Player Movement with Ground Collision
when green flag clicked
set [gravity v] to [0]
set [on ground v] to [false]
forever
// Handle horizontal movement
if <key [right arrow v] pressed?> then
change x by [5]
if <touching [Ground v]?> then
change x by [-5]  // Push back if hitting wall
end
end
if <key [left arrow v] pressed?> then
change x by [-5]
if <touching [Ground v]?> then
change x by [5]   // Push back if hitting wall
end
end

// Handle jumping
if <<key [space v] pressed?> and <(on ground) = [true]>> then
set [gravity v] to [15]  // Jump strength
end

// Apply gravity
change [gravity v] by [-1]  // Gravity acceleration
change y by (gravity)

// Ground collision check
if <touching [Ground v]?> then
repeat until <not <touching [Ground v]?>>
change y by [1]  // Move up until not touching
end
set [gravity v] to [0]
set [on ground v] to [true]
else
set [on ground v] to [false]
end
end
  

🎨 Method 2: Color-Based Collision

Great for complex level designs with multiple platform types:

    // Color Collision System
when green flag clicked
forever
// Move right
if <key [right arrow v] pressed?> then
change x by [3]
if <touching color [#8B4513]?> then  // Brown for ground
change x by [-3]
end
end

// Move left  
if <key [left arrow v] pressed?> then
change x by [-3]
if <touching color [#8B4513]?> then
change x by [3]
end
end

// Gravity and ground check
change y by [-2]  // Apply gravity
if <touching color [#8B4513]?> then
repeat until <not <touching color [#8B4513]?>>
change y by [1]
end
end

// Special platform colors
if <touching color [#FF0000]?> then  // Red for damage
broadcast [player hurt v]
end
if <touching color [#00FF00]?> then  // Green for bounce
set [y velocity v] to [20]
end
end
  

⚡ Method 3: Advanced Smooth Collision

For professional-feeling movement with variable speeds:

    // Smooth Movement System
when green flag clicked
set [x velocity v] to [0]
set [y velocity v] to [0]
forever
// Horizontal input
if <key [right arrow v] pressed?> then
change [x velocity v] by [0.5]
end
if <key [left arrow v] pressed?> then
change [x velocity v] by [-0.5]
end

// Apply friction
set [x velocity v] to ((x velocity) * [0.8])

// Move horizontally with collision
change x by (x velocity)
if <touching [Ground v]?> then
if <(x velocity) > [0]> then
repeat until <not <touching [Ground v]?>>
change x by [-1]
end
else
repeat until <not <touching [Ground v]?>>
change x by [1]
end
end
set [x velocity v] to [0]
end

// Vertical movement with gravity
change [y velocity v] by [-0.8]  // Gravity
change y by (y velocity)

if <touching [Ground v]?> then
if <(y velocity) < [0]> then  // Falling
repeat until <not <touching [Ground v]?>>
change y by [1]
end
set [y velocity v] to [0]
// Allow jumping
if <key [space v] pressed?> then
set [y velocity v] to [12]
end
else  // Moving up into ceiling
repeat until <not <touching [Ground v]?>>
change y by [-1]
end
set [y velocity v] to [0]
end
end
end
  

🔧 Pro Tips for Better Collisions

  • Use separate sprites: Create dedicated collision sprites (invisible) for complex shapes
  • Test collision before moving: Check if the next position is valid before moving
  • Handle corners properly: Use small incremental movements to avoid getting stuck
  • Optimize performance: Only check collisions when the sprite is actually moving

These methods will give you solid, reliable collision detection for any type of game! 🎉

WC

WinterCat18

Replied 1 hour later

@GameDev_Pro This is exactly what I needed! 🤩 The first method worked perfectly for my platformer!

I was trying to use just the “touching” block without the push-back logic, which is why my player kept getting stuck inside platforms. The gravity system you showed makes so much more sense now!

Quick question - how do I make the player stick to moving platforms?

GP

GameDev_Pro

Replied 20 minutes later

@WinterCat18 Great question about moving platforms! Here’s how to make the player stick to them:

    // Moving Platform Detection
when green flag clicked
forever
// Check if standing on a moving platform
change y by [-1]  // Move down slightly to test
if <touching [Moving Platform v]?> then
change y by [1]  // Move back up
// Move with the platform
change x by ([x velocity v] of [Moving Platform v])
change y by ([y velocity v] of [Moving Platform v])
else
change y by [1]  // Move back up
end
end
  

This checks if you’re standing on a moving platform and moves the player along with it! 🚀

CollisionMaster_Dev

Replied 2 hours later

Excellent explanations! 👏 Here are some additional collision techniques for specific scenarios:

🎯 Hitbox Collision (for fighting games)

    // Custom Hitbox System
define check hitbox collision
if <<(x position) > ((enemy x) - (enemy width))> and <(x position) < ((enemy x) + (enemy width))>> then
if <<(y position) > ((enemy y) - (enemy height))> and <(y position) < ((enemy y) + (enemy height))>> then
broadcast [collision detected v]
end
end
  

⚡ One-Way Platform Collision

    // One-Way Platforms (can jump through from below)
if <touching [One Way Platform v]?> then
if <(y velocity) < [0]> then  // Only collide when falling
if <(y position) > ([y position v] of [One Way Platform v])> then
repeat until <not <touching [One Way Platform v]?>>
change y by [1]
end
set [y velocity v] to [0]
end
end
end
  

These advanced techniques will take your games to the next level! 🎮

VB

Vibelf_Community

Pinned Message • Moderator

🎮 Master Advanced Game Physics & Collision Systems

Fantastic discussion on collision detection! For developers ready to implement even more sophisticated game mechanics, our community offers guidance on:

  • 🏃‍♂️ Advanced physics engines and realistic movement
  • 🎯 Precise hitbox systems for competitive games
  • ⚡ Performance optimization for complex collision detection
  • 🌍 3D collision detection and spatial partitioning

📚 Related Discussions

Ready to create professional-quality game mechanics? Connect with expert game developers in the Vibelf community!