- Home /
Does work the raycast on a mesh collider?
Hello everybody!!
I found some similar questions, but or I don't understand the answer or does not solve my situation.
I have a mesh collider and when I try to raycast to it, I dont success; if I replace the mesh collider with a primitive collider (box collider) then I get the raycasthit object. I'm making the ray from the camera.
Is that a bug? there are solutions?
Answer by Eric5h5 · Jul 15, 2012 at 05:13 AM
Raycasts work fine with mesh colliders, and there is no bug. Either your raycast or mesh collider is wrong somehow.
Well. I'm using the 2DToolkit, and it creates the mesh collider for the sprites, but when I raycast to it it doesn't work. Here is the function I use, but I guess is not the problem:
[code]
GameObject GetObjectClicked()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
return hit.collider.gameObject;
else
return null;
}//GetObjectClicked() [/code]
This site doesn't use [code] tags; ins$$anonymous$$d put 4 spaces in front of each line. See the syntax for $$anonymous$$arkdown. Anyway, I haven't used 2DToolkit; maybe the winding order needs to be reversed or something, since raycasts won't hit the back sides of polygons.
Answer by alfchee · Jul 17, 2012 at 01:00 AM
You where right, the raycast was fine, I wrote in the forum of 2DToolkit, and somebody told me this
Set "collider cap" on the mesh colliders, and commit your sprite collection. By default no cap is generated.
So I set the "Collider Cap" to "Front" and now works.
Thanks for the help!
Your answer
Follow this Question
Related Questions
Problems with 2D RaycastHit.Collider 2 Answers
RayCastHit returns Untagged for a Tagged Item, when using a Mesh Collider. 1 Answer
Raycasting will not hit some imported meshes with mesh colliders? 2 Answers
Using raycast and collider to increase int 1 Answer
NullReference when checking tag via Raycast. How to solve this? 1 Answer