Need to control a 2D character to jump only once

I’m pretty new to Unity so I want to ask something. I try to use the javascript to allow the character to jump only once, here’s my code

private var CheckOnGround:boolean = false;


function Update () 
{
	if (Input.GetKeyDown("space") && CheckOnGround == true)
	{
		Jump();
		CheckOnGround = false;
	}

}

public var JumpSpeed:float = 100.0f;

function Jump()
{
	CheckOnGround = false;
    rigidbody2D.AddForce(Vector2.up *JumpSpeed);
}

function OnCollisionEnter2D(coll: Collision2D) {
	if(coll.gameObject.tag == "Ground")
	{
		CheckOnGround = true;
	}
	
	
}

So it should have turn the value of CheckOnGround to false whenever the character is in the air, but for some reason when I test it the character are able to double jump instead, only after the 2nd jump that it cant jump anymore. Any help on this?

Hi, Maybe this code will help you.

var jump : boolean = true; //true = Player can Jump, false = Player can't Jump
var jumpspeed : int = 8; //This is how high you can jump


function Update () {

if (Input.GetKey (KeyCode.UpArrow))  
{
if (jump == true) 
{
rigidbody2D.velocity.y = jumpspeed;
jump = false;
}

}

}


function OnCollisionEnter2D(other : Collision2D)
{

if(other.gameObject.tag == "Ground") 
{
jump = true;
}

}

Give your land a tag name Ground

Hope this help.

I also post videos, for Unity 3D and 2D
Here the Link
link text

its working fine but i have to press the key two times to jump after landing.

void Update () {
	if (Input.GetKeyDown (KeyCode.A)) {
		if (jump == true) {
			rigid.AddForce (new Vector2 (0, power));
			jump = false;
		} else {
			jump = true;
		}
	}
}
void OnCollisionEnter2d(Collision2D other){
	if (other.gameObject.tag == "Finish") {
		jump = false;
	}
}

Hi,

My first time here. Let’s see how it goes :slight_smile:

My character jumps once controlling if he’s touching “ground”. It works from the floor.

I have more platforms above and they are also “ground” because I want to jump from them as well.

BUT when I jump from the floor bellow and touch the platforms above with the character’s head, he can jump again even if he’s still not touching the floor.

Any advice?

Thanks!