script

The Most Simple Movement Code

Very simple collision code.
Yosi
Lv. 46
Yosi Level 46
· 2 min read · 5551 views

The Code:


repeat(abs(hsp)) {
    if (!place_meeting(x + sign(hsp), y, obj_block)) {
        x += sign(hsp);
    } else {
        hsp = 0;
        break;
    }
}

repeat(abs(vsp)) {
    if (!place_meeting(x, y + sign(vsp), obj_block)) {
        y += sign(vsp);
    } else {
        vsp = 0;
        break;
    }
}

Notes:

  • You need to initialize the variables hsp and vsp and have an object named obj_block
  • Obviously this isn't as optimized as other solutions, but that shouldn't be an issue for most games
  • This version won't cause instances to teleport through walls when moving at extremely high speeds
  • This is designed to be pixel perfect, so an hsp of 2.4 will move as far as an hsp of 2.0

From Idea to Game in 48 Hours

Join thousands of GameMaker developers who have discovered the joy of game jams.

No experience? No problem. GameMaker developers of all skill levels and backgrounds use the gm(48) game jam to create games, learn new skills, and connect with the community.

Ready to start your game development journey? Create an account and join the next gm(48) game jam!