How can I reduce the duration of an action?

I am begining writing script for a side-scrolling game. But now I have some problem with gameplay. In design document, the player can jump with the height they would like by holding the space until they get the ApexHeight, or holding it for a shorter time to jump a shorter height. But I don’t know how to do that. I can only make player get the ApexHeight.
This is my code for Jumping:

var velocity : Vector3 = Vector3.zero;
// Update function
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
 velocity = Vector3(0,0,Input.GetAxis("Vertical"));
 if (Input.GetButton("Jump"))
 {
	velocity.y = Character.collider.bounds.size.y*3;
 }
}

Thank you for reading.

But I watched a couple of Megaman clips. No matter how high he jumps, there is always a small bit of deceleration at the top. That is, he doesn’t stop immediately. As I look at your code above, your code is inside a ‘controller.isGrounded’ which means it will do nothings once the character leave the ground. In addition, since you are using GetButton(0 which returns true every frame), he will immediately jump again the moment he hits the ground. I going to rough in some code that takes what you currently have and moves it towards what I saw in the video:

At the top of the file add some new variables:

private var jumpVelocity = 5.0;
private var isJumping = false;
private var maxJumpProcess = 4.5;
private var jumpBase : float;

Replace Lines 7 - 10 with the following

if (Input.GetButtonDown("Jump")) {
    velocity.y = jumpVelocity;
    jumpBase = transform.position.y;
    isJumping = true;
}

Outside of the ‘controller.isGrounded’ check but before you calculate gravity add the following:

if (isJumping && Input.GetButton("Jump")) {
    if (transform.position - jumpBase >= maxJumpProcess) {
        isJumping = false;
    }
    else {
        velocity.y = jumpVelocity;
    }
}

if (Input.GetButton("Jump")) {
    isJumping = true;
}

A few notes:

  • ‘jumpVelocity’ can be calculated in Start() using the formula you do now. But I you use height to make the calculation and it will only work to that specific height if you setup gravity the exact right way…and that setting may not match the feel of Megaman.
  • ‘maxJumpProcess’ is the maximum height from the start of the jump that you will continue to preserve the velocity. The character will go some beyond this point. How far will depend on both ‘jumpVeloity’ and how you calculate gravity.
  • The code works by preserving ‘jumpVelocity’ (i.e. not allowing gravity to decay it) as long as the button is held down and the jump has not reached ‘maxJumpProcess’ in height above ‘jumpBase’.