GMS 2 Help With Horizontal Platforms

T

Tristin

Guest
Hey ya'll, I've been working on a plat former and I can't seem to get the horizontal moving platform to work properly. I've watched videos, gone through forums, and tried messing around with the code my self but with no success.

I got Vertical Platforms to work alright but I can't get these dang Horizontal ones to work properly. The player can stay on the platform when the platform moves in the Positive X direction but once the platform moves in the Negative X direction, the player just drifts off the platform in the positive X direction. The Platform object also has the parent object of O_Wall.

O_Player Create Code:

vsp = 0;
grv = 0.3;
walkspd = 3;
hsp = walkspd
controller = 0;
hascontrol = true;
canjump = 0;
is_on_plat = false;
canDouble = false;
audio_sound_pitch(SN_Shoot,0.8);


O_Player Step Code:

if (hascontrol)
{
key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space)

if (key_left) or (key_right) or (key_jump)
{
controller = 0;
}

if (abs(gamepad_axis_value(0,gp_axislh)) > 0.2)
{
key_left = abs(min(gamepad_axis_value(0,gp_axislh),0));
key_right = max(gamepad_axis_value(0,gp_axislh),0);
controller = 1;

}

if (gamepad_button_check(0,gp_face1))
{
key_jump = 1;
controller = 1;

}
}
else
{
key_right = 0;
key_left = 0;
key_jump = 0;

}

//Calculate Movement
var move = key_right - key_left;
hsp = move * walkspd;
vsp = vsp + grv;
if (place_meeting(x,y+1,O_Wall)) && (key_jump)
{
repeat(5)
{
with (instance_create_layer(x,bbox_bottom,"Layer_Bullets",O_Dust))
{
vsp = 0;
}
}
vsp = -7;
}

//double jump
if canDouble && !place_meeting(x, y+1, O_Wall) && key_jump
{
vsp = -8;
canDouble = false;
}

//reset doublejump on touching ground
if place_meeting(x, y+1, O_Wall)
{
canDouble = true;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,O_Wall))
{

while (!place_meeting(x+sign(hsp),y,O_Wall))
{
x = x + sign(hsp);
repeat(5)
{
with (instance_create_layer(x,bbox_bottom,"Layer_Bullets",O_Dust))
{
vsp = 0;
}
}
}
hsp = 0;
}

//Horizontal Collision Platform
if (place_meeting(x+hsp,y,O_Plat))
{

while (!place_meeting(x+sign(hsp),y,O_Plat))
{
x += sign(hsp);
repeat(5)
{
with (instance_create_layer(x,bbox_bottom,"Layer_Bullets",O_Dust))
{
vsp = 0;
}
}
}
hsp = 0;
}

x = x + hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,O_Wall))
{
while (!place_meeting(x,y+sign(vsp),O_Wall))
{
y = y + sign(vsp);
}
vsp = 0;
}

//Vertical Collision PLAT
if (place_meeting(x,y+vsp,O_Plat))
{
while (!place_meeting(x,y+sign(vsp),O_Plat))
{
y += sign(vsp);
}
vsp = 0;

var instance = instance_place(x, y + 1, O_Plat);
if (instance !=noone)
{
is_on_plat = true;
hsp = (instance.hsp + move) * instance.dir
}
else
{
is_on_plat = false;
}
}
y = y + vsp;

//Calculate Movement
var move = key_right - key_left;

hsp = move * walkspd

vsp = vsp + grv

//Jumping
canjump -= 1;
if (canjump > 0) && (key_jump)
{
vsp = -10;
canjump = 0;
repeat(5)
{
with (instance_create_layer(x,bbox_bottom,"Layer_Bullets",O_Dust))
{
vsp = 0;
}
}
}
//Animation

if (!place_meeting(x,y+1,O_Wall))
{

sprite_index = S_Player_A;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
canjump = 10;
if (sprite_index == S_Player_A)
{
audio_sound_pitch(SN_Landing,choose(0.8,1.0,1.2));
audio_play_sound(SN_Landing,4,false);
repeat(5)
{
with (instance_create_layer(x,bbox_bottom,"Layer_Bullets",O_Dust))
{
vsp = 0;
}
}
}
image_speed = 1;
if (hsp == 0)
{
sprite_index = S_Player;
}
else
{
sprite_index = S_Player_R;
}
}

if (hsp != 0) image_xscale = sign(hsp);


O_Plat Create Code:


randomize();

dir = choose(-1,1);


O_Plat Step Code:

x += hsp * dir;

if (x < position_from || x > position_to)
{
dir *= -1;
}

with (instance_place(x,y-1,O_Player))
{
x += other.hsp
}



Any and all help is very appreciated!!
 

Simon Gust

Member
Make sure you also incooperate the platforms direction and not only it's momentum into the player's momentum.
Code:
with (instance_place(x,y-1,O_Player))
{
    x += other.hsp * other.dir;
}
 

TheouAegis

Member
hsp = (instance.hsp + move) * instance.dir

Why are you adding the player's direction to the platform's speed? Take move out of that equation.
 
T

Tristin

Guest
Make sure you also incooperate the platforms direction and not only it's momentum into the player's momentum.
Code:
with (instance_place(x,y-1,O_Player))
{
    x += other.hsp * other.dir;
}

IT WORKED!!! Thank ya'll so much!!!!
 
Top