GMS 2 Collision with wall

Discussion in 'Programming' started by Gigicom, Apr 10, 2019.

  1. Gigicom

    Gigicom Member

    Joined:
    Apr 10, 2019
    Posts:
    2
    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
     
  2. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    61
    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
    }
    
     
    Gigicom and Edwin like this.
  3. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    654
    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: Apr 17, 2019 at 4:30 PM
    Gigicom likes this.
  4. Gigicom

    Gigicom Member

    Joined:
    Apr 10, 2019
    Posts:
    2
    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!
     
    Bentley likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice