2D Toolkit Forum

2D Toolkit => Support => Topic started by: Ben on May 05, 2013, 07:38:43 pm

Title: Sprite collision detection
Post by: Ben on May 05, 2013, 07:38:43 pm
Hi,
I am using raycasting to detect which object has been clicked, however the code I've used detects only manually created game objects such as cubes or spheres. I have a group of sprites created dynamically from prefabs and those remain undetected while clicking on them. It looks like there is no collision detection occurring on those sprites. I've use the following code:

if (Input.GetMouseButtonDown(0)) {
          Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;         
          if (Physics.Raycast(ray,out hit,1000))
          {
            Debug.Log(hit.collider.tag);            
          }
      }

I'm certainly missing something here. I'd appreciate any suggestions.

Title: Re: Sprite collision detection
Post by: unikronsoftware on May 05, 2013, 07:41:55 pm
If the sprite is using a polygon collider, then you should set collider cap to front & back in the sprite collection editor. By default they are created without front or back faces - eg when using it as a platform / barrier the front and back faces aren't required.

If you're using a box collider then check that it isn't intersecting the camera...
Title: Re: Sprite collision detection
Post by: Ben on May 05, 2013, 07:44:53 pm
OK, I'll try it out. Thank you.