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;
}
}
can yoou put this line inside the if(physics.raycast…) in the groundCheck?
Debug.Log(hit.collider.name);
this will help you to know if there are any objects he is colliding with and that you should exclude since probably is a layer problem
Hi! You could try this: ` private bool isGrounded = false;
public LayerMask WhatIsGround;
private void FixedUpdate()
{
//Move
Movement();
}
private void Update() {
//NOTE: MAKE SURE TO SET THE LAYERMASK TO YOUR GROUND LAYER
isGrounded = Physics.Raycast(transform.position, Vector3.down, 1f, WhatIsGround);
if(Input.GetKeyDown(KeyCode.Space) && isGrounded) {
Jump();
}
}
private void Movement()
{
}
private void Jump()
{
rb.velocity += Vector3.up * jumpSpeed;
jumpRequested = false;
}`
I cleaned up your script a bit too.
PS. I assume that your game is 3D.
@thestrandedmoose