- Home /
Raycasting to the bottom side of a plane?
Hi,
I have a short question. Currently, If I use Physics.Raycast and cast a ray from below a plane, it won't hit it. If I cast a ray from above, it will.
Is there a way to have the raycast hit in both ways, without creating a manual dual-sided plane object?
Thanks!
Answer by Bunny83 · Apr 03, 2011 at 12:03 PM
Since the default plane in Unity have just a Mesh collider, you can replace it with a BoxCollider and make it very thin. I guess a BoxCollider performs better than two Meshcollider with a 200 tris mesh ;) (the plane have 10*10*2 tris).
The opposite Raycast was also my first thought, but I guess you will detect a lot of other colliders than the one you'd expected. I think I would use a BoxColider, but it depends on your needs.
Bunny: You'd do a RaycastAll and get the last hit on the reverse operation.
I'd pick a BoxCollider any day I could make the swap. However, if you're dealing with custom meshes then you obviously can't do this since you'd expect some other shape.
That's true, but it's not very effective to use RaycastAll
. Also it depends on what information you need from the raycast. RaycastAll juat gives you a list of all colliders and no hitpoint(s). For custom meshes you need to use a mesh collider and then you have no way around that. If you want to check for one specific Collider only you can use Collider.Raycast
ins$$anonymous$$d of Physics.Raycast
. Collider.Raycast
can only hit this collider.
BTW: Do you know if you can rely on the order of the returned collider array of RaycastAll
? Never tried that.
Answer by Statement · Apr 03, 2011 at 11:33 AM
No. You'll have to do two opposite Raycasts, or two opposite planes. I'd suggest the latter.