GMS 2 [SOLVED] Hit Box only works when moving

Sarena

Member
I set up a sword weapon that will create a hit box that decreases the enemy's hp when colliding with it. Everything works correctly except the placement of my hit box is a little weird.

When my player is standing still, the hit box will appear on top of the player.
When my player is moving, the hit box will move outwards in the appropriate direction that the player is moving towards, which is where I want it to be.

Here is the objPlayer's Step Event:
Code:
// Key Inputs

// If Expression
if(hascontrol)
{
    // Assign Variable
    key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
    key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
    key_jump = keyboard_check_pressed(vk_space);
}

// Else
else
{
    // Assign Variable
    key_right = 0;
    key_left = 0;
    key_jump = 0;
}

// Movement

// Declare Temp
var move = key_right - key_left;

// Assign Variable
hsp = move * walkspeed;

// Assign Variable
vsp = vsp + grv;

// Jump Collisions

// If Object At
var l5FF2BBF4_0 = instance_place(x, y + 1, objWall);
if ((l5FF2BBF4_0 > 0))
{
    // If Variable
    if(!(key_jump == 0))
    {
        // Assign Variable
        vsp = -5;
    }
}

// Horizontal Collisions

// If Object At
var l10CBD9A7_0 = instance_place(x + hsp, y, objWall);
if ((l10CBD9A7_0 > 0))
{
    // Loop
    while(true)
    {
        // If Object At
        var l4E305BEB_0 = instance_place(x + sign(hsp), y, objWall);
        if (!(l4E305BEB_0 > 0))
        {
            // Jump To Point
            x = x + sign(hsp);
        }
   
        // Break
        break;
    }

    // Assign Variable
    hsp = 0;
}

// Jump To Point
x = x + hsp;

// Vertical Collisions

// If Object At
var l38BDDE9F_0 = instance_place(x, y + vsp, objWall);
if ((l38BDDE9F_0 > 0))
{
    // Loop
    while(true)
    {
        // If Object At
        var l5FB9B04A_0 = instance_place(x, y + sign(vsp), objWall);
        if (!(l5FB9B04A_0 > 0))
        {
            // Jump To Point
           
            y = y + sign(vsp);
        }
   
        // Break
        break;
    }

    // Assign Variable
    vsp = 0;
}

// Jump To Point

y = y + vsp;

// Animations

// If Object At
var l6827878A_0 = instance_place(x, y + 1, objWall);
if (!(l6827878A_0 > 0))
{
    // If Expression
    if(sign(vsp) > 0)
    {
        // If Variable
        if(swordswing == true)
        {
            // Set Sprite
            sprite_index = sprPlayerSword;
            image_index += 0;
       
            // If Expression
            if(sprite_index = sprPlayerSword)
            {
                // If Expression
                if(image_index = 8)
                {
                    // Set Sprite
                    sprite_index = sprPlayerJump;
                    image_index = 0;
               
                    // Set Animation Speed
                    image_speed = 0;
               
                    // Assign Variable
                    swordswing = false;
                }
            }
        }
   
        // Else
        else
        {
            // Set Sprite
            sprite_index = sprPlayerJump;
            image_index = 0;
        }
    }

    // Else
    else
    {
        // If Variable
        if(swordswing == true)
        {
            // Set Sprite
            sprite_index = sprPlayerSword;
            image_index += 0;
       
            // If Expression
            if(sprite_index = sprPlayerSword)
            {
                // If Expression
                if(image_index = 8)
                {
                    // Set Sprite
                    sprite_index = sprPlayerJump;
                    image_index = 1;
               
                    // Set Animation Speed
                    image_speed = 0;
               
                    // Assign Variable
                    swordswing = false;
                }
            }
        }
   
        // Else
        else
        {
            // Set Sprite
            sprite_index = sprPlayerJump;
            image_index = 1;
        }
    }
}

// Else
else
{
    // Set Animation Speed
    image_speed = 1;

    // If Variable
    if(hsp == 0)
    {
        // If Variable
        if(swordswing == true)
        {
            // Set Sprite
            sprite_index = sprPlayerSword;
            image_index += 0;
       
            // If Expression
            if(sprite_index = sprPlayerSword)
            {
                // If Expression
                if(image_index = 8)
                {
                    // Set Sprite
                    sprite_index = sprPlayer;
                    image_index += 0;
               
                    // Assign Variable
                    swordswing = false;
                }
            }
        }
   
        // Else
        else
        {
            // Set Sprite
            sprite_index = sprPlayer;
            image_index += 0;
        }
    }

    // Else
    else
    {
        // If Variable
        if(swordswing == true)
        {
            // Set Sprite
            sprite_index = sprPlayerSword;
            image_index += 0;
       
            // If Expression
            if(sprite_index = sprPlayerSword)
            {
                // If Expression
                if(image_index = 8)
                {
                    // Set Sprite
                    sprite_index = sprPlayerRun;
                    image_index += 0;
               
                    // Assign Variable
                    swordswing = false;
                }
            }
        }
   
        // Else
        else
        {
            // Set Sprite
            sprite_index = sprPlayerRun;
            image_index += 0;
        }
    }
}

// If Variable
if(!(hsp == 0))
{
    // Set Instance Scale
    image_xscale = sign(hsp);
    image_yscale = 1;
}

// Sword Attack

// If Expression
if(keyboard_check_pressed(vk_shift))
{
    // Assign Variable
    swordswing = true;

    // Set Audio Pitch
    audio_sound_pitch(sndSwing, random_range(0.8, 1.2));

    // Play Audio
    audio_play_sound(sndSwing, 0, 0);

    // Declare Temp
    var xdifference = x - xprevious;
    var ydifference = y - yprevious;

    // If Variable
    if(swordswing == true)
    {
        // Set Sprite
        sprite_index = sprPlayerSword;
        image_index += 0;
   
        // If Expression
        if(sprite_index = sprPlayerSword)
        {
            // If Expression
            if(image_index = 8)
            {
                // Set Sprite
                sprite_index = sprPlayer;
                image_index += 0;
           
                // Assign Variable
                swordswing = false;
            }
        }
    }

    // If Expression
    if(!(xdifference = 0 and ydifference = 0))
    {
        // Assign Variable
        swordx = xdifference;
        swordy = ydifference;
    }

    // Set Animation Speed
    image_speed = 1;

    // Create Instance
    instance_create_layer(x + sign(xdifference) * 32, y + sign(ydifference) * 32, "Instances", objPHitBox);
}
Right now my guess is that it has something to do with my horizontal movements or when the hit box instance is created at the end of my step event.

But I'm not sure how to make the hit box move when my player is standing still.

Any help with this would be appreciated!
 
T

Thunder Lion

Guest
You need to define a facing direction. I feel your swing is only being based off when moving a direction e.g. walk right hit box goes right.
Simply solution:
If the player is facing one direction the hit box will be made in front of them. Make it disregard motion. To keep it from having a short wing do to speed just add the actual motion speed to the facing direction e.g. if going right place the hit box x+speed this will correct for the speed travel and place the hitbox the same distance infront of the player via facing direction.
 

TheouAegis

Member
var xdifference = x - xprevious;
var ydifference = y - yprevious;

Those will always be 0 when not moving. And you used them as coordinate offsets. As Thunder said, you need to base it off of a facing factor, not a movement factor.
 

Sarena

Member
Thanks guys! It was indeed that I needed to check for facing instead of movement.

I simply made a variable for facing, and changed it every time the player faces a direction. From there I was able to place the box based on the direction that they were facing.
 
Top