GML Player gets stuck in wall

M

MrSanfrinsisco

Guest
I've been spending the last while trying to create a wall that works fine, I've watched 2 or 3 youtube tutorials but all are giving me the same result regardless; my player gets stuck in the wall and I cannot get out. I want to know what the best way as to go about creating a wall that works fine. I'm not looking for anything extremely complex as I'm still new to this but something that is simple and works without very much problem. Here is my current code:
Code:
//Keyboard Variabls
moveUp = keyboard_check(ord("W"));
moveLeft = keyboard_check(ord("A"));
moveDown = keyboard_check(ord("S"));
moveRight = keyboard_check(ord("D"));

//Player Move Set
var moveLR = moveRight - moveLeft;
var moveUD = moveDown - moveUp;

//Variabls to check Vertical / Horizontal speed
playerHorizontal = moveLR * playerMoveSpeed;
playerVertical = moveUD * playerMoveSpeed;

//Player Movement
if (playerHorizontal < 0 || playerHorizontal > 0) || (playerVertical < 0 || playerVertical > 0) {
    sprite_index = spr_player_running;
    if (playerHorizontal < 0) {
        if (place_meeting(x+playerHorizontal, y, obj_wallLR)) {
            playerHorizontal = 0;
        } else {
            x -= playerMoveSpeed;
            image_angle = -90
        }
    }
    if (playerHorizontal > 0) {
        if (place_meeting(x+playerHorizontal, y, obj_wallLR)) {
            playerHorizontal = 0;
        } else {
            x += playerMoveSpeed;
            image_angle = 90;
        }
    }
    if (playerVertical < 0) {
        if (place_meeting(x, y+playerVertical, obj_wallTB)) {
            playerVertical = 0;
        } else {
            y -= playerMoveSpeed;
            image_angle = 180;
        }
    }
    if (playerVertical > 0) {
        if (place_meeting(x, y+playerVertical, obj_wallTB)) {
            playerVertical = 0;
        } else {
            y += playerMoveSpeed
            image_angle = 0;
        }
    }
} else {
    sprite_index = spr_player;
}
 
Do the sprites have different collision masks? The reason I ask is because if you are changing image_angle and/or sprite_index, you are also changing the collision mask, so part of it could be jumping into the wall.

If that's the problem, you could use just one sprite as the collision mask, like a circle about the size of the player.
 
M

MrSanfrinsisco

Guest
Do the sprites have different collision masks? The reason I ask is because if you are changing image_angle and/or sprite_index, you are also changing the collision mask, so part of it could be jumping into the wall.

If that's the problem, you could use just one sprite as the collision mask, like a circle about the size of the player.
the sprites do not have different collision masks. The other sprites are simply duplicates just edited a little bit
 

TheouAegis

Member
Code:
//Player Movement
if (playerHorizontal < 0 || playerHorizontal > 0) || (playerVertical < 0 || playerVertical > 0) {
   sprite_index = spr_player_running;
   if (playerHorizontal < 0) {
       if (place_meeting(x+playerHorizontal, y, obj_wallLR)) {
           playerHorizontal = 0;
       } else {
           x -= playerMoveSpeed;
           image_angle = -90
       }
   }
   if (playerHorizontal > 0) {
       if (place_meeting(x+playerHorizontal, y, obj_wallLR)) {
           playerHorizontal = 0;
       } else {
           x += playerMoveSpeed;
           image_angle = 90;
       }
   }
You can simplify that tremendously by not using playerMoveSpeed in that code. It's already factored into playerHorizontal... Same with the vertical.

This line:

if (playerHorizontal < 0 || playerHorizontal > 0) || (playerVertical < 0 || playerVertical > 0) {

could become
Code:
if playerHorizontal | playerVertical != 0 {
But other than those kinds of simplifications, I didn't notice anything really. You're rotating by 90, so that shouldn't be an issue.

....Other than the fact that you're using two types of walls for different collisions. But that'd just be a matter of room design: If you put an obj_WallLR above the player, then there's nothing stopping the player from walking up into it.
 
Top