C
Clawson
Guest
Hi all, looking for some help with a problem I'm having getting pixel-perfect jump heights in a 2D platformer.
I started with the usual that most tutorials (Shaun Spalding, Heartbeast) suggest, ie
where 'jumpheight' is a negative number has already been set in the create event and key_jump is a 'keyboard_check_pressed' function. Eventually after much trial and error I managed to graduate to
where 'grounded' is set based on 'if placemeeting(x, y+1...etc). Both of these worked to an extent, however the problem I'm having is that when I run my game, the actual jump height is inconsistent - I have an asset layer in my test room with a line indicating where the top pixel of the character's head should reach when they jump (I was using this to test what I needed the jumpspeed and gravity variables to be to achieve the height I wanted), and maybe half of the time this is reached, the other half of the time my character stops anywhere between 1-4 pixels short before gravity kicks in and reverses the movement. After some measurement of my sprites I know that I want the peak of my jump height to be 38 pixels up from my characters current position.
As I understand it, isn't lengthdir_y(n,dir) supposed to return the speed required to move n pixels in the direction specified? so I set jumpheight = 38 (since I want to jump exactly 38 pixels and rearranged my code and tried
since GM2 considers 90 degrees as 'up', but this just sends my character to space at rocket speed, and while I can get something closer to what I want by manipulating my gravity variable, the jump arc is running at the speed of light. No dice. So I tried
Which achieves the desired pixel perfect jump height but of course simply teleports the player to that location (and may as well just be y = y+jumpheight anyway).
I've managed to clamp my acceleration up to a maximum walk speed when moving and lerp my hspeed to a stop when the player releases the movement keys, so from that I'm suspecting that I'll have to use clamp to limit the rate of accelleration and lerp to slow down to the peak of the jump before gravity accelerates their fall, but I'm at a complete loss as to how to implement this.
Does anyone have any suggestions or can point me in the direction of any tutorials? is lengthdir_y even a useful function in this case? am I barking up the wrong tree entirely? Many thanks in advance for any help!
I started with the usual that most tutorials (Shaun Spalding, Heartbeast) suggest, ie
Code:
//in create
hsp = 0
vsp = 0
grv = 0.3
jumpheight = -7
...other variables, etc.
//in step event
...keys definition, movement, collision checking, gravity, etc...then
if (place_meeting(x,y+1,oWall)) && (key_jump)
{
vsp = jumpheight;
}
Code:
if (grounded == true) && (key_jump) && !(key_duck)
{
vsp = jumpheight;
}
As I understand it, isn't lengthdir_y(n,dir) supposed to return the speed required to move n pixels in the direction specified? so I set jumpheight = 38 (since I want to jump exactly 38 pixels and rearranged my code and tried
Code:
if (grounded == true) && (key_jump) && !(key_duck)
{
vsp = lengthdir_y(jumpheight,90);
}
Code:
if (grounded == true) && (key_jump) && !(key_duck)
{
y = y + lengthdir_y(jumpheight,90);
}
I've managed to clamp my acceleration up to a maximum walk speed when moving and lerp my hspeed to a stop when the player releases the movement keys, so from that I'm suspecting that I'll have to use clamp to limit the rate of accelleration and lerp to slow down to the peak of the jump before gravity accelerates their fall, but I'm at a complete loss as to how to implement this.
Does anyone have any suggestions or can point me in the direction of any tutorials? is lengthdir_y even a useful function in this case? am I barking up the wrong tree entirely? Many thanks in advance for any help!
Last edited by a moderator: