Raycast goes down globally instead of locally

I’m trying to do a raycast that is always pointing down, relative to the object it is attached to. When the ray is touching something with the tag “Ground”, my program should type a word. When the object flips at a 90 degree angle, the ray should too, but it does not. It says pointing down in global terms. the code I’m using:

RaycastHit hit;
Ray floorRay = new Ray(transform.position, Vector3.down);

if(Physics.Raycast(floorRay, out hit, 0.2f))
{
if(hit.collider.tag == “Ground”)
{
Debug.Log (“Fart”);
}
}

Please use Code tags as the sticky thread instructions explain.

The vector probably just needs to be converted from local space. Try this:

Ray floorRay = new Ray(transform.position, transform.InverseTransformDirection(Vector3.down));

Or just use -transform.up

2 Likes

Or just use -transform.up.