GameMaker Knockback Direction Broken?

P

_Proxy

Guest
I'm trying to knock back my player in the opposite direction he shoots his gun. But it only seems to be working on vertical directions...

PLAYER CREATE EVENT
Code:
hspd = 0;
vspd = 0;
hspd_force = 0;
vspd_force = 0;
force_applied = 0;
force_dir = undefined;
spd = 2;
PLAYER STEP EVENT
Code:
// Inputs
var down_key = keyboard_check(bind_down);
var right_key = keyboard_check(bind_right);
var left_key = keyboard_check(bind_left);
var up_key = keyboard_check(bind_up);
// Input Change
x_input = right_key - left_key;
y_input = down_key - up_key;

if (x_input != 0) or (y_input != 0)
{
    len = spd;
    if (hspd != 0) { image_xscale = sign(hspd); }
    sprite_index = spr_player_run;
}
else
{
    var len = 0;
    sprite_index = spr_player;   
}

// Force
if (force_applied > 0)
{
    hspd_force = lengthdir_x(force_applied, force_dir);
    vspd_force = lengthdir_y(force_applied, force_dir);
    force_applied *= 0.9;
}
// Movement
dir = point_direction(0, 0, x_input, y_input);
hspd = lengthdir_x(len, dir) + hspd_force;
vspd = lengthdir_y(len, dir) + vspd_force;
// Collisions
if (place_meeting(x + hspd, y, obj_collidable))
{
    while (!place_meeting(x + sign(hspd), y, obj_collidable))
    {
        x += sign(hspd);   
    }
    hspd = 0;
}
if (place_meeting(x, y + vspd, obj_collidable))
{
    while (!place_meeting(x, y + sign(vspd), obj_collidable))
    {
        y += sign(vspd);   
    }
    vspd = 0;   
}
// Application of movement
x += hspd;
y += vspd;
GUN STEP EVENT
Code:
var dir = point_direction(x, y, mouse_x, mouse_y);
// Origin
x = holder.x - lengthdir_x(current_recoil, dir);
y = (holder.y - 9) - lengthdir_y(current_recoil, dir);
// Rotation
image_angle = dir;
// Flipping
if (dir > 90) and (dir < 270) { image_yscale = -1; } else { image_yscale = 1; }

// Cooldown counter
if (cooldown_counter_start)
{
    cooldown_counter++;
}
// Shooting
if (mouse_check_button(bind_shoot)) && (ammo != 0)
{
    // Cooldown check
    cooldown_counter_start = true;
    if (cooldown_counter >= cooldown_time) or (first_shot)
    {
        // Audio
        audio_play_sound(snd_shoot, 10, 0);
        // Bullet
        var buffer_x = lengthdir_x(bullet_buffer, dir);
        var buffer_y = lengthdir_y(bullet_buffer, dir);
        var inst = instance_create_layer(x + buffer_x, y + buffer_y, "Instances", obj_bullet);
        inst.direction = dir;
        inst.direction += random_range(-bullet_spread, bullet_spread + 1);
        inst.image_angle = inst.direction;
        inst.speed = bullet_speed;
        
        // Ammo and recoil
        ammo--;
        current_recoil = recoil;
        
        // Variables
        cooldown_counter = 0;
        cooldown_counter_start = false;
        first_shot = false;
        
        with (obj_player)
        {
            var dir = inst.direction;
            force_dir = -dir;
            force_applied = 4;
        }
    }
}   

// Calculate Recoil
current_recoil = max(0, floor(current_recoil * 0.8));
 
P

_Proxy

Guest
SOLVED!
Thanks to a user on the PixelatedPope's Discord: @Sahaun#1172
all i had to do was make the horizontal direction inverse

Instead of
Code:
hspd = lengthdir_x(len, dir) + hspd_force;
vspd = lengthdir_y(len, dir) + vspd_force;
in the player's step event

I should have done:
Code:
hspd = lengthdir_x(len, dir) - hspd_force;
vspd = lengthdir_y(len, dir) + vspd_force;
 
Top