Jump not working right and run speed gets progressively faster

The controls respond but the output is not right. You can see that it tries to jump but never leaves the platform. When I run, the speed does not cap, so it gets increasingly faster. Any ways to go about this?

Create Event

/// @description Init Vars
// Global array to store gamepad info in
global.gp_index = [0];

// Get the number of available gamepad devices / slots
var gp_num = gamepad_get_device_count();

// Iterate through connected devices and store their index
var pos = 0; // Helper variable to keep the global array in order

for (var i = 0; i < gp_num; i++)
{
if (gamepad_is_connected(i))
{
global.gp_index[pos] = i;
pos++;
}
}

jumps = 0;
jumps_max = 2;

onGround = (place_meeting(x, y + 1, o_block));

// Player Vars
move_rate = 6;
move_rate_multi = 0.15;
jump_rate = 4;
jump_buffer_count = 0;
jump_buffer = 10;
jump_ledge_buffer = 10;
accel_rate_ground = 0.3;
accel_rate_air = 0.2;
brake_rate_ground = 0.4;
brake_rate_air = 0.2;
gravity_vspeed = 20;
gravity_rate = 1;


Step Event

var gp = global.gp_index[0]; // Where 0 is whatever the player selected via an options menu
/// @description Player Controls
check_left_key = keyboard_check(vk_left);
check_right_key = keyboard_check(vk_right);
check_jump_key = keyboard_check_pressed(vk_space);
check_run_key = keyboard_check(vk_control);

check_left_alt = keyboard_check_pressed(ord("A"));
check_right_alt = keyboard_check_pressed(ord("D"));

check_left_stick = gamepad_axis_value(0, gp_axislh);
check_jump_button = gamepad_button_check_pressed(0, gp_face1);
check_run_button = gamepad_button_check(0, gp_face3);

// Tells what to do if gamepad is or isn't connected
if (gamepad_is_connected(0))
{
//To account for "drift"
gamepad_set_axis_deadzone(0, 0.7);
//To allow for trigger sensetivity
gamepad_set_button_threshold(0, 0.1);
// Gamepad checks
check_left_stick = gamepad_axis_value(0, gp_axislh);
check_jump_button = gamepad_button_check_pressed(0, gp_face1);
check_run_button = gamepad_button_check(0, gp_face3);

hspeed = move_rate * (check_left_stick);
vspeed = jump_rate;

}

//If no gamepad connected, use keyboard input
else
{
//Keyboard checks
// Checks return 1 or 0
check_left_key = keyboard_check(vk_left);
check_right_key = keyboard_check(vk_right);
check_jump_key = keyboard_check_pressed(vk_space);
check_run_key = keyboard_check(vk_control);

hspeed = move_rate * (check_right_key - check_left_key);
vspeed = jump_rate;
}


// Get input
move_input_total = 0;
if keyboard_check(check_left_key) and keyboard_check(check_left_alt) and gamepad_button_check_pressed(gp, gp_axislh) {
move_input_total -= 1;
}
if keyboard_check(check_right_key) and keyboard_check(check_right_alt) and gamepad_button_check_pressed(gp, gp_axislh) {
move_input_total += 1;
}
// Jump input buffer
if keyboard_check_pressed(check_jump_key) and gamepad_button_check_pressed(gp, gp_face1) {
jump_buffer_count = 0;
}
// Check / increment jump buffer
if jump_buffer_count < jump_buffer
{
jump_buffer_count++;
}

// Run if holding down CTRL key or B button
if (check_run_button == 1) or (check_run_key == 1)
{
move_rate += move_rate_multi;
}
else{
move_rate = 4;
}


// No move input, brake
// On ground
if place_meeting(x, y + 1, o_block)
{
if move_input_total == 0 || (hspeed * move_input_total < 0)
{
hspeed -= hspeed * brake_rate_ground;
}
}
// In air
if !place_meeting(x, y + 1, o_block)
{
if move_input_total == 0 || (hspeed * move_input_total < 0)
{
hspeed -= hspeed * brake_rate_air;
}
}

// Move player and clamp value to max
// On ground
if place_meeting(x, y, o_block)
{
hspeed += move_input_total * accel_rate_ground;
}
// In air
if !place_meeting(x, y, o_block)
{
hspeed += move_input_total * accel_rate_air;
}
// Limit speed to move_rate
hspeed = clamp(hspeed, -move_rate, move_rate);

// Gravity
if (vspeed < gravity_vspeed) || !place_meeting(x, y, o_block)
{
vspeed += gravity_rate;
}

// Jump if on / close to ground
// Account for ledge buffer and allow wall jumping and double jump
if onGround
{
vspeed = jump_rate;
jumps = jumps_max;
}

if ((keyboard_check_pressed(vk_space)) and (jumps > 0)) or ((gamepad_button_check_pressed(gp, gp_face1)) and (jumps > 0))
{
vspeed = -jump_rate;
jumps -= 1;
}

// Run if holding down CTRL key or B button
if (check_run_key == 1) or (check_run_button == 1)
{
move_rate += move_rate_multi;
}
else{
move_rate = 4;
}

// Collisions and stuck/overlap prevention
if (place_meeting(x + hspeed, y, o_block)) {
while (!place_meeting(x + sign(hspeed), y, o_block)) {
x += sign(hspeed);
}
hspeed = 0;
}
if (place_meeting(x, y + vspeed, o_block)) {
while (!place_meeting(x, y + sign(vspeed), o_block)) {
y += sign(vspeed);
}
vspeed = 0;
}
// Diagonal
if (place_meeting(x + hspeed, y + vspeed, o_block)) {
while (!place_meeting(x + sign(hspeed), y + sign(vspeed), o_block)) {
x += sign(hspeed);
y += sign(vspeed);
}
hspeed = 0;
vspeed = 0;
}


// Speed debug
show_debug_message("HInput: " + string(move_input_total));
show_debug_message("Jump Buffer Count: " + string(jump_buffer_count));
show_debug_message("HSpeed: " + string(hspeed));
show_debug_message("VSpeed: " + string(hspeed));
 

Nidoking

Member
Wow, that's... a whole lot of things you want to do sometimes, but you're doing them all the time. A CODE block would really have helped with the indentation, but another thing that would help would be to put the checks inside other checks where they belong.

And you really don't need to check every input twice per step. Once is plenty.
 
Top