• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Wall Collision

Hey guys!
I'm currently working on solid wall collision for my game. Here's my code
GML:
var mySolid;

///Walls
mySolid = instance_place(x+xspeed, y, objSolid);
if place_meeting(x+xspeed+1, y, mySolid)
{
    xspeed = 0;
    
    while place_meeting(x+xspeed+1, y, mySolid)
        x -= 1;
}
else if place_meeting(x+xspeed-1, y, mySolid)
{
    xspeed = 0;
    
    while place_meeting(x+xspeed-1, y, mySolid)
        x += 1;
}
When the character's right side touches the wall, it works just fine. For their left side, however, they phase right through the wall.
 

Slyddar

Member
Try this :
GML:
//horizontal collision
if place_meeting(x+xspeed, y, objSolid) {
    while !place_meeting(x+sign(xspeed), y, objSolid) x+=sign(xspeed);
    xspeed = 0;
}
x += xspeed;

//vertical collision
if place_meeting(x, y+yspeed, objSolid) {
    while !place_meeting(x, y+sign(yspeed), objSolid) y+=sign(yspeed);
    yspeed = 0;
}
y += yspeed;
 
Top