I'm attempting to make a platformer game. So far, I have the horizontal movement where I would like it to be; however... I'm having trouble implementing code that properly detects collision. When I move my box, it will stop ever-so briefly before sidling up against the edge. Additionally, occasionally when I land after jumping or falling, I will be stuck in the floor. Jumping allows me to escape, but it will be a full jump and cause my character to simply be plunged into the floor a few pixels again. What am I doing incorrectly here?
Let it be known that I deleted the original code for the vertical movement in an attempt to rewrite it correctly, to no avail. So while the original code doesn't exist, I still have problems with it so it doesn't matter. If you'd like to test it out yourself, you can just comment out the vertical movement for now.
GML:
/// @description Things that occur each frame (step)
key_left = keyboard_check(ord("A")); //Detect "left" key pushed
key_right = keyboard_check(ord("D")); //Detect "right" key pushed
key_crouch = keyboard_check(ord("S")); //Detect "down/crouch" key pushed
key_jump = keyboard_check_pressed(vk_space); //Detect "jump" key pushed
player_facing = key_right - key_left; //Gives 1 for right, 0 for none or both, -1 for left
player_accelerate = player_facing*h_accel; //Gives -accel to left and +accel to right
/* Horizontal Movement */
if (key_right || key_left) { //If either are pushed...
if (true_speed < walk_speed || true_speed > (walk_speed*-1)) { //If true speed is less than cap...
true_speed = true_speed + player_accelerate; //Player speed (x movement) will be accelerated appropriately
if (true_speed > walk_speed || true_speed < (walk_speed*-1)) { //If player speed is greater or less than max walk speed...
true_speed = walk_speed*sign(player_facing); //Player speed is set evenly to the max walk speed
}
}
}
if (player_facing == 0 && true_speed != 0) { //If both or none input while player speed is not 0...
true_speed = true_speed + (-1*(sign(true_speed))*h_accel); //Player speed accel/decelerates until 0
if ((true_speed > 0 && true_speed < h_accel) || (true_speed < 0 && true_speed > h_accel)) { //If player speed is greater or less than zero but deceleration over/undershoots...
true_speed = 0; //Set player speed evenly to 0
}
}
if (!place_meeting(x+true_speed, y, obj_solid)) {
x = x + true_speed;
} else {
true_speed = 0;
}
/* Vertical Movement */
if (key_jump && place_meeting(x, y+1, obj_solid)) {
true_fall = true_fall + jump_height;
}
if (place_meeting(x, y+1, obj_solid)) {
true_fall = 0;
} else {
true_fall = true_fall + grav_accel;
}
y = y + true_fall;
/* Reset Room Key */
if (keyboard_check(ord("R"))) {
room_restart();
}