Hey guys- I am having an issue with my game. I create a raycast to test if the player is grounded. If so, he should be able to jump… If not, he should. However I find that if I tap jump repeatedly, he jumps multiple times (even if not grounded) and sometimes jumps really high. Does anyone know how to fix this??
private bool isGrounded = false;
private void FixedUpdate()
{
//Move
Movement();
}
private void Movement()
{
GroundCheck();
//Apply jump if jump requested
Jump();
}
private void GroundCheck()
{
//NOTE: MAKE SURE YOUR LAYERMASK EXCLUDES PLAYER IN THE INSPECTOR
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f, excludeCharacter))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
private void Jump()
{
if (jumpRequested && isGrounded)
{
rb.velocity += Vector3.up * jumpSpeed;
jumpRequested = false;
}
else
{
jumpRequested = false;
}
}