unity – Calculating velocity to succeed in the leap top whereas the participant presses the leap button

unity – Calculating velocity to succeed in the leap top whereas the participant presses the leap button

[ad_1]

The peak of the leap is dependent upon how lengthy the participant presses the leap button. There are solely two parameters – top and time. Gravity is fixed.

As an instance that the peak has three items, and the time is about to a second.

There are mounted time-segments throughout which velocity have to be added. If the participant presses the button for the whole second, they’ll attain the three items top, taking into consideration the truth that gravity is pulling them down.

After I tried to unravel this downside, I used to be making an attempt to make use of
v = sqrt(2gH) formulation which was good to calculate instantaneous velocity for fixed-height jumps.

Not for this case! My code could seem like this:

non-public float jumpHeight = 3f;
non-public float jumpTime = 1f;

non-public const float GRAVITY = -10f;

...

non-public float timer = jumpTime;

public void FixedUpdate(float fixedDeltaTime) {
    if(timer < 0) {
        return;
    }
    timer -= fixedDeltaTime;
    
    var deltaTimeNormalized = fixedDeltaTime / jumpTime;
    var deltaHeight = jumpHeight * deltaTimeNormalized;
    var jumpVelocityY = CalculateJumpSpeed(deltaHeight, Math.Abs(GRAVITY));

    character.rigidbody2D.velocity.y += jumpVelocityY;
}

non-public float CalculateJumpSpeed(float top, float gravity) {
    return Math.Sqrt(2 * top * gravity);
}

The managed character skyrockets, and I do not fairly perceive the rationale for this habits.

I clearly see that the pace is gaining quickly, however I can not perceive why the unique concept doesn’t work, and what different options there is perhaps that will take the identical two parameters.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply