How do I make a character slide in one direction until they hit a collision

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cashwarrior1

I’m just trying to make one of those sliding block puzzles but instead of pushing a block, you press a direction and then your character slides in that direction until it hits a wall. I’m new to coding and know nothing about the language so I rely on tutorials but there’s none for sliding block puzzles apparently. If you could respond with a code and explanation of the code, that’d be appreciated!

:bust_in_silhouette: Reply From: Sayteriously

move_and_collide can: move your player, stop your player when it collides with something, and return what was collided with.

example project files

Hope this helps. Have fun with Godot Engine :smiley:

:bust_in_silhouette: Reply From: JimArtificer

Broadly speaking, you have two options.

  1. Rely on physics. Create your blocks with collision shapes. Use TileMap to help with level creation and set certain types of tiles as blocking. When the player moves into a block, apply a force to it and then fine tune the details. (Magnitude, friction, etc.)
    Physics introduction — Godot Engine (stable) documentation in English

  2. Handle the game logic separately from the visual representation in a grid matrix of some type and use the engine to render visuals. This method will give you total control over the logic, but it may be a bigger challenge to get animations working correctly, especially if you plan to have pushing a block create a chain reaction.