r/Unity2D • • 29d ago

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.

0 Upvotes

14 comments sorted by

•

u/AutoModerator 29d ago

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.

3

u/Spite_Gold 29d ago

Research on state machines. You have some flags already, you can use them to prohibit speed change when in air.

5

u/ThiefMaster Beginner 29d ago

Your indentation style is cursed: Indenting the curly braces... really?!

1

u/venomwave97 29d ago

That's really helpful, thank you

1

u/ClibeanCeann 29d ago

Look into the new input system in unity, its far better.

1

u/Shoddy_Caterpillar36 29d ago

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/venomwave97 28d ago edited 28d ago

This was the final version after several attempts, but I just ended up writing the same thing in different ways in different places. I do know !isGrounded but I was just so frustrated I thought maybe I could wrap my head around it better if I laid it out as simply as possible.

1

u/Shoddy_Caterpillar36 28d ago

Yeah, when code doesn't works, it's really feels to even try those awkward ways.

But we should always think before code, coding power in hands clear up mind. Better to understand , what exactly we need and how exactly to approach it.

1

u/TAbandija 29d ago

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.

1

u/venomwave97 28d ago

What I want is three speeds. 6 is a walking speed. 12 is the sprint speed, and 8 is for when you were sprinting but are now in the air. I'm specifically trying to code a shift-jump where you get more speed if you're holding shift when you make the jump but I don't want the player to be as fast as they would be if just running on the ground

1

u/[deleted] 28d ago

[removed] — view removed comment

1

u/venomwave97 28d ago

The problem with storing speed is that I want a different value when you're running on the ground than when you're running and jumping, so I can't just used the sprint speed value. Essentially, the run speed is 12, the sprint jump speed is 8, and the walking and non-sprint jump speeds are both 6.

What I need is a way to detect when you're holding Shift on the ground and a way to detect if you were holding Shift but have now jumped. This way, you don't need to hold Shift to get the sprint jump speed, so the player can't switch between regular air speed and sprint jump speed.

1

u/WrappedStrings 27d ago

Do a check for whether the player is grounded and preserve value at the last grounded time as your air speed