Boolean switch to control jump

I’m trying to get this function to only happen when player has landed. Also to reset when player has jumped.

#pragma strict

public var jumpForwardVel : float = 5f;

public var jumpUpVel : float = 20f;

//var Gravity : float = 10f;

var chargeLevel : float = 0; 

//Don't change this in the inspector.

var chargeSpeed : float = 1; 

//Default, the charge will go up 1 per second

var canJump = boolen(false);
//Is player grounded.


function Update () {
	

	
     if (Input.GetKey(KeyCode.Space)) { //Is the user Clicking?
          chargeLevel += Time.deltaTime * chargeSpeed; // Increase charge
         
     }
     
     if (chargeLevel > 2) {
      this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
          chargeLevel = 0;
          }
     
     if (Input.GetKeyUp(KeyCode.Space)) { // Did the player just stop clicking?
          // do the actual jumping here-
          // this takes the actual charge level into account-
          this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
          // now reset the charge level!
          chargeLevel = 0;
     }
     
     if (canJump){
     OnCollisionEnter : boolean = true;
     }
     
     
}

See this line?

var canJump = boolen(false);

That isn’t really a thing. You probably want

var canJump = false;

instead.

Delete that bit at the end of your Update function which goes-

if (canJump){
    OnCollisionEnter : boolean = true;
}

it doesn’t do what you think it does.

Now, to do the moded jumping that you want, you should split the bit that does the jumping off into a different function, like this-

function DoJump()
{
 if (Input.GetKey(KeyCode.Space)) { //Is the user Clicking?
      chargeLevel += Time.deltaTime * chargeSpeed; // Increase charge
 }
 if (chargeLevel > 2) {
  this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
      chargeLevel = 0;
  }
 if (Input.GetKeyUp(KeyCode.Space)) { // Did the player just stop clicking?
      // do the actual jumping here-
      // this takes the actual charge level into account-
      this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
      // now reset the charge level!
      chargeLevel = 0;

      // Having jumped, we now prevent the player from jumping again until they hit the ground-
      canJump = false;
 }
}

then, in your update function, where you had all of that, instead just have this one if statement-

function Update9)
{
    if(canJump)
    {
        DoJump();
    }
}

Now, make a new function called ‘OnCollisionEnter’. This will get called whenever the physics engine detects a collision involving that object.

function OnCollisionEnter(var collision : Collision)
{
    canJump = true;
}

Of course, this will also reset the jump counter if your player hits a wall, or something else in midair. You should put all your floors on a layer called ‘Ground’, and then put a check into the OnCollisionEnter, like so-

function OnCollisionEnter(var collision : Collision)
{
    if(collision.gameObject.layer == LayerMask.NameToLayer("Ground"))
    {
        canJump = true;
    } else if (!canJump) {
        // we must have hit something in mid-air!
        Debug.Log("Ooof!");
    }
}