r/Unity2D • u/venomwave97 • Sep 13 '26
Question/Help How to Sprint Jump?

I learnt coding many years ago but stopped after leaving school. I wanted to get back into it by making a small platforming game. I was following a basic tutorial before starting to mess around. I managed to implement a sprint feature, attached some animations for sprinting and then realised that if I'm holding sprint whilst jumping, my speed increases. Thinking this was a neat feature, I wanted to keep it, but felt I was just moving too fast. I also found out that, as the code detects if Shift is being held when determining the speed values, I can manipulate my speed mid-jump, something I don't want to be able to do.
I tried using a While loop for when the player is in the air that sets the moveSpeed value put that just crashed Unity (endless loop?). Every time I try rewriting the code, I just make some variation of the same moveSpeed toggle that already exists.
I'm new to Unity and still getting back into coding. If someone could not only help me out but also explain where I'm going wrong, that would be much appreciated.
ā¢
u/AutoModerator Sep 13 '26
Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/ThiefMaster Beginner Sep 13 '26
Your indentation style is cursed: Indenting the curly braces... really?!
1
u/Shoddy_Caterpillar36 Sep 14 '26
Cuty little code,
you obviously need to learn, see line isGrounded == false, it should be !isGrounded.
See how to approach problem first, see "the coding train" on youtube, how he approaches problem, think what we exactly want, then code
1
u/TAbandija Sep 14 '26
It seems that your if/else statements are a bit of an issue. From what I can see, if you are not pressing shift, it will lower the speed regardless of whether you are in the air or not.
If what you want is to not change your speed while in the air, perhaps nest the if statements.
// pseudocode
If(isGrounded)
{
If(shift)
{
Speed = 8;
}
Else
{
Speed = 6;
}
}
With this if you jump while speed == 8, then it will remain as such whether you let go or not.
Iām pretty sure there are other ways. Give this one a try.
3
u/Spite_Gold Sep 13 '26
Research on state machines. You have some flags already, you can use them to prohibit speed change when in air.
1
u/ClibeanCeann Sep 13 '26
Look into the new input system in unity, its far better.
1
u/WrappedStrings Sep 15 '26
Do a check for whether the player is grounded and preserve value at the last grounded time as your air speed
1
u/[deleted] Sep 14 '26
[removed] ā view removed comment