CharacterMotor - how can I keep the Jumping go higher until jumpkey is released?

Hi,

I would like to ask for advice on this please, I have been searching the web etc but so far not come up with anything similar (maybe my searches are not good enough?)

I’m using Unity 5.1 and my player has CharacterMotor and the FPSInputController scripts attached, It all works well but I’d like to have the option where if I hold the Jump key down, when that happens then the character should just keep going up until the jump key is released… as if I have a jetpack which can make the player fly up high… I have tried going through these scripts but am not sure how to modify this for my needs. Can anyone give me a clue on where to start looking, or how to change it?

I think (of course I might be wrong) but the jump code is controlled in CharacterMotor.js , specifically this piece of code:

``````private function ApplyGravityAndJumping (velocity : Vector3) {

if (!inputJump || !canControl) {
jumping.holdingJumpButton = false;
jumping.lastButtonDownTime = -100;
}

if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
jumping.lastButtonDownTime = Time.time;

if (grounded)
velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
else {
velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;

// When jumping up we don't apply gravity for some time when the user is holding the jump button.
// This gives more control over jump height by pressing the button longer.
if (jumping.jumping && jumping.holdingJumpButton) {
// Calculate the duration that the extra jump force should have effect.
// If we're still less than that duration after the jumping time, apply the force.

//
if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
// Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
}
}

// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
}

if (grounded) {
// Jump only if the jump button was pressed down in the last 0.2 seconds.
// We use this check instead of checking if it's pressed down right now
// because players will often try to jump in the exact moment when hitting the ground after a jump
// and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
// it's confusing and it feels like the game is buggy.
if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
grounded = false;
jumping.jumping = true;
jumping.lastStartTime = Time.time;
jumping.lastButtonDownTime = -100;
jumping.holdingJumpButton = true;

// Calculate the jumping direction
if (TooSteep())
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
else
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);

// Apply the jumping force to the velocity. Cancel any vertical velocity first.
velocity.y = 0;
velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);

// Apply inertia from platform
if (movingPlatform.enabled &&
(movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
) {
movement.frameVelocity = movingPlatform.platformVelocity;
velocity += movingPlatform.platformVelocity;
}

}
else {
jumping.holdingJumpButton = false;
}
}

return velocity;
}
``````

I could not see how to format the code in my post here , apologies for that.
I tried changing various parts in this code e.g. remove “Time.time < jumping.lastStartTime” but nothing helps me so far.
Any help would be much appreciated.

Use code tags.

Many thanks hippocoder, found the option after a bit more searching.

I have to point out to people to use code tags because it dramatically increases the chance that an experienced programmer will look at it.

Agreed Thanks for helping.

I would like to give this a bump up.