Glad to see you fixed the issue. In problems like these I like to really try to understand what the code is doing on a deeper level so I can fix the issue in the best way possible. Excited to see more of your work and progress!
As Yrekcaz7 said, it's a good idea to set up print commands for debugging purposes. To track what's going on with the jumping mechanic, I'd place a couple underneath your problematic if statement, like so (bold lines are my suggestions here): if "space" in pressed_keys and on_ground: velocity_y -= jump_force *print("Space pressed.")* on_ground = False *print("on_ground is ", on_ground)* This way, the console will tell you when you've pressed Space and when on_ground is set to False. Wherever your code sets on_ground to True again, you'll want a similar print command right underneath to tell you - *print("on_ground is ", on_ground)* - right after. If, after you've set this up, the console never tells you that on_ground has been set to True (or it happens when you don't expect it), that should give you a better idea of what's actually going on in your code. In case you'd rather figure it out yourself with this clue, I'll put my ideas in a reply to this comment.
Okay, so here's what I think is going on with your code. Every frame, Python is running your physics code. When you press Space, your if statement is verified as True (as on_ground is also True, we assume), so this runs: velocity_y -= jump_force on_ground = False Your physics are applied, but only for one frame - on the very same frame, on_ground is set to False. From there, the gravity code is applied, but here's the crucial part: your if statement no longer verifies to True, because on_ground is False. No matter what happens there, your jump will only ever happen for one frame. To fix this, I suggest using a new boolean variable to track when your character's jumping - call it something like is_jumping and initialise it as False. You don't want your jump logic to be totally dependent on being on the ground, but your character should only be able to start a jump if on_ground is True. When the player presses Space, you set is_jumping to True (right above on_ground = False), but handle the actual jumping code outside of that if statement. The character's jump should depend on the player holding Space and on a timer to limit how long they can remain in the air. I'd suggest using a While loop for that.
Disclaimer everyone who goes frantically writing in the comments their fixes. I am very grateful for all of you, but I've managed to divide my own fix. Therefore, save your knowledge for some one else.
What was that one line of code you removed? Also, are you sure the `on_ground` variable gets set to true when touching the ground? a good way to test that would be to add some sort of temporary "print()" that just spams whether or not `on_ground` is true or false
Yeah, III didn't I never really thought to actually do that debug and. I managed to fix the error anyways. Turns out in the part where it was checking for the collision thing I added. in as an if else statement. Well, a else statement in order to wear is this condition didn't go right then just said it to true and I tweaked to the thing a little bit because now I was touching a Phantom blocks and I made the Y - 39 and now everything's working just fine. I also tweaked up how the physics works. You don't really notice it. I don't really notice it either but I the physics has changed.
Glad to see you fixed the issue. In problems like these I like to really try to understand what the code is doing on a deeper level so I can fix the issue in the best way possible. Excited to see more of your work and progress!
As Yrekcaz7 said, it's a good idea to set up print commands for debugging purposes. To track what's going on with the jumping mechanic, I'd place a couple underneath your problematic if statement, like so (bold lines are my suggestions here):
if "space" in pressed_keys and on_ground:
velocity_y -= jump_force
*print("Space pressed.")*
on_ground = False
*print("on_ground is ", on_ground)*
This way, the console will tell you when you've pressed Space and when on_ground is set to False. Wherever your code sets on_ground to True again, you'll want a similar print command right underneath to tell you - *print("on_ground is ", on_ground)* - right after. If, after you've set this up, the console never tells you that on_ground has been set to True (or it happens when you don't expect it), that should give you a better idea of what's actually going on in your code. In case you'd rather figure it out yourself with this clue, I'll put my ideas in a reply to this comment.
Okay, so here's what I think is going on with your code. Every frame, Python is running your physics code. When you press Space, your if statement is verified as True (as on_ground is also True, we assume), so this runs:
velocity_y -= jump_force
on_ground = False
Your physics are applied, but only for one frame - on the very same frame, on_ground is set to False. From there, the gravity code is applied, but here's the crucial part: your if statement no longer verifies to True, because on_ground is False. No matter what happens there, your jump will only ever happen for one frame.
To fix this, I suggest using a new boolean variable to track when your character's jumping - call it something like is_jumping and initialise it as False. You don't want your jump logic to be totally dependent on being on the ground, but your character should only be able to start a jump if on_ground is True. When the player presses Space, you set is_jumping to True (right above on_ground = False), but handle the actual jumping code outside of that if statement. The character's jump should depend on the player holding Space and on a timer to limit how long they can remain in the air. I'd suggest using a While loop for that.
@@hostiusasinhostilityhostil7853 Thank you. But I already had my own fix, but thank you for the help
Disclaimer everyone who goes frantically writing in the comments their fixes. I am very grateful for all of you, but I've managed to divide my own fix. Therefore, save your knowledge for some one else.
What was that one line of code you removed? Also, are you sure the `on_ground` variable gets set to true when touching the ground? a good way to test that would be to add some sort of temporary "print()" that just spams whether or not `on_ground` is true or false
Yeah, III didn't I never really thought to actually do that debug and. I managed to fix the error anyways. Turns out in the part where it was checking for the collision thing I added. in as an if else statement. Well, a else statement in order to wear is this condition didn't go right then just said it to true and I tweaked to the thing a little bit because now I was touching a Phantom blocks and I made the Y - 39 and now everything's working just fine. I also tweaked up how the physics works. You don't really notice it. I don't really notice it either but I the physics has changed.