RayCastHit2D hits Background

Hello!

My RayCastHit2D is hitting the Backgrund. How to Fix that?

        RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.down);
        if (hit.collider != null) {
            print (hit.collider.name + " at " + hit.point);
        }

EDIT:
If i disable my background in herachy, it works

One solution is to create a layermask to items you want to be able to raycast. Tell your MainCamera to ignore background layer. Why does a collider exist on your background sprite?

You could move your z position of the background back slightly.

1 Like