GMS 2 [SOLVED] Collision with wall

Gigicom

Member
Hello I now tried several tutorials for a code that works better than the solid option for collision.
But the character always collides with the wall a few pixels away.

Here is my movement-code:

Code:
if keyboard_check_direct(ord("W")){
    y -= walk_speed;
}

if keyboard_check_direct(ord("A")){
    x -=walk_speed;
}

if keyboard_check_direct(ord("S")){
    y += walk_speed;
}

if keyboard_check_direct(ord("D")){
    x += walk_speed;
}
as player name I use "obj_player" and as wall name I use a parent object "par_wall".
Thank you in advance
 

woods

Member
there are hundreds of ways to make collisions work...

what do you have for your collision code?

i mean "move if clear" could be as simple as..
Code:
if (!place_meeting(x+32,y,obj_wall) and (keyboard_check(ord("D"))
{
x+=4
}
 

Bentley

Member
But the character always collides with the wall a few pixels away.
Code:
if (keyboard_check(ord("W")))
{
    // If there is not a wall walk_speed away
    if (!place_meeting(x, y - walk_speed, par_wall))
    {
        // Move walk_speed
        y -= walk_speed;
    }
    // Otherwise, there is a wall walk_speed away
    else
    {
        // So move 1 pixel up until you are next to the wall
        while (!place_meeting(x, y - 1, par_wall))
        {
            y--;
        }
    }
}
You would do the same for the other 3 keys.
 
Last edited:

Gigicom

Member
Thank you very much for the answers.

Thank you Bentley, the code solved my problem. Now I can finally continue working on my game. Thanks!
 
Top