P
Potatoverse
Guest
Hi there
So I want to have 4-directional movement but still allow for diagonal movement. It's just basically the movement Undertale has but I just don't know how to make it work properly.
what I have now is:
This kind of works but it's a little wonky. For example: If you press left and right and then press up the player won't go up but if you press left and right and then press down the player will go down. Also because you don't move upwards like that the sprite doesn't change if you stop pressing either left or right so it looks like the player is doing a moonwalk. This also goes for pressing up and down and then pressing left.
I'm doing all of this so weirdly because I don't want the direction the player is looking in to be biased. In other words I don't want the player's sprite to prioritise looking in a certain direction because of the order it checks the direction in.
Please help I've been working on this simple movement for way too long and I'm starting to feel like I actually don't have a brain and I'm living on pure willpower. Thanks in advance!
So I want to have 4-directional movement but still allow for diagonal movement. It's just basically the movement Undertale has but I just don't know how to make it work properly.
what I have now is:
GML:
/// Create Event
plyrspeed = 2;
// I need to work with directions like this because I have something else that needs the players direction
enum dir {
right,
left,
up,
down
}
/// Step Event
// INITIAL SPRITE SETTING
// The variable "direc" is fed to the player by a different object when it's created
switch(direc) {
case dir.right: sprite_index = spr_plyrright; break;
case dir.up: sprite_index = spr_plyrup; break;
case dir.left: sprite_index = spr_plyrleft; break;
case dir.down: sprite_index = spr_plyrdown; break;
}
// INPUT
moveX = (keyboard_check(vk_right) - keyboard_check(vk_left)) * plyrspeed;
moveY = (keyboard_check(vk_down) - keyboard_check(vk_up)) * plyrspeed;
// COLLISION DETECTION
if(moveX != 0) {
if(place_meeting(x + moveX, y, obj_collision)) {
repeat(abs(moveX)) {
if(!place_meeting(x + sign(moveX), y, obj_collision)) { x += sign(moveX);}
else { break;}
}
moveX = 0;
}
}
if(moveY != 0) {
if(place_meeting(x, y + moveY, obj_collision)) {
repeat(abs(moveY)) {
if(!place_meeting(x, y + sign(moveY), obj_collision)) { y += sign(moveY);}
else { break;}
}
moveY = 0;
}
}
// SPRITE CHANGING
if(moveX != 0 or moveY != 0) {
image_speed = 1;
if(moveX == 0) {
if(moveY > 0) {
direc = dir.down;
}
if(moveY < 0) {
direc = dir.up;
}
}
if(moveY == 0) {
if(moveX > 0) {
direc = dir.right;
}
if(moveX < 0) {
direc = dir.left;
}
}
}
else {
image_speed = 0;
image_index = 0;
}
// ACTUAL MOVEMENT
x += moveX;
y += moveY;
I'm doing all of this so weirdly because I don't want the direction the player is looking in to be biased. In other words I don't want the player's sprite to prioritise looking in a certain direction because of the order it checks the direction in.
Please help I've been working on this simple movement for way too long and I'm starting to feel like I actually don't have a brain and I'm living on pure willpower. Thanks in advance!