© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
11 replies
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:
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;
    }
}
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())
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Vost-api not giving accurate results
C#CC# / help
15mo ago
✅ my unity project is being weird
C#CC# / help
4mo ago
✅ Type Casting
C#CC# / help
4y ago