C#C
C#2y ago
nissemanen

Unity ray casting giving weird results

so I need ground checking in my game to see if you are touching the ground or not. but when I do the ray cast it says that I'm only grounded when I look in a specific place. like if it only checks when I'm looking in a perfect number of degrees. i am very new to coding in unity and i have no knowledge on what culd be the problem. here is a snipet of my code. just ask me if you need everything:
c#
void checkForGround()
{
    Ray ray = new Ray(transform.position, new Vector3(0f, -1.1f, 0f));
    RaycastHit hit;
    Debug.DrawRay(transform.position, new Vector3(0f, -1.1f, 0f), Color.green);

    if(Physics.Raycast(ray, out hit, 1, ~RayLayerMask))
    {
        Debug.Log("grounded " + hit.collider.gameObject.name);
        isGrounded = true;
    }else
    {
        Debug.Log("not grounded");
        isGrounded = false;
    }
}
(this is run in Update())
Was this page helpful?