• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Getting stuck in walls when jumping (top-down game)

Zabicka

Member
Hello, I have an issue with my Player getting stuck in Walls, I don't know how to handle this issue. Does anybody have an idea how to solve it?

Here are some codes:

Collision Code Script
Code:
/// @desc collision(object)
/// @arg object

// Horizontal
if (place_meeting(x + hspd, y, argument0)) {
    while (!place_meeting(x + sign(hspd), y, argument0)) {
        x += sign(hspd);
    }
    hspd = 0;
}
x += hspd;

// Vertical
if (place_meeting(x, y + vspd, argument0)) {
    while (!place_meeting(x, y + sign(vspd), argument0)) {
        y += sign(vspd);
    }
    vspd = 0;
}
y += vspd;
Create Event (Player)
Code:
// Z axis
z = 0;

// Position
hspd = 0;
vspd = 0;
zspd = 0;

// Stats
run_speed  = 2;
walk_speed = 1;
move_speed = walk_speed;
jump_power = 3;
Step Event (Player)
Code:
// Key input
var hinput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
var vinput = keyboard_check(ord("S")) - keyboard_check(ord("W"));
var kshift = keyboard_check(vk_shift);
var kjump  = keyboard_check_pressed(vk_space);

// Movement
if (place_meeting(x + hinput, y, obj_wall)) { hinput = 0; }
if (place_meeting(x, y + vinput, obj_wall)) { vinput = 0; }

if (hinput != 0 || vinput != 0) {
    dir  = point_direction(0, 0, hinput, vinput);
    hspd = lengthdir_x(move_speed, dir);
    vspd = lengthdir_y(move_speed, dir);
 
    // Collision
    collision(obj_wall);
}
move_speed = kshift ? run_speed : walk_speed;

// Jump
if (z > 0) {
    zspd -= 0.2;
    z += zspd;
} else {
    z = 0;
    if (kjump) {
        zspd = jump_power;
        z = zspd;
    } else {
        zspd = 0;
    }
}

// Jumping over walls (Laggy)
if (z > 0) {
    mask_index = spr_empty;
} else if (z == 0) {
    mask_index = spr_player_mask;
}
Draw Event (Player)
Code:
// Draw self
draw_sprite_ext(sprite_index, image_index, x, y - z, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
I know what the issue is,
when the Player lands back to the ground he does have a collision again,
but I don't know how to solve it.
 

Bentley

Member
When you land, does your sprite change? When you land, and your sprite changes, your new sprite probably sticks in the ground, so you movement won't work. You can use a mask.
 

CloseRange

Member
a small tip: i'm pretty sure if you set the mask to -1 that will make it so no mask is applied, so no collision (i'm pretty sure)

also this solution depends on how you mean to go about it.
as the player of your game, what is to stop me from jumping and, while in the middle of the jump, going over a wall and just not moving out of the way?
What do you want to happen when, me, the malicious user, trys to break your game by jumping atop the walls?
Do you want there to be another level where the player can actual walk on top of the walls but not walk off without jumping?
Do you want the player to snap to the nearest free spot?
Do you want the game to take control and force forward movement?
 
Top