Hello Guest

Author Topic: Sprite collision detection  (Read 3800 times)

Ben

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Sprite collision detection
« 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.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite collision detection
« Reply #1 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...

Ben

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprite collision detection
« Reply #2 on: May 05, 2013, 07:44:53 pm »
OK, I'll try it out. Thank you.