GML Slopes and Platformer Movement for Beginners

Discussion in 'Tutorials' started by Bentley, Feb 2, 2018.

Tags:
  1. JeanSwamp

    JeanSwamp Member

    Joined:
    Jan 11, 2018
    Posts:
    131
    @Bentley I'm playing around a bit with this, trying to set an acel/dec with down/up slopes, but seems quite hard the way the scripts work, and I'm not a fan of your suggestion of creating some sort of climb power based on dy.

    Any suggestions?
     
  2. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    790
    Hey Jean, I had something a while ago when I was working on a Kirby's Adventure clone (I use the word "clone" very loosely). Basically, if he was going uphill, I would get the direction from Kirby's feet to the slope. I'd then use lengthdir_x(hspd, dir_of_slope) and move the player that distance. So his speed is decreased uphill.

    This tutorial was specifically for slopes on destroyable terrain. If your slopes are pre-defined (you have sprites for slopes that aren't created in-game), you can just get the direction by looking at the slope. For example, if the sprite is a diagonal at 45 degrees, you'd input that:

    // Pseudo code
    hspd = lengthdir_x(hspd_max, 45);

    You do the opposite downhill. I'll elaborate / write some code if you reply.
     
    Last edited: Sep 18, 2019 at 5:42 PM
  3. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    111
    I felt the need to post just to say how simple yet brilliant this is.
     
    Bentley likes this.
  4. JeanSwamp

    JeanSwamp Member

    Joined:
    Jan 11, 2018
    Posts:
    131
    I calculate the angle of the slope I'm about to collide to set the sprite rotation accordingly, so I guess is similar approach to your lengthdir_x(hspd, dir_of_slope) concept. I only move forward as I'm using this for some sort of minecart mini game.

    When you say, do the opposite for downhill, what do you mean? Can't really figure it out
     
  5. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    790
    @JeanSwamp You could move at hspd_max and add additional movement based on the slope
    Code:
    hspd_extra = hspd_max - lengthdir_x(hspd_max, 45);
    hspd += hspd_extra;
    Something like that. You just find out how much hspd you didn't move b/c of the slope and add that to your current speed.

    Edit: and obviously this doesn't account for what you need to do when you are floating above the slope. I'd just use a while loop and move him to the ground.
     
    Last edited: Sep 20, 2019 at 6:04 PM

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