• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Player teleporting when jumping!

  • Thread starter Grady Boudreaux
  • Start date
G

Grady Boudreaux

Guest
Hey! So I'm making a platformer game, but whenever I try to jump, the player teleports down and to the left. I've looked around a bit but had no idea why this was happening. Hopefully, someone with more experience than me can swoop in and fix the problem. Here's the code:

The Step Event:
//Get Player Input
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) or keyboard_check(vk_up) or keyboard_check(ord("W"));

//Calculate Movement
var move = key_right - key_left;

hsp = move * walksp;
vsp = vsp + grv;

//Jumping
if (place_meeting(x, y + 1, obj_block)) and (key_jump) {
vsp = -7;
}

//Horizontal Collision
if (place_meeting(x + hsp, y, obj_block)) {
while (!place_meeting(x + sign(hsp), y, obj_block)) {
x = x + sign(hsp);
}
hsp = 0;
}

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

//Animation / Graphics
if (!place_meeting(x, y + 1, obj_block)) {
sprite_index = spr_player_airborne;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else {
image_speed = 1;
if (hsp == 0) {
sprite_index = spr_player_two
}
else {
sprite_index = spr_player_running;
}
}


x = x + hsp;
y = y + vsp;

The Create Event:

hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 4;
 

TheouAegis

Member
Check the origin of your airborne Sprite. Compare it to the origin of your normal standing or running sprites.
 

Megax60

Member
First of all changue:

vsp = vsp + grv

To

vsp += grv

Also grv should be 1 or 0.5, also it should be below the jump code, also it should run only if player is on air, also changue:

x = x + hsp

To

x += hsp

Also it should be below horizontal code and above vertical code

Im not on the pc to test the code so thats probably all i can do

Did you fixed it?
 
Last edited:

TheouAegis

Member
None of those will make any significant difference to his project, they are mostly minor syntax changes. His grv can be whatever value he wants and he can add it to vsp whenever he wants.
 
G

Grady Boudreaux

Guest
Thanks to all of you! I fixed it! (I don't know how I didn't notice the origin point was off...). Although the other things are just small syntax errors, I will still change them. :)

Gardy
 

Megax60

Member
@TheouAegis im still here you know, you should show more respect instead of calling my post nonsensical, and im "her" not "his"
i helped fixing his code
first i told him to change vsp = vsp + grv to take away that bad habit, so he can improve
grav should be 0.2, 0.5 or 1, because if vsp surpasses the grav limit (if he implements it(i repeat, if he implements it)) it will just ignore the limit
the rest is to fix a bug that sometimes causes the player to get stuck in the corner

i have better things to do
 

TheouAegis

Member
Sorry. ^"HER post."

0.2 doesn't even work. Rounding errors. So his grv can be whatever. If it surpasses the limit, that's what > is for.

vsp=vsp+grv can be run first just fine and is how gravity works in the real world. His collision code is to prevent gravity from accelerating him through the ground.

X += hsp

Also it should be below horizontal code and above vertical code
the rest is to fix a bug that sometimes causes the player to get stuck in the corner
Ok, that I agree with, but the way your post was worded, it was kind of vague what your intended meaning was there. So to clarify for anyone else reading:

x+=hsp (or x=x+hsp) should be run immediately after horizontal collision checks with this style of collision checking, but before any vertical collision code. Or if you run your vertical collision checks first, you need to make sure y+=vsp is handled before the horizontal collision check. Concave corners shouldn't be an issue, but you will easily get stuck on convex corners like cliff edges and stairs if x+=hsp and y+=vsp are handled at the same time, as there could be no immediate collision detected horizontally nor vertically, but if there is one horizontally AND vertically, you would move into it after the collision check failed to find it.
 

rIKmAN

Member
i know, but i was on phone, it automatically sets the first letter to capital
If you knew, then you should have changed it.

If you are going to give advice, then please make sure the code changes you are telling people to make are correct.
 
Top