• 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 I want some help with my movement system.

AceKiron

Member
With my current movement system the player can move in both X and Y axis at a time. But I want it to either move on the X or the Y axis, not both at the same time. I already added 2 variables to indicate if the player is moving and in what axis (X = 0, Y = 1). I can't seem to figure it out how to do it, so can anyone help me please?

Code:
script_get_input();

if (run_key) {
    var spd = run_speed;
} else {
    var spd = walk_speed;
}

var moveX = (right_key - left_key) * spd;
var moveY = (down_key - up_key) * spd;

if (moveX == 0 && moveY == 0) {
    moving = false;
} else {
    if (moveX != 0) {
        if (place_meeting(x + moveX, y, object_collidable)) {
            repeat(abs(moveX)) {
                if (!place_meeting(x + sign(moveX), y, object_collidable)) {
                    x += sign(moveX);
                } else {
                    break;
                }
            }
            moveX = 0;
        }
        moving_axis = 0;
        moving = true;
    }

    if (moveY != 0) {
        if (place_meeting(x, y + moveY, object_collidable)) {
            repeat(abs(moveY)) {
                if (!place_meeting(x, y + sign(moveY), object_collidable)) {
                    y += sign(moveY);
                } else {
                    break;
                }
            }
            moveY = 0;
        }
        moving_axis = 1;
        moving = true;
    }
}

x += moveX;
y += moveY;
I don't have PayPal or anything so the only thing I can pay with is putting the name of the user(s) that help me in the final credits.
 
J

jimi

Guest
movey = (input_down - input_up) * spd;
if(movey == 0) { movex = (input_right - input_left) * spd; }

I hope this helps
 

AceKiron

Member
movey = (input_down - input_up) * spd;
if(movey == 0) { movex = (input_right - input_left) * spd; }

I hope this helps
that's not exactly what I was looking for :/ I want it to move in 1 axis and then can't move in the other until the player no longer moves in the first.
I got something figured out myself but it doesn't feel right.

Code:
script_get_input();

if (run_key) {
    var spd = run_speed;
} else {
    var spd = walk_speed;
}

var moveX = (right_key - left_key) * spd;
var moveY = (down_key - up_key) * spd;

if (moveX == 0 && moveY == 0) {
    moving = false;
} else {
    if (moving_axis == 1 && moving) { // Y-Axis
        moveX = 0;
    }
    else if (moving_axis == 0 && moving) { // X-Axis
        moveY = 0;
    }
    
    if (moveX != 0) {
        if (place_meeting(x + moveX, y, object_collidable)) {
            repeat(abs(moveX)) {
                if (!place_meeting(x + sign(moveX), y, object_collidable)) {
                    x += sign(moveX);
                } else {
                    break;
                }
            }
            moveX = 0;
        }
        moving_axis = 0;
        moving = true;
    }
    else if (moveY != 0) {
        if (place_meeting(x, y + moveY, object_collidable)) {
            repeat(abs(moveY)) {
                if (!place_meeting(x, y + sign(moveY), object_collidable)) {
                    y += sign(moveY);
                } else {
                    break;
                }
            }
            moveY = 0;
        }
        moving_axis = 1;
        moving = true;
    }
    else {
        moving_axis = 2; // I added another "axis" for if the player stands still.
    }
}

x += moveX;
y += moveY;
 
Add an extra variable and check for it in step.

Code:
Create
moveDIR='none'
Code:
Step
script_get_input();

if (run_key) {
    var spd = run_speed;
} else {
    var spd = walk_speed;
}

var moveX = (right_key - left_key) * spd;
var moveY = (down_key - up_key) * spd;

if (moveX == 0 && moveY == 0) {
    moving = false
} else {
    if (moveX != 0 && moveDIR!='vertical') {
        moveDIR='horizontal'
        if (place_meeting(x + moveX, y, object_collidable)) {
            repeat(abs(moveX)) {
                if (!place_meeting(x + sign(moveX), y, object_collidable)) {
                    x += sign(moveX);
                } else {
                    break;
                }
            }
            moveX = 0;
        }
        moving_axis = 0;
        moving = true;
    }
    else if (moveX==0){
        moveDIR='none'
    }

    if (moveY != 0 && moveDIR!='horizontal') {
        moveDIR='vertical'
        if (place_meeting(x, y + moveY, object_collidable)) {
            repeat(abs(moveY)) {
                if (!place_meeting(x, y + sign(moveY), object_collidable)) {
                    y += sign(moveY);
                } else {
                    break;
                }
            }
            moveY = 0;
        }
        moving_axis = 1;
        moving = true;
    }
    else if (moveY==0){
        moveDIR='none'
    }
}

x += moveX;
y += moveY;
Edited code after realizing there would be a problem. Which there still may be with no testing myself.
 
Last edited:

AceKiron

Member
@DarkFireDemon the moveDIR is about the same as moving_axis, but this code should do the trick though.

EDIT: It didn't :/ I can move in both X and Y axis at the same time and even glitch through collision boxes.
 

Rob

Member
Why don't you try an "else" statement if you want one or the other.

Either the player is moving horizontally (which will prevent the vertical code running) else check to see if the vertical code can run
 
Top