GMS 2 Jump Glitch (Please Help)

S

Sahibjot Cheema

Guest
Need help on this glitch. Everytime I jump and touch a wall and then press the opposite movement button to the wall, I get teleported to the wall on the other side and wise versa. Example, If I jump and touch the wall on the right and press left arrow key, I get teleported the very other wall on the left side and I can press right arrow key to move back to where i was.

Here is the code:

//Horizontal Movement
hsp = 0;
maxhsp = 4;
maxhspInV = 2;
//Vertical Movement
vsp = 0;
vspMax = 5;
grv = 0.2;
//Jumping
jumpHeight = -5.6;
//Friction
groundFric = 1;
airFric = 4;
//Extra
onFloor = false;
time = 0;
//Air Friction
if (onFloor) var curFric = groundFric;
else var curFric = airFric;
//Player Controls
if (keyboard_check(vk_right)) hsp+=1 / curFric;
if (keyboard_check(vk_left)) hsp-=1 / curFric;
if (!keyboard_check(vk_left)) && (!keyboard_check(vk_right)) && (onFloor) hsp = 0;
key_jump = keyboard_check_pressed(ord("Z"));
key_jump_held = keyboard_check(ord("Z"));
hsp = clamp(hsp, -maxhsp, maxhsp);
vsp = Approach(vsp, vspMax, grv);
//Horizontal Collision
if (place_meeting(x+(hsp),y,oGrass))
{
while (!place_meeting(x+sign(hsp),y,oGrass))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+(vsp),oGrass))
{
while (!place_meeting(x,y+sign(vsp),oGrass))
{
y += sign(vsp);
}
vsp = 0;
onFloor = true;
}
y += vsp;
//Jump Mechanic
if (onFloor) && (key_jump)
{
vsp = jumpHeight;
onFloor = false;
grv = 0.2;
}
if (vsp < 0) && (!key_jump_held)
{
vsp = max(vsp,jumpHeight / 2);
}
//Animation
if (!onFloor)
{
if (hsp == 0)
{
if (sign(vsp) < 0)
{
time = 0;
sprite_index = Player_Still_Jump;
}
else
{
time = 0;
sprite_index = Player_Still_Fall;
grv = 0.25;
}
}
else
{
if (sign(vsp) < 0)
sprite_index = Player_Jump;
else
sprite_index = Player_Fall;
grv = 0.25;
}
}
else
{
if (hsp == 0)
{
time += 1;
if (time > 300)
{
sprite_index = Player_Idle;
}
}
else
{
time = 0;
sprite_index = Player_Walk;
}
}
if (hsp != 0)
{
image_xscale = sign(hsp);
}
time += 1;
if (time > 180)
{

}
 

Simon Gust

Member
Check the bounding boxes of the sprite.
The sprite origin should never off-center. If your character turns around
Code:
if (hsp != 0)
{
   image_xscale = sign(hsp);
}
the bounding box of the sprite rotate around the origin. So when you rotate on a wall you might get stuck in it.
The while loops in your collision code detect that and send you to the other side of the map when you try to move out.
 

TheouAegis

Member
By the way your vertical collision code has this:

vsp =0;
onFloor = true;

You need to check if vsp was greater than 0 before setting onFloor to true. If vsp was negative, then you can't be on the floor.


Also...

if (hsp==0)
{
...
}
if (hsp != 0)
{
...
}

should be reduced to

if hsp == 0
{
...
}
else
{
...
}

It's not a matter of semantics; the two structures are fundamentally different. In the first code (yours), you check hsp twice every time. In the second code (mine), hsp is only checked once. The fewer times you read a variable if possible, usually the better.
 
Last edited:
S

Sahibjot Cheema

Guest
Check the bounding boxes of the sprite.
The sprite origin should never off-center. If your character turns around
Code:
if (hsp != 0)
{
   image_xscale = sign(hsp);
}
the bounding box of the sprite rotate around the origin. So when you rotate on a wall you might get stuck in it.
The while loops in your collision code detect that and send you to the other side of the map when you try to move out.
Yes everything is in the middle center. I also figured out how to fix the glitch. It was caused by the curFric variable.
 
Top