Movement Speed While Jumping

Hello Everyone,

I have been trying to solve this issue for a while now and I cannot figure out how to do it.

I am trying to implement a walk speed and a run speed. I have been doing this by simply dividing deltaX by 2 when the designated run button is not being pressed.

HOWEVER

The player can freely switch between these two (half speed range & full speed range) while they are jumping creating some very wild jump arches. I want for people to still be able to redirect themselves mid-jump but not suddenly accelerate above the speed at which they started the jump from.

Right now I can make it so the jump arch is completely set from the moment the player stops touching the ground, but like I just mentioned, I want them to still be able to redirect the jump, just not accelerate more (if they started the jump while in walk speed then pressed the jump button mid-jump) or suddenly decelerate (if they started the jump at running speed and then released the run button mid jump).

Any help would be appreciated!! Thank you in advance!

Hey rods012, I think what I would do is store the movement speed into a variable and make it so you can only manipulate it while on the ground and as soon as you jump it will be the last speed you were using while moving on the ground.

Hope that helps!

Hey Riley, I did attempt this, but that ends up locking the jump’s arch and no adjustments can be made while in the air. I still want the player to be able to turn left or right and adjust their angle, I guess I just don’t want more speed to be added to the equation in the form of pressing the run button while in the air.

Thank you for your thought though!

Yes that’s what I meant by that still give the player control in the air but they can only move left and right at the speed they left the ground at.

Maybe show me your code and I can show you what I mean?

So I have been working on this and I found the solution and it was so simple and stupid (maybe you have a cleaner way to do this)

private void Move()
{
    deltaX = Input.GetAxis("Horizontal");

    if (Input.GetButton("Fire3"))
    {
        if(myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            speedModifier = 1;
        }
    }
    else if(!Input.GetButton("Fire3")) 
    {
        if(myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            speedModifier = 1.5f;
        }  
    }

    myRigidbody.velocity = new Vector2((deltaX / speedModifier) * runSpeed, myRigidbody.velocity.y);

    RunAnimation();
}

I had been putting my conditions together “(Input.GetButton(“Fire3”) && myFeetCollider.IsTouchingLayers(LayerMask.GetMask(“Ground”)))”

and it was actually causing Unity to crash depending on some of the variations I tried. but this produces the exact result I wanted. Throughout the jump momentum is maintained and more speed cannot be gained while in the air.

I have only started coding with this course so all of this is very new to me. This might be a super rudimentary way of doing it, so if there are any suggestions I would truly appreciate them!

That’s exactly what I was trying to communicate, glad you got it solved! If it works then I don’t see any need to change the code. There’s a few ways you could write it:

This is just some psuedo code:

If(isGrounded)
{
If(runButtonPressed)
speedModifier = 1.5;
else
speedModifier = 1;
}

rigidBody.velocity = new Vector2(newVelocity);

1 Like

Many soluons for your problem. I suggest create enum State {stop, normal, accelerating, jump}
public State currentState;
public float accelSpeed, normalSpeed;
public float lastSpeed;

public float GetSpeed(){
float temp= normalSpeed;

switch(currentState){
case State.stop : return 0;
case State.accel : temp= accelSpeed; break;
case State.jump : temp= lastSpeed; break;}

lastSpeed = temp;
return temp;
}

and you need to handle states with your input. Do this part yourself.

For example
void Update(){
HandleState();
Move(GetSpeed());
}
Handle state is your own method where you decide depending on input whish current state it is now.

I suggest using states instead of speed mods simply because its readable, and will be easy to handle when you deal with animations.

Yea, that would be cleaner for sure! Thank you for the suggestion earlier too, I just wasn’t understanding what you were saying!

I will be completely honest, some of this goes over my head, I have never used “switch” or “case” and such so I am not exactly sure how that works (or “temp”). Seems like a good robust solution though :thinking:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms