GML [Solved] Horizontal collision problem with Enemy Object

L

LunarIceCream

Guest
So I was making a platformer using a tutorial from Shaun Spalding, and it was a little outdated but overall working fine, until I got to this specific line of code with the oEnemy object in the step event.

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

It should work fine, but it always thinks it's doing a horizontal collision, resulting in the Enemy sprite pretty much dancing (moving left and right over and over again) in place as soon as it hits the floor instead of moving in the other direction when it hits a wall, like I want it to. I've tried everything I can think of, but there's seemingly no foolproof way to prevent the Enemy obj. from either dancing in place or walking through the walls straight off the screen. I'm not sure if it's a bug or if there's something I'm missing to prevent this, but I'd really appreciate any help on this topic.
 
Last edited by a moderator:

Slyddar

Member
It is probably happening because of the sprite origin. If it's not direct centre, when the sprite turns it gets stuck on the wall it just turned from, resulting in the dance.

You can try and change the origin to the middle, try and take 1 pixel off the sprites width, or the better option, just draw the sprite yourself and store the image_xscale in a variable, instead of directly flipping the object.

Create a new variable in CREATE called facing. In a draw event have this
Code:
draw_sprite_ext(sprite_index, image_index, x, y, facing, image_yscale, image_angle, c_white, image_alpha);
And any code you have that changes the image_xscale, assign it to facing instead. When facing is 1 the sprite is facing right, when facing is -1 it's facing left. Also depending on how you are assigning it, you may need to add code to ensure facing can never be 0 or the sprite will vanish.

The changing of the mask_index can also be the problem, but that depends on a few factors. Try the above first.
 
Last edited:
L

LunarIceCream

Guest
I miss worded that a bit, sorry. I meant to say it starts ‘dancing’ as soon as it hits the floor. It thinks the floor is the wall, essentially.
 

Slyddar

Member
I miss worded that a bit, sorry
Ah well that makes a difference. Well one reason that can happen is the origin is not within the mask of the sprite. Try adjusting that. Can also happen if you are swapping to a different sprite when you land. If you are, then it's most likely the 2 masks are different. Set them exactly the same to fix that. The other thing you can do to see if it's the mask, is just set the mask at the end of your step with: (note this is only useful if your object actually has multiple sprites)
Code:
mask_index = sEnemy;  //or whatever your enemy sprite is.
 
L

LunarIceCream

Guest
Again, not what I was talking about. The enemy sprite moves left and right over and over again as soon as it hits the floor. It’s basically mistaking a vertical collision for a horizontal one and idk what to do to fix it. It’s not a problem with the collision mask. Sorry I wasn’t very clear, I’m not good with explanations.
 
L

LunarIceCream

Guest
Code:
vsp = 0;
grv = 0.3;
walksp = 3;
hsp = walksp;

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

x = x + 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;
All of the code for the collision area and then some. The horizontal collision area is where the problem started anyways.
 

Simon Gust

Member
Doesn't sound like a collision problem at all.
All of the code for the collision area and then some. The horizontal collision area is where the problem started anyways.
The collision code looks fine it can't be there, I refuse to believe that.

Is there any movement or AI code for the enemy?
 
L

LunarIceCream

Guest
There’s no ai code, no. I don’t really know how to write any, just the ‘hsp = - hsp’, but that’s clearly not working out for me.
 

Simon Gust

Member
There’s no ai code, no. I don’t really know how to write any, just the ‘hsp = - hsp’, but that’s clearly not working out for me.
Does the enemy get stuck in place if that line is hsp = 0; ?

You could try to include that line in AI before collision happens.
Code:
if (place_meeting(x + hsp, y-10, oWall))
{
   hsp = -hsp;
}
 
Top