C
C#4mo 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;
}
}
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())
7 Replies
Pobiega
Pobiega4mo ago
Sounds like a question for $unity to be honest.
nissemanen
nissemanen4mo ago
alright ill post it there then
Pobiega
Pobiega4mo ago
👍
nissemanen
nissemanen4mo ago
i just dont know where phys or somewhere else
Pobiega
Pobiega4mo ago
¯\_(ツ)_/¯ No clue. I don't do unity. but what you are asking for is specific to the unity engine, not a C# problem itself
nissemanen
nissemanen4mo ago
:/