#[HELP] Variable jump height for 2d game
1 messages · Page 1 of 1 (latest)
Which line has the relevant topic?
let me ping OP, I didn't work on that part of our project :-) @astral stream
Hello. I believe that the relevant lines would be in the Jumping method.
if (GroundCheck.grounded) //This checks if the player is inputting a jump
{
Debug.Log("Jumped");
Jumping = true;
//ProtagPhysx.velocity = new Vector2(ProtagPhysx.velocity.x, JumpStr); //This transitions to the Jumping state
}
if (Input.GetButtonUp("Jump")) //This line detects if the player has let go of the jump button, shortening the jump
{
ShortStop = true;
Debug.Log("Shortstopped");
//ProtagPhysx.velocity = new Vector2(ProtagPhysx.velocity.x, 0); //By setting the velocity in y to 0, we effectively halt vertical movement upward
}```
What's the unexpected behaviour?
The commented physics calculations are because I moved them to a FixedUpdate method
Well, theoretically what should happen is the character goes from idling to jumping state when space (or whatever jump button is set) is pressed, jumps however high while space is still pressed, and then the player can regulate the jump height by releasing space sooner or later after having pressed it
as it stands, the only thing it does is jump a fixed height all of the time
no matter how fast you let go
The implementation was taken from this video in particular https://www.youtube.com/watch?v=Mo1-sKYbks0
In this short unity tutorial we will add variable height jump to our platform movement. The feature is well known from many games like Celeste, Hollow Knight and many more.
Got any questions? Join our discord:
https://discord.gg/hrZF93sCkc
Try to debug to see what's causing the issue. I guess the easiest thing to do for now is to simply use debug.log to see what's happening. And, also print the current state so you know in which state it currently is. If it's in a state which you don't expect, you know more where the error is.
Remember you can pause the game and go frame by frame to exactly know what's happening!
To fill onto Xitech's comment, breakpoints can be quite useful in the debugging proces as well
Assuming you use VS, this should be relatively easy to use
Thank you for the suggestion. When setting up the state machine and the transitions, I made sure to debug every state to make sure it was in the correct one. This seemingly worked. When I press space, it switches from idle to jumping or from running to jumping without any issue. I also set it to print "jumping" once it is inside the Jumping.State and applying the physics. What doesn't seem to print, however, is "Shortstop", which I set to print on ButtonUp (which is when velocity in y is supposed to be set to 0 to control the jump). This makes me believe that for some reason it is not properly detecting the ButtonUp
I have implemented this code in a separate script without the state machine and it works as intended. So what I have come to is that it is an issue with the State Machine. How should I set up a debug to see if it is somehow affecting the ButtonUp?
^
Might be your best bet given the amount of variables that are at play here
I will give that a try then
Logging the State will help a lot as well
Also I'd try to clean some code
I'd start with having consistent naming
Adhere to the C# code conventions
In this case everything here should be camelCase
Thank you, we were not taught these
By your teachers?
Yep
That's a bit dissapointing
We weren't thought them either though
Even though they definitely should be
I'll say that my teacher isn't exactly good. His credentials seem to be that he's made a few games, but he gets most of his teaching material online
I will clean it up though, thank you
That's rough
On the script side of things, I put a breakpoint on the if that controls whether or not the jump button is being released, and somehow it seems to activate when transitioning from idle (then again I may be doing this wrong)
Screenshot of your VS?
No worries
I've attached the breaking point to the bool that's within
You can place more than 1 breakpoint btw
And it'l just trigger in the order of execution
Pressing continue will go to the next breakpoint if it's there, if it's triggered
oh thats really helpful
Though you'd use the step in to follow allong the events
Don't place a breakpoint for every line, you can just do that with the other arrows already
this one specifically
Thanks!
I have placed a breakpoint when it changes states from idle to jump, and within jump when it sets the jump bool to true and the shortstop bool to true
Also if it goes too deep you can press this to step out of whatever function you're in
I don't know if that's bad or good
You know your system better than I do, to know if that's expected behaviour or unexpected behaviour
Yeah, that's the expected behavior. I was trying to see if I could trigger the shortstop (which stops the character from going any higher when releasing the jump button) and it never seemed to trigger
So I guess the next step is to see what it does when transitioning from idle to jumping, right?
Sure you can do that
okay, so what seems to happen is that it goes from the state change immediately into the jump (this is good, because I want it to jump) but it completely ignores the getbuttonup check
is there any reason why that would happen?
Could you indicate which lines these things are occurring on?
Yes of course
(Your script is quite lengthy ;P)
The state change from idle to jumping occurs on line 120, the jump occurs in line 164 and the shortstop is supposed to be triggered in line 169, when the player releases the jump button
yeah, my bad. It doesn't help that I separated the physics calculations into a FixedUpdate. I still don't really know much about optimizing
Funcception, I'll definitely have to look into that
so 169 is never triggered?
Then it's not getting past the if
Yeah
I'm not really sure why, because it should read the getbuttonup properly, no?
or is it too fast?
It's a keyboard. Is there a better function for that?
Of course, one second
Oh by the way I don't know what Jump is bound to by default, but that line of code binds it to spacebar
should I change the GetButtonDown in line 118 to GetKeyDown as well?
No, just test if this works
okay
So no luck with GetKeyDown
checking this
Nope
I have a theory that it is almost immediately switching to the AmFalling state after executing the jump command
Oh boy
I'm a bit of an idiot. I have it set so that it changes to falling when it isn't grounded, instead of when it isn't grounded AND the y velocity is negative. Line 187
I'm going to change that and see if it works. If it does I'm gonna feel really silly
ah well, I'm feeling really silly but at least it works now
It was the fact that it transitioned instantly into the falling state because of this
God, I've been raking my brain for the past two days.
Thank you @unborn mural for the help. If it hadn't been for the breakstops I'm not sure how long it would've taken me to notice that it wasn't actually calling any of the other ifs within CharaJumping. Shows the importance of debugging.
And thank you also @sinful crescent
Btw, in terms of optimizing, is it a good idea to have all the physics calculations in the fixedupdate or should I put them in functions too?
:)
Believe me, you're going to feel like an idiot all the time in code, it never goes away
Speaking from experience ;)
Sorry for not responding, was doing some stuff for my boss
Absolutely no need to apologize :)
And I bet I am. What a road awaits lmao