SOLVED Issues with enemy collision while in certain state - will only detect one side and stop.

Nirwins

Member
Hi all.

I'm having a strange issue with collision for my enemy object while in its "attack" state in my 2D platformer. While in this state the enemy moves towards the player object if there is no collision with the wall in front of it. This works perfectly with any wall to the right of the enemy, and it stops at the wall, but when moving to the left, the enemy runs straight through any wall.

I've watched multiple tutorials trying to figure out my issue, but I cannot figure out how to fix this.

Apologies if my code isn't very good, I only started learning recently.

Here is the code for the step event in my enemy object:

GML:
vsp = vsp + grv;


//Horizontal collision

    if (place_meeting(x+hsp,y,oWall))
    {
        while (!place_meeting(x+sign(hsp),y,oWall))
        {
            x = x + sign(hsp);
        }
       
        hsp = -hsp;
       
    }

//Vertical collision
if (place_meeting(x,y+vsp,oWall))
{
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y +sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

//Jumping
if place_meeting(x+5*sign(hsp),y,oTrigger)
{
    vsp = -5;
}

if(state == states.idle){
    #region Idle
    //Behavior
    counter += 1;
   
    //Transition triggers
    if(counter >= room_speed){
        var change = choose(0,1);
        switch(change){
            case 0: state = states.wander;
            case 1: counter = 0; break;
        }
    }
    if(collision_circle(x,y, 250, oPlayer, false, false)){
        state = states.alert;
    }
   
    //Sprite
    sprite_index = sEnemy;
    #endregion
}

else if(state == states.wander){
    #region Wander
    //Behavior
    counter += 1;
    x = x + hsp;
   
    //Transition triggers
    if (counter >= room_speed){
        var change = choose(0,1,2);
        switch(change){
            case 0: state = states.idle;
            case 1: x = x + hsp;
            case 2: x = x - hsp;
        }
    }
    if(collision_circle(x,y, 200, oPlayer, false, false)){
        state = states.alert;
    }
   
    //Sprite
    sprite_index = sEnemyR;
    image_xscale = sign(hsp);
    #endregion
}

else if(state == states.alert){
    #region Alert
    //Behavior
   
    //Transition triggers
    if(!collision_circle(x, y, 200, oPlayer, false, false)){
        state = states.idle
    }
    if(collision_circle(x, y, 100, oPlayer, false, false)){
        state = states.attack;
    }
   
    //Sprite
    sprite_index = sEnemy;
    var aimside = sign(oPlayer.x - x);
    if (aimside != 0) image_xscale = aimside;
    #endregion
}

else if(state == states.attack)
{
    #region Attack
    if (instance_exists(oPlayer))
    {
        //Behavior
        var dir = sign(oPlayer.x - x);
        if (dir != 0) and (!place_meeting(x*dir,y,oWall))
        {
            hitwall = false;
            hsp = dir * walksp;
            x = x + hsp;
        }
        else hitwall = true;

        //Transition triggers
        if(!collision_circle(x, y, 200, oPlayer, false, false))
        {
            state = states.idle;
        }
   
        //Sprite
        if (hitwall = false) sprite_index = sEnemyR;
        else sprite_index = sEnemy;
        image_xscale = dir;
    }
}
    #endregion
   

if (!place_meeting(x,y+1,oWall))
{
    grounded = false;
    sprite_index = sEnemyA;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
    grounded = true;
    image_speed = 1;
}
 
Last edited:

Nirwins

Member
What is x*dir? Where is that in relation to the enemy, the player, or anything in the game?
x*dir should be making sure that the enemy checks for collisions in the direction it is facing (dir is the variable that is either 1 or -1 depending on the position of the player object in relation to the enemy.)

Though to be honest I haven't been 100% sure about the x*dir, it just seemed to be the only thing that would work, at least with detecting walls to the right.
 

Nirwins

Member
x*dir should be making sure that the enemy checks for collisions in the direction it is facing (dir is the variable that is either 1 or -1 depending on the position of the player object in relation to the enemy.)

Though to be honest I haven't been 100% sure about the x*dir, it just seemed to be the only thing that would work, at least with detecting walls to the right.
If I have it as just x, then when the enemy chases the player into the wall, it gets stuck there and cannot move until it changes state for some reason.
 

Nidoking

Member
And what is the operation you need when you want a certain number of pixels to the left or the right of the current pixel? You want one MORE or one LESS than what you have. That's not a job for multiplication.
 
Top