Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
Version: GM 5+6 Read: 57532 Tutorial
Filesize: 12 kb Creative Commons License

Moving platforms tutorial

1/52/53/54/55/5 (73 votes)

German translation A german translation of this article is made by Windapple and is available at the German GMC.

This tutorial describes how to create moving platforms, like elevators for a platform game. It will first describe moving platforms horizontal and after that vertical. This tutorial assumes you are already capable of creating a platform game. A tutorial on how to create a platform game can be found on the Game Maker Website.

Horizontal Platforms

Picture taken from Coscrove Air escape by DR Incorporated

With a horizontal moving platform the trick is to check the speed of the platform below you and adjust your speed to it. First you need to check whether you are standing on ground:

 Example code
if !place_free(x,y+1) then
  {
  gravity:=0;
  vspeed:=0;
  }

Then you need to add a check whether you are standing on a moving platform:

 Example code
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  {
  //some code
  }

You see that I use for the x coordinate: x-sprite_xoffset+sprite_width/2. x-sprite_xoffset means that you start looking at the left side of the image. Even if the origin of the image is set to a different position. Then the sprite_width/2 means that the x coordinate moves than to the right with half the sprite's width. So the x coordinate will be at the middle of the image. For the y coordinate you see y-sprite_yoffset which means the coordinate of the top side of the sprite. When adding the sprite height to it it will become at the bottem site of the image. So there will be checked whether there is a moving platform in the middle of the character right below the character.

Location of the origin

Now you only need to make the movement. For this you can set the hspeed of the player to the hspeed of the block:

 Example code
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  {
  objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  hspeed = objID.hspeed;
  }
else
  {
  hspeed:=0;
  }

This code will check whether there is a moving platform below the character and if there is, take is's ID and set the hspeed to the hspeed at that coordinate.

This will give you a horizontal moving platform.

A few notes:

  • In the above code you need to fill in object_moving_platform, fill in on that place the name of the object of the moving platform. If you have multiple moving platforms with different settings then create one object, which you set as parent in all the moving platforms and to which you let the code point. This will make the code work with all the moving platforms
  • Please remember to make the platform solid!!!
  • An important detail is to make it so that you can not get stuck between the moving platform and the ground. So make that the moving platform will automatically switch direction when you collide with it and your y coordinate is larger than the y coordinate of the moving platform at which you need to take account of the height of the sprite as well:
     Example code
    if y-sprite_yoffset+sprite_height>other.y-other.sprite_yoffset then
      {
      other.hspeed:=-other.hspeed;
      }
    This code needs to be placed in the collision event of the player with the moving platform.
  • There is an example file with horizontal and vertical moving platforms, get it here

Vertical Moving Platforms

Vertical moving platforms are a lot more difficult than horizontal moving platforms because there is always the risk of you getting stuck in them. The positive thing is that vertical moving platforms which are moving down are so that you also move down because of the gravity so they always work.

For a vertical platform you can use the same code as above with a few minor adjustments

 Example code
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  {
  objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  vspeed = objID.vspeed;
  }

However using this code will give problems because when you are not yet standing on top of the platform you can get stuck inside it. So add:

 Example code
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  {
  objID = instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
  vspeed = objID.vspeed;
  move_outside_solid(90,sprite_height);
  }

A few notes:

  • Make sure that the moving platform never squashes you. So make sure the moving platform stops when approaching the roof on a distance of at least your character's sprite height. Else way your character gets stuck inside the roof.
  • With a vertical moving platform you do not need to have a complex collision test for when the player approaches from the wrong side. Because then the player will simply drop through the moving platform and the platform will keep moving away from him so he is automatically free although he might fall down some distance.
  • Please remember to make the platform solid!!!
  • Please note that the unit can not jump when on the platform. In order to achieve this you need to add some more code:
     Example code
    if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
      {
      objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
      vspeed = objID.vspeed;
      move_outside_solid(90,sprite_height);
      if keyboard_check(vk_up) then
        {
        vspeed:=-10; //change 10 to the jump speed
        }
      }
  • There is an example file with horizontal and vertical moving platforms, get it here

User comments

 Christopher Carosn posted at 2005-02-19 17:45:05Quote
Very Useful
 Christopher Carson posted at 2005-02-19 17:49:05Quote
How do you know what event the code needs to go in

SD: The code goes within the step event
 Theo Chandler posted at 2005-03-12 19:06:59Quote
Thanks for helping me
 No_SkillZ posted at 2005-04-02 03:18:50Quote
There is one problem with this script, the object I made with the script keeps passing the platforms when ever it falls from a high point down onto the vertical moving platform, can you help me here?

SD: That should not happen. Have you looked at the example file to see how all scripts should be implemented?
This is the first time somebody told me about this error so I suspect is an issue with your implementation rather then this system.
 No_SkillZ posted at 2005-05-27 23:18:38Quote
You know the reply I sent you at 2005-04-02? I looked at the example, but I probably missed something, I don't know...

SD: As I stated this is not a known issue with this system. You are the first to report this so I do not know what is causing this. And as you have not given any other specific information I can once again only say to look at the tutorial again. The tutorial works for a lot of people that have tried it so there must be something wrong with your interpretation of the tutorial and your implementation of the code. And as I dont know anything about how you have done this I can't tell you anything more specific then I did last time you asked.
 DavidX posted at 2005-06-10 22:23:56Quote
Thanks
 Gilded posted at 2005-07-01 04:16:29Quote
"Please remember to make the platform solid!!!"

Heeey... Didn't you say in your coding efficiency tutorial that you should never ever use moving solids? :)

Very nice tutorial anyway, good job!

SD: Yup, luckely though the floor doesn't walk away so you don't have a moving solid object. For moving platforms have a look at the more specific moving platforms tutorial :p
 sticky_soldier posted at 2005-07-25 12:19:34Quote
i can't seem to download the example...
it just says: "Download request failed, please check your URL"
any help?

SD: fixed
 220.239.83.135 posted at 2005-11-10 10:05:04Quote
i am a loser with no life
 david posted at 2005-11-15 12:00:12Quote
um this code doesnt work for me because i am using an object that walks meaning its legs move. WHen i used this code my object just walked without keypress. And for some reason when the object jumps onto the block the block and him freeze and i cant jump at all. can u help me?
 david posted at 2005-11-15 12:04:08Quote
so just pretend it was a mario game. how would you make a moving block where mario moves with him?
 Simon Donkers posted at 2005-11-15 19:27:44Quote
By uhm, reading this tutorial.
 Brendan posted at 2006-05-20 08:28:04Quote
After i use this code my sprite doesnt jump
 T681 posted at 2006-06-09 15:19:18Quote
ehm, if you already got a platform ame you probably added this code:
if !place_free(x,y+1) then
{
gravity:=0;
vspeed:=0;
}
Remove that one.
If that was the problem, it\'s actually a shame you yust copy and paste the code before you actually understand it, or even read it -_-
 Benjamin posted at 2006-06-13 19:59:40Quote
Please help me, i am like so confused!!! do you just have to copy paste the above codes in to a code box, and it will work, or are you supost to change some of the code to abide to you game settings?
 Master Pobble-Wobble posted at 2006-07-21 16:47:28Quote
You're supposed to read the script. It tells you to change the comments. The script in the tutorial has virtually everything needed for the gravity. To make it work, follow the directions in the comments.

I've got a problem. I was making my own platformer before with moving platforms. It was working wonderfully, except that my hspeed had to be 0 when my character was not standing on it. Is there a way to use hspeed and not have to set your character's hspeed to 0 autmoatically when he's not on?
 Simon Donkers posted at 2006-07-21 19:35:22Quote
Hello Master Pobble-Wobble, this tutorial is based around the platform tutorial from Mark Overmars. Movement in that is handled by changing the x coordinate (x+=4;, x-=4; ).

There are plenty of ways to modify this but I guess the easiest would be to make a variable like hsp and use that all the time rather then hspeed. Now in the step event add:

x+=hsp;

and you are done. Note that you need to write things like friction and calculate the var direction yourself if you need them.
 Ultimate posted at 2006-08-14 20:17:58Quote
Oh, *thats* how you do it! I was using this:

{CODE}

if instance_place(self.x,self.y,character)
{
if self.vspeed=-2
{
character.vspeed=-2
}
else
if self.vspeed=2
{
character.vspeed=2
}
}
 WILKO posted at 2006-09-05 11:17:24Quote
gosh...im new to gm, and i only use the drag commands and variables...ill try to copy the codes straight in
 FunnyMaker posted at 2006-12-04 16:33:26Quote
Its very usefull!! Can you make something that will move auto? I mean with out set the direction, will choise himself? up-down or left-right.
 Dark Mage posted at 2006-12-08 06:45:10Quote
dude ur tut example dont work right, u need to make your hor blocks bounce of ea other. They get behind one another if u dont fix that, also the hor blocks bounce of "player" when u move towards em.
 Simon Donkers posted at 2006-12-08 13:40:55Quote
@FunnyMaker, have a look into the commands to change one object into an other object. That way you can switch between horizontal to vertical platform and do exactly what you want to have.

@Dark Mage, this example does exactly what I want it to do. I quite like blocks sliding behind each other. If something doesn't work the way you want it to work it's not broken, it's merely something else. The idea of an example is that people modify it to suit there specific wishes. Rather then clutter the code with special exception cases and bounce systems and multi directional movement systems I make a very basic example that every can understand.
 Cipher Software posted at 2006-12-08 19:12:43Quote
Is there any way to download this, say, in .PDF format?

If you don't have Acrobat you could print it with FinePrint PDF Factory. The trial version is great.

Thanks,

Cipher Software
 xelaron posted at 2007-01-09 20:08:32Quote
I implanted the code in my game and it didn't work so I tried few things and made a few changes and also that didn't work then I opend the original file from gamemaker.nl and implanted al the code there and it didn't work then I opend your file and corrected a few errors in my game and it didn't work.
the only result of my work was that I couldn't move on the platform not left right or up.
this error only occures in the horizontal moving object.
I read the othr replies but didn't find anything usefull that worked.
can you help me?
 xelaron posted at 2007-01-09 20:11:31Quote
sorry forgot to mention that if I press the buttons to move the action that makes me look left and right still works.

oh and a happy newyear btw
 Cipher Software posted at 2007-03-03 17:10:55Quote
Wow! How cool.

I'm currently developing a new game using some of this stuff. It's called ArenaForce, and (eventually, when it's done) will be available from http://www.ciphersoftware.co.uk/
 AlexAR posted at 2007-03-24 03:42:44Quote
Hey, this is really useful. But I have one question. Is my character object supposed to move through it when approaching the moving platform from the bottom. I copied your vertical platform code and it does what it is supposed to. I can land on it and I don't get stuck. And I did set my moving platform to solid. Am I supposed to be able to jump through it from the bottom?
 Potato posted at 2007-03-25 01:39:32Quote
"Friggin' awesome!"

No, really. After combining this with your platform script(which I'm in love with, by the way) and some decent sprites, my game's pretty much finished. I had the oh-no-I-fell-through-a-vertical-moving-platform problem at first, but after tinkering with your code I finally fixed my problem: turns out I had some conflicting code elsewhere. Thanks again!
 PLZ HELP ME! posted at 2007-05-11 22:15:18Quote
IM SOOOO SORRY! but im a newb at this...: ( im having trouble using it.....could u at all spell things out to me step by step???????? I tried downloading the tutorial and copy pasting the scripts and what not, but then an error message appeared on my screen when I ran the game! OMG HELP ME PLZ!!!!!!!!!!!!!!!!!!
 chaemelion posted at 2007-06-12 00:10:43Quote
I love your scripts and use them in my games, but my latest game uses paths and so the directions and hspeed of my platforms dont apply. Is there another version for using paths?
 Erik Leppen posted at 2007-07-09 19:56:17Quote
Hi there,

I just came across this because someone pointed this out, it looks very useful I must say. I checked out your example, there is the same little offset error that my own game suffers from... when you are standing still on a horizontally moving platform, you can see your sprite move two pixels when the platform reverses direction. That is because the movements of the character are done one step later for some reason. Do you know a way around this? I know it's just begin nit-picky about two pixels, but in my game those two pixels might cause a collision between character and moving platform making it impossible for my character to jump. If this is not the place to ask this question, please ignore it. Thanks in advance :)
 HEY posted at 2007-07-20 19:28:38Quote
I can't find an example of this.
Where is it?
 HEY posted at 2007-07-20 19:48:44Quote
Sorry

I already found it
 vincent cupo(coo-poh) posted at 2007-07-22 01:36:55Quote
(I'm italian irish and polish so cupo is pronounced coo-poh)
How do I make a program like greg overmars for other people to make their own games with?
 macon posted at 2007-07-31 02:49:27Quote
sub. vertical platforms

hey i know yall like to write complex codes and things but it will do the exact same thing if not even a little better if you just set your ch. speed to plat. speed when he is on it and just preform a test if somwhere in the middle and make sure he doesnt inter sect form side bottom or top.
duuuuuuuuuhhhhhhhh
thnx
 CoJaCk posted at 2007-08-19 11:27:53Quote
how the hell do you make a variable say global.color without it saying "error unknown variable".
 Kymagic posted at 2007-09-01 15:54:19Quote
The tutorial works great with my vertical platforms; my old code used to make the player get stuck in the platform when you jumped back onto the platform after jumping off. However, jumping onto a horizontal moving platform causes the player and platform to freeze. If I try to go left/right, my character only turns to look that way, and jumping won't work. Any idea why this could happen?
 kaaskop posted at 2007-09-05 15:12:00Quote
you're fantastic
 DOMBO posted at 2007-09-05 17:10:01Quote
very great
you are very smart and you may be proud of this scripts
 ryan posted at 2007-10-07 04:29:24Quote
9_6 A little tiny bit confusing. I can't find the example file xD
 Simon Donkers posted at 2007-10-10 17:46:11Quote
@Ryan, read the very last line of the tutorial.
 Peter posted at 2007-11-07 23:49:56Quote
I would like to see a version of a game that I could manipulate the game itself. If you have an example of the game I could learn from, please send me a copy of the game.
Thanks

Hawley81@yahoo.com
 dude posted at 2007-12-17 20:18:52Quote
how do you make like a spring thing?
 Christian posted at 2008-02-12 23:30:17Quote
to dude: Really simple. You make a spring object with only 1 frame for animation. For spring's event when it collides with character, set variable vspeed to whatever you want it to be. then make a timeline that for every... well, just look at my website after im done with ex: www.christianssite.zoomshare.com.
 Laloeka posted at 2008-03-16 13:03:56Quote
I can't understand
In NL???
 Drew posted at 2008-04-21 03:09:55Quote
umm i dont know how to code or anything i just make simple platformers :/

need to kno how to make shooting weapons , everything else i've figured out but i'm only 14 i got another 100 years to figure this program out..

p.s do not bother replying to this because the chances i will see ths thread again are slim
 yoshidude posted at 2008-05-28 00:19:50Quote
(Christian)

...Drew? I am 12, and I make 3D GAMES!!! wow am i good if a 14 year old cant do that...


...(no offense there are probably a lot of people that cant do that at your age either...)...
 sinister96 posted at 2008-06-05 03:07:00Quote
he keeps falling off the damn platfrm... i dosent do anything
 RayRay posted at 2008-06-16 03:23:37Quote
Well, this is how I do it, cause that's just complicated: <GM7>
Collision(obj_floorhor):
Speed Horizontal(other.hspeed)
But it's nice I guess
 jayjaywray posted at 2008-07-10 04:04:59Quote
i used the horizantelmoving platform example, when he touches the platform he stops
 brian22440 posted at 2008-07-13 14:49:54Quote
when he touches the platform, he moves correctly, but when he comes off, he still KEEPS ON GOING!


I use the platform engine.

Is there any way to solve this?
 cidoku posted at 2008-09-27 01:49:47Quote
thanks a lot
 Reddot Games posted at 2008-11-29 13:51:24Quote
I have version 7 of GM so, Do you think it would work???

If not, where can i get 6?
 Razourik posted at 2009-03-27 04:23:12Quote
it does not work! my character simply sits there spinning and the moving platform stops moving (im making a sonic game) and then a few moments later he falls through the moving platform and the moving platform starts moving again :( and if i have the set hspeed to 0 if you arent colliding with the moving platform then sonic cant move! :( can you help me? :)
 cunt posted at 2009-04-04 18:14:33Quote
hey ur example is a bit glitchy in the movment dept. but nonethe less great platform tut.
 Game Detour posted at 2009-05-06 01:24:47Quote
can you put a jump through platform example on here?


m/
 me posted at 2009-05-17 01:49:07Quote
this doesn't make scene
 kit posted at 2009-09-02 16:30:42Quote
i want to make a site that you have to pay to play on. i want it to be like a computer game on a disc except on the internet. i have no clue how to make a website and need help. are there any websites that help u make a website like this?
 eragon posted at 2009-11-05 22:24:15Quote
i dont get it...you should make a video
 kara posted at 2009-11-14 16:32:52Quote
is it realy impossible to do this only with drag & drop?
I mean, sure, thank you for thaking the time to write this tutorial but i don't want to copy and paste scripts and create a game i don't know 100% how it's made.
So... any drag and drop sollutions?
Thanks!
 Ultimate Killer posted at 2009-12-19 14:12:36Quote
Great!
 AdyPadyKet/Yambam posted at 2010-02-02 19:09:42Quote
Why was i banned?!?!
 Ijay posted at 2010-04-01 10:20:18Quote
How to put a recoil on sniping games like others.?????
 Keith posted at 2010-04-12 22:42:10Quote
quick question i am trying to implement your code and make it work with my game but i use WSAD which seems to be more comfortable with me and my friends who test my games. i got the foundation down and everything without your code but i would love to use your code so i can have some moving platforms but everytime i input your code my game goes bananaz and ive altered the code completely and still nothing seems to work right if i send you my game that im working on (under construction things will change) can you see what you can do about your code working coenside with my foundation? thanks in advance.
 chip posted at 2010-06-16 10:54:03Quote
when my object lands on the platform it moves with it but when i get off it contiues in the same direction as when it was on the platform how can i fix this
 Tyler posted at 2010-07-03 21:48:29Quote
Thanks man. You really answered my questions clearly. I have been wondering how to do this for some time now
 MBeentjes posted at 2010-09-02 19:03:00Quote
Dutch: Erg handige uitleg, ik had dit echt nodig!
English: Really nice explanation, I really need this!
 Jason posted at 2010-10-28 16:37:23Quote
Thank you so much and to all I do believe up above codes goes in step event :) : )

http://7i.proboards.com/
 Jester-n001 posted at 2010-11-02 06:34:38Quote
your a life saver, thanks, And I love the great explanations. Well, expect moving platforms to be in my next Super Block World level
 Dude posted at 2010-11-05 22:18:11Quote
It worked but I have no idea what I did, because it does not work anymore.

Reply

Name:
Site: