Constrain Total Number of Jumps

Hi,

I am curious how you can limit the total number of jumps in a game. I currently only want one jump, but later might implement a double jump. More to the point I would like to base jumping on collisions. That is to say, if the hero jumps and hits a wall, the jumpCounter should reset back to zero allowing the hero to start jumping again.

Here is a snippet:

var isFalling : boolean=true;  

function Update () {
    var fingerCount = 0;
    for (var touch : Touch in Input.touches) {
         if (touch.phase == TouchPhase.Began)
            fingerCount ++;
    }
    if (fingerCount > 0)
		rigidbody.velocity.y=7;
	}
		
function OnCollisionStay(collisionInfo : Collision) {
	isFalling=false;

}

Thank you

I would have a jumping flag: only jump when it’s false and nJump < maxJumps. When jumping, set the jumping flag. In OnCollisionStay, clear jumping and count the jump only when was jumping and landed on the ground:

var jumping = true;
var maxJumps = 1;
var nJump = 0;  

function Update () {
    var fingerCount = 0;
    for (var touch : Touch in Input.touches) {
         if (touch.phase == TouchPhase.Began)
            fingerCount ++;
    }
    // only jumps if not jumping yet and if maxJumps not exceeded
    if (fingerCount > 0 && !jumping && nJump < maxJumps){
       rigidbody.velocity.y = 7;
       jumping = true; // is jumping now
    }
}

function OnCollisionStay(collisionInfo : Collision) {
    // if was jumping and landed in some Ground object...
    if (jumping && collisionInfo.gameObject.CompareTag("Ground")){
        jumping = false; // jump ended
        nJump++; // count the jump
    }
}