Begginer Jumping javascrpit problem.

If i spam the jump button (w) my ball jumps the first time then when it lands it does not jump right away but it jumps randomly after a number of button presses.

#pragma strict

var rotationSpeed = 100;
var jumpHeight = 8;

private var isFalling = false;

function Update ()
{
    //Handle ball rotation.
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotation);
   
    if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    {
        rigidbody.velocity.y = jumpHeight;
    }
    isFalling = true;
}

function OnCollisionStay ()
{
    isFalling = false;
}

I head this was a arithmetic behavior problem in java script so i tried in c# but its not working.

public class BallControl : MonoBehaviour
{
public float rotationSpeed = 100;
public float jumpHeight = 8;

private bool isFalling = false;



void Update ()
{
float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
rigidbody.AddRelativeTorque(Vector3.back * rotation);   

if(Input.GetKey("space") && isFalling == false)
{
isFalling = true;
Vector3 jump = gameObject.rigidbody.velocity;
jump.y = jumpHeight;
gameObject.rigidbody.velocity = jump;

}



}

public void OnCollisionStay()
{
isFalling = false;
}
}

I am a very beginner at programming in JavaScript, c# and this is the first game/project im making in unity. Help would be greatly appreciated.

you are calling the collision function incorrectly… there isn’t a unity engine function called “OnCollisionStay” that takes no arguments.

It always needs to have the argument of type collision.

What you have done here is overload the unity engine function with your own definition (i.e. one without parameters) but the unity engine is never going to call that. What you want to do is override the unity engine function with your own code.

The code was given to me by someone else. I have very little experience programming. I’m understanding the concept of what your saying but i have no idea how to put it into code.

Don’t worry about that. OnCollisionStay work also.

Add OnCollisionExit(){isFalling=true;}

#pragma strict

var rotationSpeed = 100;
var jumpHeight = 8;

private var isFalling = false;

function Update ()
{
    //Handle ball rotation.
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotation);
   
    if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    {
        rigidbody.velocity.y = jumpHeight;
    }
    isFalling = true;
}

function OnCollisionStay ()
{
    isFalling = false;
}

function OnCollisionExit()
{
isFalling=true;
}

Like this?
It still gives me the same problem.

Bump. Still in need of help.