Hello Guest

Author Topic: Raycast doesn't detect concave colliders  (Read 5121 times)

Zama Games

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Raycast doesn't detect concave colliders
« on: April 23, 2013, 02:05:26 pm »
Hi, I have some sprites in the game which need to have concave polygon colliders or even colliders with a few islands.

To detect tap on the sprite, I am using this method.

Code: [Select]
protected bool IsOverObject(Vector3 screenPosition, out RaycastHit hitInfo)
{
Ray ray = this.camera.ScreenPointToRay (screenPosition);
bool isOverObject = Physics.Raycast (ray, out hitInfo, Mathf.Infinity);
return isOverObject;
}

Appparently this only works if the collider is marked as convex. In that case however 2D toolkit connects all the islands and makes a bigger convex collider, and the casted ray hits that collider. Without "concave", the ray never intersects a collider, even if the tap was directly on one of the islands or on a point inside the concave collider.

So, what is the correct way to detect taps on concave colliders?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Raycast doesn't detect concave colliders
« Reply #1 on: April 23, 2013, 07:28:18 pm »
The reason polygon colliders don't work when "clicked" is that by default, the polygon caps aren't created. Refer to the image below.
You can enable faces (if you're clicking with the mouse, you should only need front front caps). If you only have very few of these then you can have front & back caps without worrying about anything.

So enable the caps you require, and click Commit - your clicking should now work.




Why convex "worked" -
When you ticked convex, that formed a convex hull around the points. A convex shape is naturally closed, and will have front and back caps.

Zama Games

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Raycast doesn't detect concave colliders
« Reply #2 on: April 24, 2013, 01:11:35 am »
Thank you very much! It works now.
I guess you could place those images and the explanation into your official documentation page about polygon colliders since it's not obvious what "caps" and "convex" mean.
You could also note there that if "convex" is checked, the collider will be turned into a convex one in runtime even if the polygon was made concave or has islands (at least if I understood it correctly)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Raycast doesn't detect concave colliders
« Reply #3 on: April 24, 2013, 11:21:51 am »
That concave behaviour is what Unity does actually - it takes whatever shape you give it and constructs a convex hull around it. Its hard to know where to stop documenting, as it could go very very deep :)

But I'll add that thing about the caps, and maybe make it default to front & back faces to avoid this kinds of situations. At least the option to delete faces will be there for optimization purposes.