• 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 So I need help with collisions...

F

FunkyFreshJJD

Guest
I am slightly new to Game Maker and have never programmed before. I am trying to add collisions to my player when it hits a wall. Here is the code:

// Collide
if place_meeting(x, y, obj_wall) and x > xprevious then {
do x -= 1 until not place_meeting(x, y, obj_wall)
};
if place_meeting(x, y, obj_wall) and x > yprevious then {
do x -= -1 until not place_meeting(x, y, obj_wall)
};

This code makes it so the player can go right up to the wall and move up and down it when still on the wall. But for some reason I cannot go on top or bottom of the wall without it putting me back to my original position. Sorry if I am bad at explaining, I will have pictures explaining this.

So when I try to collide with the wall by going to the top or bottom, it will skip my player character to the position in the bottommost picture.

Idk what I am doing wrong or what I need to add.Made in GameMaker Studio 2 6_19_2019 10_08_52 AM.png Made in GameMaker Studio 2 6_19_2019 10_09_03 AM.png
 

Bentley

Member
Just reverse it for the other directions. Using your code:
Code:
if (place_meeting(x, y, obj_wall))
{
    if (y > yprevious)
    {
        do
        {
            y--;     
        }
        until (!place_meeting(x, y, obj_wall));
    }
}
I also saw you are checking your x position against your yprevious. That will mess things up.
 
F

FunkyFreshJJD

Guest
But how do I write that same code for all 4 sides of the wall?
 

Bentley

Member
But how do I write that same code for all 4 sides of the wall?
That's what I was saying to do in the previous post. Anyway, I'll just post the code. This works for 4 directional movement. Going off your code:

Code:
var xinput = keyboard_check(vk_right) - keyboard_check(vk_left);
var yinput = keyboard_check(vk_down) - keyboard_check(vk_up);

// No diagonal movement
if (xinput != 0)
{
    yinput = 0;    
}

x += xinput * move_speed
y += yinput * move_speed;

if (x > xprevious)
{
    while (place_meeting(x, y, o_solid))
    {
        x--;
    }  
}
else if (x < xprevious)
{
    while (place_meeting(x, y, o_solid))
    {
        x++;
    }      
}
else if (y > yprevious)
{
    while (place_meeting(x, y, o_solid))
    {
        y--;
    }      
}
else if (y < yprevious)
{
    while (place_meeting(x, y, o_solid))
    {
        y++;
    }      
}
 
Last edited:
Top