Raycast not always detecting floor
I am using raycast to detect that the player is grounded after a jump, my problem is that some times the raycast is not detecting that the player is grounded and it is.
This is my code for the raycasting:
void IsGrounded()
{
RaycastHit hit;
int mask = 1 << LayerMask.NameToLayer("Floor");
if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f, mask))
{
isGrounded = true;
checkGround = false;
Debug.Log("is grounded");
if (!myAudioSource.isPlaying)
{
myAudioSource.Play();
}
}
Debug.DrawLine(transform.position, hit.point, Color.red);
}
I noticed using the Debug.Drawline that the raycast is always sending the ray to the original position. Like it can be seen in this images:
Here is my problem:
The character is in the ground but the ray is still not detecting it, and it's casting to the original position.
My level is made of many objects with the layer Floor, so I think that maybe it's getting the first object with the layer and always using it to raycast. How could I get the current relevant floor object?
Answer by lgarczyn · Nov 29, 2019 at 08:08 PM
It's drawing to the origin because hit.point is uninitialized, and thus (0,0,0).
The first step would be to draw the Debug.Line to hit.point if the raycast hit, or to transform.position + Vector3.down * raycast length
if the raycast failed.
Your actual problem is likely that you player transform position is exactly overlayed with the collider under it, which means that the raycast starts already through the collider.
To fix this, simply start your raycasts from transform.position + Vector3.up * 0.01f
.
Or just use the CharacterController.isGrounded value.