(Help)How can i detect how many raycasts are hitting an object?

(Help)How can i detect how many raycasts are hitting an object?

In 3D you are given a boolean back from the Raycast.

In 2D you are given a RaycastHit2D object back and check if its collider is non-null.

Both of these concepts are covered explicitly with sample code in the Unity documentation for Physics.Raycast and Physics2D.Raycast.

Beware that there is no direct interoperability between 2D and 3D physics.

1 Like

I am using 3d but i need the see if raycasts are hitting a specific object i looked at the documentations but there were only one bool that was to see if it hits any objects sorry i didnt specify the question enough.

There are many types of 3D raycasts.

One is the generic Physics one: hits anything in the scene

Another is one that lives on a collider.

If you know which collider you care about, you can use that collider instance to see if a ray hits it.

That way you not only know that it hit, but because you are already asking “Did anything hit this collider?” you also know what collider it is.

These are some of the types of 3D raycasts:

  • asks “did I hit anything in the scene?”
  • asks “did I hit this specific collider?”
  • asks “did I hit this plane?”

They are unrelated to each other but related conceptually.

3 Likes

thanks will review