I wanted to try and make gravity and jump code myself not really knowing how to do it properly. Im still not great at gamemaker, although i've had coding experience with stuff like scratch and python.
I'm sorry if this code is an eyesore to anyone.
As shown in the video, the gravity works mostly fine (apart from the jitter once it gets near to the collision). The jumping however, dosen't work at all (I know why but dont know how to fix it).
Thanks to anyone who tries to help!
#Need help with gravity/jump code i tried making myself.
24 messages · Page 1 of 1 (latest)
there's a few issues here, some wrong math but also some wrong terminology which maybe is confusing you
gravity as a concept is not a speed, it's not how fast you are currently moving. when somebody jumps, they don't have "negative gravity"
gravity is an acceleration, it's a force that is always acting on us and always constantly applying to us
it's a mistake to write something like y = y + Gravity; because gravity is not our motion, it's the amount of change in our motion from moment to moment
on top of that your math is backwards in a lot of places, your vertical speed is kinda flipped upside down and you're subtracting instead of adding in a lot of places
your check to see if you're able to jump isn't really right either, you've got this CanJump variable that is only ever set to true
so once it's true, you can always jump
and the jump itself is more like a teleportation
you aren't giving yourself upward speed, you're teleporting up instantly without changing your speed
if you look in your create event you can see you made yourself a PlayerSpeedX variable, which is great
But you seem to have decided not to make a matching PlayerSpeedY variable, which doesn't really make any sense
You're gonna need a variable like that to implement gravity
oh, actually, that variable doesn't make too much sense for X either looks like, seems like it actually stores your maximum speed and not your current speed, so the name is kinda misleading
Well I guess we won't touch that but we need to at least restructure all your vertical code, a lot
yspd = 0;
grav = 0.09;
Let's add two variables like this to the create event. I'm not gonna reuse any of your variable names so that it's clear that these are distinct
yspd will track how fast you're currently moving vertically, and grav will be a constant that is always pulling us down
It's important to note that gravity is ALWAYS pulling on us, even when we're standing on solid ground, this is just a true aspect of physics, and it helps to keep that in mind when programming for games
So that means we'll be adding (not subtracting) grav to yspd at all times
// apply gravity
yspd += grav;
// max falling speed
if (yspd > 10)
yspd = 10;
// this handles jumping
if (place_meeting(x, y + 1, Collision_tile)) {
if (keyboard_check_pressed(vk_up)) {
yspd = -PlayerJump;
}
}
// colliding with the ground
if (place_meeting(x, y + yspd, Collision_tile)) {
yspd = 0;
}
// actually move vertically
y += yspd;
Other little fixes to make, we should always be moving using our vertical speed, so y += yspd; at the end makes sense. Note that we are always adding, everywhere, there is no subtraction anywhere here. In fact the only mention of a negative number is yspd = -PlayerJump; when we jump, to give us some upward speed
sorry that i went offline
thank you, ill try to remake my code with the advice you've given me.