Search code examples
collisiongmlgame-maker-studio-2

Having trouble with wall collisions


I'm working on this game right now (with GameMaker Studio 2) and I'm currently coding walls and when I tried making horizontal collision, it worked fine, but when I made floor collision I would get stuck in the walls and floor

here's the code:

//input

keyRight = keyboard_check(ord("D"));

keyLeft = keyboard_check(ord("A"));

keyUp = keyboard_check(ord("W"));

keyDown = keyboard_check(ord("S"));

// direction

var move2 = (keyDown - keyUp) * vspd;

var move = (keyRight - keyLeft) * hspd;

//collision stuff idk

if place_meeting ( x + move, y ,obj_wall)


move2= 0;

if place_meeting ( x + move2, y ,obj_wall)

move= 0;
    

// comiit

x += move

y += move2

If someone could tell me what I'm doing wrong and how to do it right please tell me I would appreciate it thanks in advance.


Solution

  • You are checking for a wall at a horizontal offset in both cases and also move/move2 assignments are mixed up. So, instead of

    if place_meeting ( x + move, y ,obj_wall)
        move2= 0;
    
    if place_meeting ( x + move2, y ,obj_wall)
        move= 0;
    

    you could have

    if place_meeting ( x + move, y ,obj_wall)
        move= 0;
    
    if place_meeting ( x, y + move2 ,obj_wall)
        move2= 0;
    

    Or a small loop if you need the object to approach the collision target.