- Home /
Still constant jumping even with raycasting.
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.01f)); {
canJump = true;
}
if(canJump && Input.GetKeyDown("space")) {
canJump = false;
GetComponent<Rigidbody> ().AddForce (0, jumpingForce, 0);
}
}
}
Comment
Do you know what does a semi-colon after an if
entail? The canJump = true;
statement will be executed even if the Physics.Raycast
returns false.
Answer by unity_ek98vnTRplGj8Q · Jun 10, 2020 at 09:56 PM
Add a small delay before you check for being grounded again after a jump: what is likely happening is the frame after you press jump your canJump variable is being set to true again because your character hasn't moved far enough to clear the raycast check. You can also add an else statement to the raycast that says to set canJump to false if there is no ground under you.