PJProgramming
Member
Trying to code a simple platformer and I'm having some issues with falling onto platforms. The player character will either be slightly above the platform when he lands or slightly below. If I use full numbers for the gravity variables (1, 2, etc.) I don't have this problem but then it messes with my jump height. I also have another bug that may or ay not be related but if he falls into the corner of the platform he sort of wedges in and cannot continue moving in that direction. He can only jump or move the other way at that point. The code I used has been placed below.
Create Event:
/// @description Initialize
hsp = 0;
vsp = 0;
fall_spd = 3;
grv = .25;
spd = 1.25;
Step Event:
/// @description Bon's Abilities
//Buttons
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(ord("Z"));
if keyboard_check_pressed(ord("I"))
{
game_restart();
}
//Gravity is a thing
if !place_meeting(x, y + grv, o_collision)
{
if vsp < fall_spd
{
vsp += grv;
}
} else
{
vsp = 0;
if key_jump
{
vsp -= 4
}
}
hsp = (key_right - key_left) * spd;
//Collisions
if place_meeting(x + hsp, y, o_collision)
{
while !place_meeting(x + sign(hsp), y, o_collision)
{
x += sign(hsp);
}
hsp = 0;
}
if place_meeting(x, y + vsp, o_collision)
{
while !place_meeting(x, y + sign(vsp) * grv, o_collision)
{
y += sign(vsp) * grv;
}
vsp = 0;
}
x += hsp;
y += vsp;
Create Event:
/// @description Initialize
hsp = 0;
vsp = 0;
fall_spd = 3;
grv = .25;
spd = 1.25;
Step Event:
/// @description Bon's Abilities
//Buttons
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(ord("Z"));
if keyboard_check_pressed(ord("I"))
{
game_restart();
}
//Gravity is a thing
if !place_meeting(x, y + grv, o_collision)
{
if vsp < fall_spd
{
vsp += grv;
}
} else
{
vsp = 0;
if key_jump
{
vsp -= 4
}
}
hsp = (key_right - key_left) * spd;
//Collisions
if place_meeting(x + hsp, y, o_collision)
{
while !place_meeting(x + sign(hsp), y, o_collision)
{
x += sign(hsp);
}
hsp = 0;
}
if place_meeting(x, y + vsp, o_collision)
{
while !place_meeting(x, y + sign(vsp) * grv, o_collision)
{
y += sign(vsp) * grv;
}
vsp = 0;
}
x += hsp;
y += vsp;