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?
is the floor set to "Ground" and does it have a collider ?
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
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.
is the floor set to "Ground" and does it have a collider ?
– Klarax