Skip to content

Fixing the 0.2/-0.2 X velocity sliding bug in platformer games

💡 Having trouble with platformer physics and movement bugs? 🚀 Get Help Now

PM

PlatformMaster_Dev

Posted on January 22, 2024 • Intermediate

🐛 Dreaded 0.2/-0.2 X velocity sliding bug in my platformer!

Hey everyone! I’m working on a platformer with acceleration and deceleration mechanics. The player can’t move while jumping until they land, but I’m facing the infamous 0.2/-0.2 velocity bug! 😱

The problem: Sometimes when I stop moving, the X velocity shows -0.2 instead of 0, causing my character to slowly drift to the left. It’s super annoying!

I’m also planning to add:

  • Sliding mechanics (lying down while moving to pass under low gaps)
  • Wall climbing/clambering system
  • Slide jumps with timing-based height bonuses

Any help fixing this velocity bug would be amazing! 🙏

PE

PhysicsExpert_Sam

Replied 3 hours later • ⭐ Best Answer

I know exactly what you’re dealing with @PlatformMaster_Dev! This is a classic floating-point precision issue. Here’s the complete solution:

🔧 Root Cause Analysis

The problem occurs because your deceleration logic doesn’t account for when velocity should be exactly zero. The 0.2/-0.2 values get stuck because your conditions don’t handle the edge case properly.

✅ Complete Fix

Replace your movement logic with this improved version:

    when flag clicked
go to x: [0] y: [0]
set [X Velocity v] to [0]
set [Y Velocity v] to [0]
set [On Ground v] to [0]
forever
// Input handling
if <(Yield Controls) = [0]> then
if <key [d v] pressed?> then
change [X Velocity v] by [0.3]
end
if <key [a v] pressed?> then
change [X Velocity v] by [-0.3]
end
end

// Improved deceleration with threshold
if <not <<key [a v] pressed?> or <key [d v] pressed?>>> then
if <([abs v] of (X Velocity)) < [0.1]> then
set [X Velocity v] to [0]
else
set [X Velocity v] to ((X Velocity) * [0.85])
end
end

// Velocity clamping
if <(X Velocity) > [5]> then
set [X Velocity v] to [5]
end
if <(X Velocity) < [-5]> then
set [X Velocity v] to [-5]
end
end
  

🎮 Advanced Movement Features

Now let’s add those cool mechanics you wanted:

Sliding System:

    // Sliding mechanics
when flag clicked
set [Is Sliding v] to [0]
forever
if <<key [s v] pressed?> and <(On Ground) = [1]> and <([abs v] of (X Velocity)) > [2]>> then
set [Is Sliding v] to [1]
set [Y Velocity v] to [0]
change y by [-10] // Lower hitbox
else
if <(Is Sliding) = [1]> then
set [Is Sliding v] to [0]
change y by [10] // Restore hitbox
end
end

// Slide physics
if <(Is Sliding) = [1]> then
set [X Velocity v] to ((X Velocity) * [0.95]) // Gradual slowdown
if <([abs v] of (X Velocity)) < [0.5]> then
set [Is Sliding v] to [0]
change y by [10]
end
end
end
  

Wall Climbing System:

    // Wall climbing/clambering
when flag clicked
set [Climbing v] to [0]
forever
// Check for wall grab
if <<key [w v] pressed?> and <touching color [#8B4513]?> and <(Y Velocity) < [0]>> then
if <not <(Climbing) = [1]>> then
set [Climbing v] to [1]
set [Y Velocity v] to [0]
set [X Velocity v] to [0]
end
end

// Climbing controls
if <(Climbing) = [1]> then
if <key [w v] pressed?> then
change [Y Velocity v] by [0.2]
end
if <key [s v] pressed?> then
change [Y Velocity v] by [-0.2]
end

// Release from wall
if <<key [space v] pressed?> or <not <touching color [#8B4513]?>>> then
set [Climbing v] to [0]
if <key [space v] pressed?> then
set [Y Velocity v] to [8] // Jump off wall
if <key [a v] pressed?> then
set [X Velocity v] to [-3]
end
if <key [d v] pressed?> then
set [X Velocity v] to [3]
end
end
end
end
end
  

Slide Jump with Timing Bonus:

    // Slide jump mechanics
when flag clicked
set [Slide Jump Timer v] to [0]
forever
// Track slide jump window
if <(Is Sliding) = [1]> then
set [Slide Jump Timer v] to [10] // 10 frame window
else
if <(Slide Jump Timer) > [0]> then
change [Slide Jump Timer v] by [-1]
end
end

// Enhanced jump from slide
if <key [space v] pressed?> then
if <(Slide Jump Timer) > [0]> then
set [Y Velocity v] to [12] // Higher jump
set [Slide Jump Timer v] to [0]
broadcast [slide jump effect v] // Visual effect
else
if <(On Ground) = [1]> then
set [Y Velocity v] to [8] // Normal jump
end
end
end
end
  

🚀 Pro Tips

  • Use multiplication for deceleration instead of addition/subtraction - it’s more natural
  • Always include threshold checks to prevent micro-movements
  • Clamp your velocities to prevent unrealistic speeds
  • Test edge cases like rapid key presses and direction changes

This should completely eliminate your sliding bug and give you those awesome advanced mechanics! 🎯

PM

PlatformMaster_Dev

Replied 45 minutes later

@PhysicsExpert_Sam This is absolutely incredible! 🤩 The sliding bug is completely gone and the movement feels so much smoother!

The sliding and wall climbing mechanics work perfectly. One question - how can I add a visual effect when the player does a successful slide jump?

VE

VisualEffects_Pro

Replied 1 hour later

@PlatformMaster_Dev Great question! Here’s how to add awesome slide jump effects:

    // Visual effects for slide jump
when I receive [slide jump effect v]
repeat [8]
create clone of [particle v]
wait [0.05] seconds
end

// Particle sprite code
when I start as a clone
go to [Player v]
point in direction (pick random [-45] to [45])
set [speed v] to (pick random [3] to [7])
set [gravity v] to [-0.3]
set size to [50]%
set [ghost v] effect to [0]
repeat [20]
move (speed) steps
change [gravity v] by [-0.1]
change y by (gravity)
change [ghost v] effect by [5]
end
delete this clone
  

This creates a burst of particles when you slide jump! You can also add screen shake, sound effects, and color changes for even more impact! ✨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Platformer Development!

Excellent problem-solving everyone! For those looking to create even more sophisticated platformer mechanics, our community can help you implement:

  • 🎯 Precise collision detection systems
  • 🌊 Advanced physics simulations
  • ⚡ Performance optimization techniques
  • 🎨 Professional visual effects

📚 Related Topics

Ready to build the next great platformer? Get personalized guidance from our expert game development tutors!