GMS 2 platformer isn't working

Discussion in 'Programming' started by Jonar, Feb 10, 2019.

  1. Jonar

    Jonar Member

    Joined:
    Dec 9, 2018
    Posts:
    7
    I'm using code from Shaun's complete platformer tutorial, wall jumping tutorial and tile collision tutorial. The player bounces on walls, does not jump normally and starts flying left when I try to wall jump.

    PLEASE HELP ME!!!


    PLAYER CREATE

    // sets animation

    sprite_index = s_player_idle


    // starting speeds (don't change)

    hsp = 0;
    vsp = 0;

    hsp_frac = 0;
    vsp_frac = 0;


    // left/right speeds

    hsp_acc = 1;
    hsp_fric_ground = 2;
    hsp_fric_air = 1;
    hsp_walk = 8;
    hsp_walljump = 10;


    // jump/max-fall speeds

    vsp_jump = -15;
    vsp_max = 15;
    vsp_walljump = -12;
    vsp_max_wall = 5;


    // variables that check for if you are toouching a wall or the ground (also don't change these)

    onground = false;
    onwall = false;


    // gravity

    grv = 0.5;
    grv_wall = 0.1;


    // other stuff

    jumpbuffer = 0;
    walljumpdelay = 0;
    walljumpdelay_max = 17;

    hascontrol = true;


    // tile collision

    tilemap = layer_tilemap_get_id("Collision");


    PLAYER STEP

    var bbox_side;


    // get player input
    #region
    if (hascontrol) {

    key_left = keyboard_check(vk_left);
    key_right = keyboard_check(vk_right);
    key_up = keyboard_check(vk_up);

    }

    else {

    key_right = false
    key_left = false
    Key_up = false

    }
    #endregion

    // calculate horizontal movement
    #region
    walljumpdelay = max(walljumpdelay - 1, 0);

    if (walljumpdelay == 0) {

    var dir = key_right - key_left;
    hsp += dir * hsp_acc;

    if (dir == 0) {

    var hsp_fric_final = hsp_fric_ground;
    if (!onground) hsp_fric_final = hsp_fric_air;
    hsp = approach(hsp, 0, hsp_fric_final); // moves hsp to 0 by hsp_fric_final

    }

    hsp = clamp(hsp, -hsp_walk, hsp_walk);

    }
    #endregion

    // wall jump
    #region
    if (onwall = true) and (!onground) and (key_up) {

    walljumpdelay = walljumpdelay_max;

    hsp = -onwall * hsp_walljump;
    vsp = vsp_walljump;

    hsp_frac = 0;
    vsp_frac = 0;

    }
    #endregion

    // calculate vertical movement
    #region
    var grv_final = grv;
    var vsp_max_final = vsp_max;

    if (onwall != 0) and (vsp > 0) {

    grv_final = grv_wall;
    vsp_max_final = vsp_max_wall;

    }

    vsp += grv_final;

    vsp = clamp(vsp, -vsp_max, vsp_max);


    // ground jump

    if (jumpbuffer > 0) {

    jumpbuffer--;

    if (key_up) {

    jumpbuffer = 0;
    vsp = vsp_jump;
    vsp_frac = 0;

    }

    }
    #endregion

    // dump fractions and get final integer speeds
    #region
    hsp += hsp_frac;
    vsp += vsp_frac;

    hsp_frac = frac(hsp);
    vsp_frac = frac(vsp);

    hsp -= hsp_frac;
    vsp -= vsp_frac;
    #endregion

    // horizontal collision
    #region
    if (hsp > 0) bbox_side = bbox_right; else bbox_side = bbox_left; // direction collision is happening

    if (tilemap_get_at_pixel(tilemap, bbox_side + hsp, bbox_top) == true) or (tilemap_get_at_pixel(tilemap, bbox_side + hsp, bbox_bottom) == true) { // if touching tilemap

    onwall = true;

    if (hsp > 0) x -= (x mod 16) + 15 - (bbox_right - x);
    else x += (x mod 32) + (bbox_left - x);

    hsp = 0;

    }

    x += hsp;
    #endregion

    // vertical collision
    #region
    if (vsp > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top; // direction collision is happening

    if (tilemap_get_at_pixel(tilemap, bbox_left, bbox_side + vsp) == true) or (tilemap_get_at_pixel(tilemap, bbox_right, bbox_side + vsp) == true) { // if touching tilemap

    if (vsp > 0) {

    y -= (bbox_bottom mod 16) - (bbox_bottom - y); // move player on top of ground

    }

    else y += (y mod 32) + (bbox_top - y); // move player below ground

    vsp = 0;

    }

    y += vsp;
    #endregion

    // calc current status
    #region
    if (onground) jumpbuffer = 6;


    // death check

    if (y > room_height) {

    hascontrol = false;
    slide_transition(TRANS_MODE.RESTART);

    }
    #endregion
     

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