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.

2

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest