The Ball jump also upon the nothing

How i can fix this ?

-------------- SCRIPT ------------------------

var jumpDelay : boolean;
    var doubleJump : int = 0;
    function Update()
    {
    if( Input.GetKeyDown(KeyCode.Space) && jumpDelay == false)
    {
    Jump();
    }
    }
 
    function Jump()
    {
    if (doubleJump <= 1)
    {
    rigidbody.velocity.y = 10;
    jumpTimer();
    }
    }
 
    function jumpTimer()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    doubleJump ++;
    }
 
    if (doubleJump > 1)
    {
    doubleJump = 0;
    jumpDelay = true;
    yield WaitForSeconds(3);
    jumpDelay = false;
    }
    }

You should tell what you want. Nobody can help you if they don't know what the problem is. Do you want the ball to jump also when it's not on the ground, or is that what it's doing currently and you don't want that? What is happening now and what is wrong with it and how do you want to change it?

I want that The ball must jump only on the terrain , the ball must not jump when is not on a ground

2 Answers

2

if you’re wanting to let your self only jump when on ground.

do this

(it’s in C#, but should be easy to convert to Unityscript)

public bool canJump = true;

and then for your jumping put.

if (Input.GetKeyDown(KeyCode.Space) && canJump == true){
//YOURCODE
}

void OnCollisionStay(Collision col){
 if(col.gameObject.tag == "WhateverYouWant"){
canJump = true;
}

void OnCollisionExit(Collision col){
canJump = false;
}

Like I said, not hard to convert it, but it works!

Try performing a raycast from inside the ball that goes downwards until it just barely comes out the bottom. If the raycast returns true, then the ball is “grounded.” Otherwise, the ball is assumed to be in the air.

First, you can create a float variable to store the distance from the ball to the ground. (See the code below.)

var distToGround : float;

Next, in the Start() function, you can assign the distToGround variable your ball’s height, plus an extra 0.1 “units”. The extra 0.1 will come in handy in the next step. (See the code below.)

function Start()
{
   distToGround = collider.bounds.extents.y + 0.1f;
}

Finally, you can write a short function that performs a raycast downwards from inside the ball, and with a length that is slightly more than the ball’s height (that’s where the 0.1 comes into play!).

If the ray hits something, the function will return true and the ball will be considered grounded. Otherwise, it will return false and the ball will be considered airborne. (See the code below.)

function isGrounded() : bool
{
	return Physics.Raycast(transform.position, Vector3.down, distToGround);
}

The rest is easy. When the “jump” key/button is pressed, call the isGrounded function. If it returns true, make the ball jump. Otherwise, do nothing!

I hope this helps. Best of luck!