Hello Guest

Author Topic: Physics 2D problem  (Read 4740 times)

pedronveloso

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Physics 2D problem
« on: November 21, 2013, 04:21:48 pm »
I've switched all my sprite atlas to physics 2D, however my collision detection does not work anymore, even simple things like BoxCollider2D.

I'm doing collision detection like this:

Code: [Select]
Ray ray = hudCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();
            // check hits on HUD
            if (Physics.Raycast(ray, out hit, Constants.HUDRayDistance)) {


Is there something else I should be changing?

mrsquare

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Physics 2D problem
« Reply #1 on: November 21, 2013, 05:19:44 pm »
Hey - yep, you want to use Physics2D.Raycast, not Physics.Raycast. As of 4.3, there are different raycasting systems for the two types of physics engines. To detect clicking on something, I use this:

Code: [Select]
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

RaycastHit2D rayCastResult = Physics2D.Raycast(mouseWorldPosition, new Vector3(0, 0, 0), 0.0f);

if(rayCastResult.rigidbody)
{
    // Do Stuff
}
« Last Edit: November 21, 2013, 05:21:29 pm by mrsquare »

pedronveloso

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Physics 2D problem
« Reply #2 on: November 22, 2013, 10:53:09 pm »
That solved my problem. Thanks ;)