#[HELP] Variable jump height for 2d game

1 messages · Page 1 of 1 (latest)

queen sapphire
#

i made this into a thread so we can go back to this message :)

unborn mural
#

Which line has the relevant topic?

queen sapphire
#

let me ping OP, I didn't work on that part of our project :-) @astral stream

astral stream
# unborn mural Which line has the relevant topic?

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
        }```
unborn mural
#

What's the unexpected behaviour?

astral stream
#

The commented physics calculations are because I moved them to a FixedUpdate method

astral stream
# unborn mural What's the unexpected behaviour?

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

sinful crescent
#

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!

unborn mural
#

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

astral stream
#

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?

unborn mural
#

Might be your best bet given the amount of variables that are at play here

astral stream
#

I will give that a try then

unborn mural
#

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

astral stream
#

Thank you, we were not taught these

unborn mural
astral stream
#

Yep

unborn mural
#

That's a bit dissapointing

#

We weren't thought them either though

#

Even though they definitely should be

astral stream
#

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

astral stream
#

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)

astral stream
#

Here

unborn mural
#

That always gets triggered

#

It will always check the if

astral stream
#

Right, thats the check

#

makes sense, sorry

unborn mural
#

Which does not mean it'll pass or not

#

So put it one line below the if

unborn mural
astral stream
#

I've attached the breaking point to the bool that's within

unborn mural
#

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

astral stream
#

oh thats really helpful

unborn mural
#

Don't place a breakpoint for every line, you can just do that with the other arrows already

#

this one specifically

astral stream
#

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

unborn mural
#

Also if it goes too deep you can press this to step out of whatever function you're in

unborn mural
#

You know your system better than I do, to know if that's expected behaviour or unexpected behaviour

astral stream
#

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?

unborn mural
#

Sure you can do that

astral stream
#

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?

unborn mural
#

Could you indicate which lines these things are occurring on?

astral stream
#

Yes of course

unborn mural
#

(Your script is quite lengthy ;P)

astral stream
#

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

astral stream
unborn mural
#

You should try using more functions

#

Functions in functions ;P

astral stream
#

Funcception, I'll definitely have to look into that

astral stream
#

sorry, it's never triggered

unborn mural
#

Then it's not getting past the if

astral stream
#

Yeah

#

I'm not really sure why, because it should read the getbuttonup properly, no?

#

or is it too fast?

unborn mural
#

I don't use GetButtonUp, so I'm not sure

#

Is it keyboard or controller?

astral stream
#

It's a keyboard. Is there a better function for that?

unborn mural
#

Could you try Input.GetKeyUp(KeyCode.Space) for me?

#

(within your if)

astral stream
#

Of course, one second

unborn mural
astral stream
#

should I change the GetButtonDown in line 118 to GetKeyDown as well?

unborn mural
#

No, just test if this works

astral stream
#

okay

unborn mural
#

Also check if anything within CharaJumping ever gets called?

#

brb

astral stream
#

So no luck with GetKeyDown

astral stream
#

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

astral stream
#

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?

unborn mural
#

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

astral stream
astral stream
sinful crescent
#

All credits go to Wumpie 🙂 !

#

Its amazing to see the Unity community coming together like this and help each other out.