longer jump when holding space

hi
I use FPSwalker for my game… and I add some sort of sprinting… now I want that to add somthing to say that if player press SPACEBAR jump with jump speed … but when holding it player jump for jump speed + 5 … I read other placeses but I didn’t get it… could any one edit this for me??

var WalkSpeed = 6.0;
var RunSpeed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function FixedUpdate() {

	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		var mySpeed = WalkSpeed;

		if (Input.GetKey("left shift")) {
			mySpeed = RunSpeed;
		}
		moveDirection *= mySpeed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

To format code, highlight it and press 101-010 button (click Edit to change your question).

3 Answers

3

The jump button is only read when the character is grounded - but it’s no more grounded as soon as it starts the jump. If you move the jump button out of the if, it will behave like a jet-pack - the character will go up while the button is being pressed. When the button is released the character falls by action of gravity:

    ...
    if (grounded) {
        // We are grounded, so recalculate movedirection directly from axes
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        var mySpeed = WalkSpeed;

        if (Input.GetKey("left shift")) {
            mySpeed = RunSpeed;
        }
        moveDirection *= mySpeed;
        
    }
    if (Input.GetButton ("Jump")) { // jump will last while button pressed
        moveDirection.y = jumpSpeed;
    }
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
    ...

We’re not here to write your code for you but we’ll be happy to help you learn how to do it.

In your script you currently allow jump only while being grounded. One way to approach this would be to store the time at which we were last grounded then allow the jump button to add diminishing force if held each frame based on how long ago the button was pressed

I’m making this up as I go but you’d probably need a boolean wasGrounded to store whether we were grounded last frame. Then if we are grounded but we weren’t last frame, we set the lastGroundedAt value to the current Time.time. Then you can decide on how long the button can be held as a float extendedJumpTime and take Mathf.Min(1,Time.time-lastGroundedAt)/extendedJumpTime) to get a 0 to 1 value for how far through a jump you are. Using this you can apply any type of force interpolation function you want. Unity has a built in linear interpolation using Mathf.Lerp(jumpForce, 0, t) where jumpForce is the maximum jump power and t is the value for how far through a jump you are.

You will probably have to decrease your current jumpSpeed because originally you applied the force only during one frame while you were grounded and now it will be applied over several frames.

You make my day Rennat! Your solution works great for my rigidbody controler with powerJump function :)) Thanks so much, I'm so sorry for my low reputation prevent me to give You thumb up.

you can try this : when

if (mode == 1) {

		if (!ground && !secondJump) { 
			if (temJumptime > 0.25f) {
				GetComponent<Rigidbody2D> ().AddForce (Vector3.up * (speed / 3.0f), ForceMode2D.Force);
				secondJump = true;
			}
		} 
		if (Input.GetKey (KeyCode.Space)) {
			temJumptime += Time.deltaTime;
			print (temJumptime);
			if (ground) { 
				GetComponent<Rigidbody2D> ().AddForce (Vector3.up * speed, ForceMode2D.Force);
			}
			ground = false;
		} 
		if (Input.GetKeyUp (KeyCode.Space))
			secondJump = true; 
		
	}