• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

help with vertical moving platforms

T

temmieflakes42

Guest
ive managed to achieve vertical and horizontal motion in moving platforms, however there is an issue with the vertical platform (and with the horizontal platform, but it's less consequential than the vertical one)

neither of the platforms carry the player along
you have to walk along them and move with them to get across
this is a problem with the vertical platform, because you can't move up in time, so it passes through you, causing you to get stuck and then dropping you below.

both of my platforms are running on paths in case that's important, because it was the easiest method i could figure out to get them to move.

i just want to know how to get them to carry the player along. both of them are children of my parent wall object, "obj_wall" which has this collision code coded into the "step" event of my player object:
Code:
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);
    }
    vsp = 0;
 

obscene

Member
Here's what I use in my game with platforms that can move both horizontally and vertically. It can be tricky and you might need to experiment with where you place this exactly.

Code:
// If player is on the lift
with par_player
    {
    if place_meeting(x,y+2,other)
        {
         // Move him with the lift
          x+=other.x-other.xprevious
          y+=other.y-other.yprevious
        }
    }
 

NightFrost

Member
Vertical platforms (or lifts or elevators as you like) bring in multiple cases your code needs to work properly on. You should at least be aware of:
  • When player falls onto a vertically moving platform it needs to "catch" the player properly. You need to decide whether the platform or the player object does the catching, and that it always runs in correct order in regards to executing the objects' code. Or it might happen that the catch is done improperly and the player sinks into the platform.
  • You need to decide how the game must react when a platform pushes player against a solid. You can't let it have it push player through.
  • You need to decide how the game must react when a platfform comes down and player stands beneath it.
  • The platform needs to push and pull the player standing on it, but the player must not react to it. That is, when the platform goes down, it must pull the player downwards, but the player must not react to that motion as if it were falling and execute fall animation and sound. Likewise, when the plaform goes up, the upward motion must not be intepreted as jumping.
  • In reverse case, if the platform code does not pull the played down when going downwards, your code will likely interpret this as player having no floor to stand on, and player executes a fall, until they reach the platform again, which then moves away, and the player falls again until they reach the platform, and...
  • Further, in platformers, player movement code usually conserves vertical up/downward momentum as part of jumping/falling and have gravity work with it. The push and pull given by the platform must not be considered as momentum. Failure to take this into account is most notable in player jumping up when an upwards-moving platform stops.
In my experience many things become simpler when player object has a flag to tell when they are standing on a lift, and has separate variables to handle motion given by external sources.
 
S

Simulacron

Guest
Sorry for posting again in this thread, I realized it's already a little bit older, but I hope that someone can help me with this topic:
I used the code from @obscene to create a lift, that moves the player up and down: I modified the code the following:
Code:
if collision_rectangle(x,y,x+64,y-64,obj_player,0,0){
with obj_player
    {
    if place_meeting(x,y+2,other)
        {
            y+=other.y-other.yprevious
        }
    }
}
I removed the "x+=other.x-other.xprevious"-line because I want to move my player only up and down and added the collision_rectangle to check if the player is above the lift. All of this is in the step event. My problem is that the player allways lags behind the platform when it's moving down, because the obj_player object thinks he's falling then hits the lift and then the lift moves down again. I also can't jump during this period of time because the obj_player doesn't notice the lift as a solid ground. Surprisingly the code works when the lift moves upwards.
Thanks in advaced!
 

CoderJoe

Member
I don't know the rest of your code but make the platform recognized as ground by your player (probably select the parent to be whatever your using for ground). You need to look in your falling code and check first if the player is on top of the platform first, if not then fall normally, otherwise move down with the platform
 

Docker

Member
Sorry for posting again in this thread, I realized it's already a little bit older, but I hope that someone can help me with this topic:
I used the code from @obscene to create a lift, that moves the player up and down: I modified the code the following:
Code:
if collision_rectangle(x,y,x+64,y-64,obj_player,0,0){
with obj_player
    {
    if place_meeting(x,y+2,other)
        {
            y+=other.y-other.yprevious
        }
    }
}
I removed the "x+=other.x-other.xprevious"-line because I want to move my player only up and down and added the collision_rectangle to check if the player is above the lift. All of this is in the step event. My problem is that the player allways lags behind the platform when it's moving down, because the obj_player object thinks he's falling then hits the lift and then the lift moves down again. I also can't jump during this period of time because the obj_player doesn't notice the lift as a solid ground. Surprisingly the code works when the lift moves upwards.
Thanks in advaced!
Inside your platform step event put:
Code:
if (place_meeting(x, y-1, obj_player_parent)){
   obj_player_parent.y = ceil(bbox_top-16+speed);
}
change obj_player_parent to your player object, speed to whatever variable is controlling the speed that the platform moves at and 16 to half of whatever your player sprites width is, this checks for a collision with the player 1 pixel above the platform and 'locks' the player to the platform by setting the players y coordinate to the platforms bbox_top minus half its sprite width (so that the bottom of the sprite touches otherwise youll be placed in the middle of it assuming your player sprite is centered) as well as allowing for the speed its traveling to stop that fall catch fall catch problem.

Then you can use the exact same code but with -speed rather than +speed for when the platform travels up, I have two separate scripts that control up and down movement of objects in my platform but you can always use an if statement or something so it can tell which to use based on which way the platform is moving.

for instance:
Code:
if (y < yprevious){
   platform_direction = up;
} else {
   platform_direction = down;
}

if (place_meeting(x, y-1, obj_player_parent)){
   if (platform_direction == up){
      obj_player_parent.y = ceil(bbox_top-16-speed);
   } else {
      obj_player_parent.y = ceil(bbox_top-16+speed);
   }
}
This code should work and only requires minor tweaks based on how you've coded your platform movement.

You can then go one step further and add a collision event in your player object with the platform containing something like:
Code:
var below_platform = y > other.bbox_bottom; // // if my y position is 1 pixel below the platform

if (below_platform){
   if (place_meeting(x, y+other.spd+1, obj_solid)){ // if ground is platform speed + 1 below the player
        room_restart() // Modify with what you want to happen if a player is touching the ground and the platform lands on their head
    } else {
        y += other.spd+1; // move player down platform speed + 1
    }
}
And again you would just change spd equal to what controls your platform speed, obj_solid to whatever your ground, walls etc are called and change room_restart() for what you want to happen if the player gets trapped between the ground and platform.
 
Last edited:
K

Kyle Rider

Guest
I have had this bookmarked for a long time. I really like how simple this code is and runs very well when placed into a game.

I am having trouble adapting this code into my game and I think it is because of tilemap collision. I am starting to believe tilemap collision isn't the way to go.
 
R

RoboFartGasm

Guest
Here's what I use in my game with platforms that can move both horizontally and vertically. It can be tricky and you might need to experiment with where you place this exactly.

Code:
// If player is on the lift
with par_player
    {
    if place_meeting(x,y+2,other)
        {
         // Move him with the lift
          x+=other.x-other.xprevious
          y+=other.y-other.yprevious
        }
    }
Hey, just wanted to thank you for this beautiful piece of code. Did exactly what I wanted, and is so much cleaner and simpler than I could have hoped.
 
E

evilmatter2

Guest
obscene, could you explain step by step what i have to do for vertical moving platfomrs?
I put your code in the creation-code, but it doesnt work. The platform doesnt move when the player is standing on.

the only thing i have is the create-event which inlcudes "set a path for instance". setting a path is for me the simplest way, so what is the next step?

Thank you!
 

TheouAegis

Member
Obscene code does not go in the creation event, it goes in the end step event of the platform. And his code only works if the platform moves at a speed of 1 pixel per second.
 
E

evilmatter2

Guest
So, i have moving platform which includes a create-event with the "set a path for the instance" and a "end step"-event with code which contains obscenes code.
Now the platform doesnt move when the player stands on the platform, no matter if it moves horizontal or vertical. I use the iwbtg-engine.
 

obscene

Member
Sorry, but if you're using someone else's engine, realize probably nobody else here is using it or has any idea how it's working. If you don't understand how it works, it's probably not a good idea to use it until you know some basics.

The idea behind my code is more important than the code. It's like this...

1. When the platform moves, check if the player is on it.
2. Measure how far the platform just moved.
3. Move the player the exact same amount.

Where and how you'll do that depends on if you are moving your platform manually or using some built-in motion like speed,hspeed,vspeed,motion_add,etc.
 
Top