The Most Simple Movement Code
script

The Most Simple Movement Code

Very simple collision code.
Yosi
Yosi
· 2 min read · 64 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