I’m having trouble with this piece of code. Specifically I’m trying to code this collision system below the character controller that can tell me if I’m grounded or not. The only problem is that the isGrounded bool rapidly switches.
The Character Controller code also includes a jump and gravity system as well as a progressive movement system (Slowly builds up speed) .
Here’s the essential code. I hope you guys can find the solution to the problem.
void GroundedCheck()
{
if (characterController.collisionFlags == CollisionFlags.Below || (characterController.collisionFlags & CollisionFlags.Below) != 0)
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
void Jumping()
{
if (isGrounded == true)
{
JumpSpeed = 0;
if (Input.GetButtonDown("Jump"))
{
JumpTimer = 0f;
JumpSpeed = JumpIntensity;
}
}
if (isGrounded == false)
{
if (JumpTimer == JumpInterval){ //Check if JumpTimer is up
JumpSpeed -= GravitySpeed; //Lower Jump Speed
if (JumpSpeed <= -JumpGravity) //Check if jump speed is faster than gravity
{
JumpSpeed = -JumpGravity; //Set Speed to max gravity
}
}
else if (JumpTimer < JumpInterval && Input.GetButton("Jump")) //Check if JumpTimer is under Max and check if Jump button is held down
{
JumpTimer += Time.deltaTime; //Add time to Timer
JumpSpeed = JumpIntensity; //Keep Jump going
}
else //if the button is not held or the Jump timer is over interval
{
JumpTimer = JumpInterval; //Set JumpTimer at end
}
}
}
Also, Quick Question: Is it practical to use Char collision flags or is there another accurate way to check collisions? Thanks in advance!