- Home /
The question is answered, right answer was accepted
CapsulCast is switching between false and true [SOLVED]
Hello, I am trying to get my CapsulCast to detect wether the Player is on the ground or not. It workes aslong the player is falling its giving a steady false for the grounded variable but when it starts hiting the ground it will start to switch between true and false... Is this a known bug?
public void Gravitation()
{
//prüft ob der character aud dem boden steht
CheckGround();
if (IsGrounded == false)
{
Velocity.y -= Gravity * Time.deltaTime;
}
if (IsGrounded && Velocity.y < 0f)
{
Velocity.y = 0f;
}
if (Velocity.y < MaxVelocity)
{
Velocity.y = MaxVelocity;
}
PLAYER.transform.position = PLAYER.transform.position + new Vector3(0, Velocity.y * Time.deltaTime, 0f);
}
public void CheckGround()
{
Vector3 point1 = PLAYER.transform.position + new Vector3(0, Collider.height / 2 - Collider.radius, 0);
Vector3 point2 = PLAYER.transform.position + new Vector3(0, -Collider.height / 2 + Collider.radius, 0);
float castDistance = 0.1f;
if (Physics.CapsuleCast(point1, point2, Collider.radius, Velocity.normalized, castDistance, Groundmask))
{
IsGrounded = true;
}else { IsGrounded = false;}
}
I also tried using Physics.CapsulCastNonAll and i saw the same thing it switched between 0 and 1 when it should always detect the ground.
SOLVED: I found my mistake if ground is detectet my velocity will be 0 so in the next frame there will be no direction where the sphere will be cast so it will return false, then the velocity will go up again and it will cast and return true and then the loop begins again. To fix that I entered a fixed value for the direction because it always should check downwards.
if (Physics.CapsuleCast(point1, point2, Collider.radius, new Vector3(0f, -1,f 0f), castDistance, Groundmask))