1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 Collision not working.

Discussion in 'Programming' started by Dropsuitcaptain, Feb 23, 2019.

  1. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    My horizontal collision isn't working properly but my vertical is, could someone help please:p

    my horizontal code:
    Code:
    if place_meeting(x+hspd,y,oHouse1){
        while !place_meeting(x+sign(hspd),y,oHouse1){
            x += sign(hspd);
        }
        hspd = 0;
    }
    my vertical code:
    Code:
    if place_meeting(x,y+vspd,oHouse1){
        while !place_meeting(x,y+sign(vspd),oHouse1){
            y += sign(vspd);
        }
        vspd = 0;
    }
    hspd is my variable for moving left & right.
    I've also tried this on different objects and it gives me the same result.
    Doesn't stop me like it's supposed to.
    Any help is appreciated!
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,130
    Are you using hspd or did you accidentally type hsp or hspeed? Don't just tell me what you use, go back and verify what you actually used throughout your entire code.
     
  3. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    Checked and there's nothing but hspd.
     
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    Check the collision mask and bounding box variables to ensure your place_meeting() tests actually fire when they're supposed to given x and y? More fundamentally, figure out what aspect of your nested if statements for modifying x is not triggering, and debug it using the available tools (debugger, show_debug_message(), draw_text() in the draw event, etc.)
     
  5. SnotWaffle Studios

    SnotWaffle Studios Member

    Joined:
    Jun 21, 2016
    Posts:
    291
    Could you explain what isn't working exactly?
     
  6. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    I have a house object and I don't want the player to be able to walk on top of it, so when I try to walk left or right I'm not supposed to be able to walk on top of the house.
    The vertical collision works fine though which is whats confusing me.

    Screenshot:
    https://gyazo.com/a0ae6cd585674454e15c2b87838c12cf

    My collisions just aren't working and it's confusing me.
     
  7. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    I tested this on a different project with centered collisions on the player and the wall (both 32x32 squares) and it still makes me pass right through the wall.
     
  8. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Can you show us your entire movement code? It may be something outside of your collision code that is making it so you can move horizontally onto the object.
     
  9. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    step event:
    Code:
    //move
    if keyboard_check(ord("D")){
        hspd = 3;
    }else if keyboard_check(ord("A")){
        hspd = -3;
    }
    else{
        hspd = 0;
    }
    
    x += hspd;
    y += vspd;
    
    
    if !place_meeting(x,y+1,oWall){
        vspd += grav;
    }else{
    if keyboard_check_pressed(vk_space){
        vspd = -11;
    }
    }
    //horizontal collision
    if place_meeting(x+hspd,y,oWall){
        while !place_meeting(x+sign(hspd),y,oWall){
            x += sign(hspd);
        }
        hspd = 0;
    }
    //vertical collision
    if place_meeting(x,y+vspd,oWall){
        while !place_meeting(x,y+sign(vspd),oWall){
            y += sign(vspd)
        }
        vspd = 0;
    }
    create event:
    Code:
    hspd = 0;
    vspd = 0;
    grav = 1;
     
  10. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Right after you check for the keyboard input, you are moving the player on the x and y, regardless of whether they are able to. That is your problem. You need to move the
    Code:
    x +=hspd;
    y += yspd;
    below all of your collision checks.
     
  11. Dropsuitcaptain

    Dropsuitcaptain Member

    Joined:
    Feb 8, 2019
    Posts:
    23
    Ah I feel stupid now lol.
    Thanks for your help!!:p:p:p
     

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