im using the y velocity of my rigidbody to perform a double jump action. The problem is that when i press the jump button for the 2nd time on the highest point for the first jump i expect the y velocity to be higher. But it works the complete opposite, the y velocity goes down. And when i press the jump button for the 2nd time before it reaches its highest point the y velocity goes up and it jumps really high???
This is what I’m using for my jumping, works pretty well for me. A raycast uses a distance that is found at the begining of the code to see when the player is grounded. From then on, it will send the raycast out and if it is infact grounded, the jumped variables are set to false. You can set the two jump speed variables to your liking. This code should work with copy and pasting, I took it almost exactly out of my scripts. Hope it helps.
var jumpSpeed : float = 8.0; //First jump speed
var doubleJumpSpeed : float = 12.0; //Second jump speed
private var distToGround: float;
private var Jumped1 = false;
private var Jumped2 = false;
function Start () {
// Get the distance to ground
distToGround = collider.bounds.extents.y;
}
//Raycast to see if grounded
function IsGrounded(): boolean {
return Physics.Raycast(transform.position, - Vector3.up, distToGround + 0.1);
}
function Update() {
//Check if grounded (if so, reset jumps)
if(IsGrounded()){
Jumped1 = false; Jumped2 = false;
}
//Jump inputs
if (Input.GetButtonDown ("Jump") !Jumped1) {
Jumped1 = true;
rigidbody.velocity.y = jumpSpeed;
}
//Double Jump
else if (Input.GetButtonDown ("Jump") !Jumped2) {
Jumped2 = true;
rigidbody.velocity.y = doubleJumpSpeed;
}
}