var jumpH = 8;
var isFalling = false;
function Update () {
if(Input.GetKey(KeyCode.Space)&& isFalling == false)
{
GetComponent.<Rigidbody2D>().velocity.y = jumpH;
isFalling = true;
}
}
function OnCollisionStay () {
isFalling = false;
}
I’m using this script to make my character jump and it works…once. Why only once?
2 Answers
2
Since “isFalling” is initialized to false, and the only place you set it back to false is in the OnCollisionStay function, I guess the function is note being called.
Debug your code, add a Debug.Log line inside the OnCollisionStay function to make sure it is called. If it’s not called, make sure at least one of the objects colliding has a rigidbody, that they’re not marked as IsKinematic and that the layers of the objects are ticked in the collision matrix of the physics configuration.
Your “OnCollisionStay()” function is behaving exactly as it’s supposed to, and therein lies your problem.
You’re creating the function, rather than using the existing version from Unity’s API. Here is the correct implementation of it.
To be precise, the problem you’re encountering is that you are using:
function OnCollisionStay()
where you need to be using something more like:
function OnCollisionStay(Collision other)
Even if you don’t make use of the other object’s properties passed along during the collision, you still need to include them as a part of the function or it will not match with Unity’s built-in implementation.
Do you not get a warning/error regarding slicing given that you assign an individual element to a vector? Normally one would construct a complete vector and assign the whole thing.
– meat5000I would use OnCollisionExit instead of stay, to determine the point at which the object leaves the collider. Otherwise it is viable to assume that the object stays grounded longer than it should. Given the code this may not seem to be the issue, but stranger things have happened in the Unityverse.
– meat5000