GML Play can walk through walls when angled a certain way

M

MrSanfrinsisco

Guest
I have always struggled with creating walls and for that reason I try to create games that don't require them however that's bad habit and I think it's about time I get this resolved. I only know of one way to create walls and that's to check the place meeting of the player and the wall object and change their movement speed to to counteract them from moving through the wall. The problem I get is if a player touches a wall they can walk but they glitch out, some parts of the wall don't allow the player to move against it, some parts do, and sometimes.. The player can faze through the wall and just walk right through it... Here's the code
Code:
//Wall Collisions
if (place_meeting(x-1, y, obj_wall)) x += playerMoveSpeed;
if (place_meeting(x+1, y, obj_wall)) x -= playerMoveSpeed;
if (place_meeting(x, y-1, obj_wall)) y += playerMoveSpeed;
if (place_meeting(x, y+1, obj_wall)) y -= playerMoveSpeed;

if (place_meeting(x-1, y-1, obj_wall)) {
    x += playerMoveSpeed;
    y += playerMoveSpeed;
}
if (place_meeting(x+1, y+1, obj_wall)) {
    x -= playerMoveSpeed;
    y -= playerMoveSpeed;
}
if (place_meeting(x+1, y-1, obj_wall)) {
    x -= playerMoveSpeed;
    y += playerMoveSpeed;
}
if (place_meeting(x-1, y+1, obj_wall)) {
    x += playerMoveSpeed;
    y -= playerMoveSpeed;
}
 

breakmt

Member
Try to do it this way:

1. Define 2 variables for player - moveDirectionX, moveDirectionY. Each can be -1, 0, 1 (you should update them in movement code, so -1 is left, 1 is right, 0 is no move)
2. Collision check:
Code:
var nextX = x + moveDirectionX * moveSpeed;
var nextY = y + moveDirectionY * moveSpeed;

if (!place_meeting(nextX, nextY, obj_wall)) {
    x = nextX;
    y = nextY;
}
 
Top