GameMaker [HELP] Moving platforms

AnnaM

Member
Hello!
I am trying to code a moving platform, however, it sometimes (not always) glitches into place and just...stays there shaking (not touching the walls or anything it might be colliding into).
I would like to know what could be causing this in the codes.

CREATION:
GML:
randomize();

dir = choose (-1,1);

STEP:
Code:
x += horizontal_speed * dir;

if ( x <= position_from or x >= position_to){
    dir *= -1;
}
horizontal_speed, position_from and position_to are variables set on the Variable Definitions inside the room.


It seems pretty simple coding and pretty straightforward but I can't seem to understand why it gets stuck by itself sometimes
 
We need to see what math you're setting those position variables with

Also, it's good practice IMO to use hdir and vdir to make the direction the variable is controlling a little clearer
 

AnnaM

Member
We need to see what math you're setting those position variables with

Also, it's good practice IMO to use hdir and vdir to make the direction the variable is controlling a little clearer and allow for adding vertical speed to things if you choose
The position variables are literally the X position (Where they move to and where they will come from) inside the room. Every platform has a different X to and X from, and it is set inside the Room. They all glitch randomly and independently though. Although sometimes they work perfectly, which is what baffles me!
 
The position variables are literally the X position (Where they move to and where they will come from) inside the room. Every platform has a different X to and X from, and it is set inside the Room. They all glitch randomly and independently though. Although sometimes they work perfectly, which is what baffles me!
Is there a reason you've done it in such a roundabout way instead of the platforms just spawning with their own variables and moving a set distance before turning?
 

AnnaM

Member
Is there a reason you've done it in such a roundabout way instead of the platforms just spawning with their own variables and moving a set distance before turning?
I was following a tutorial, which can be found Here. The only other tutorial about this is Shaun Spalding’s but many other newcomers had collision issues and other bugs, so I went with this one. The one I followed works perfectly about 80% of the time, but then the platforms randomly get stuck on their own. I’m trying to learn so it would be insightful to figure out why this specific way and this code just randomly fails, without any apparent changes.
 

FoxyOfJungle

Kazan Games
I think you shouldn't use minor/major and equal:

GML:
if (x < position_from || x > position_to) {
    dir = -dir;
}

x += horizontal_speed * dir;
And when positioning the platform, place it a little bit away from the position defined by position_from and position_to.

Another alternative would be to do this simply:

GML:
if place_meeting(x, y, obj_solid) {
    dir = -dir;
}

Or you simply do this:

GML:
// Create Event:
hsp = choose(1, -1);


// Step Event:
x += hsp;

if place_meeting(x+hsp, y, obj_solid) {
    hsp = -hsp;
}
Tip: only use randomize() once in the game, when starting the game.
 
Last edited:
I think you shouldn't use minor/major and equal:

GML:
if place_meeting(x, y, obj_solid) {
    dir = -dir;

[/QUOTE]
That's what I did a for a moving platform. I made sure they were placed to collide with the ground on either side of the gap and used dir*-1 to make them change direction when they hit a solid floor object
 

Nidoking

Member
The issue may be a floating point rounding issue. Position math sometimes gets off by a tiny fraction, especially if you're not working with integers (but even if all of the numbers are integers, sometimes they just turn into 0.00000001 on their own). Your platform might be 0.0000001 short of the desired position in one step, but once it moves one more time and changes course, it's now outside the bounds of the check and reverses again immediately, back and forth forever. Interestingly, one possible way to solve this, as said above, might be to adjust your bounds so that the platform intentionally goes one movement unit beyond them i.e. placing them in the middle of a movement step, so there's no possibility of rounding error. Another option could be to adjust the position a bit at the same time as you reverse direction, although that changes the movement a bit and might cause a less desirable effect.
 
Top