2D Toolkit Forum

2D Toolkit => Support => Topic started by: pedronveloso on November 21, 2013, 04:21:48 pm

Title: Physics 2D problem
Post by: pedronveloso 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?
Title: Re: Physics 2D problem
Post by: mrsquare 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
}
Title: Re: Physics 2D problem
Post by: pedronveloso on November 22, 2013, 10:53:09 pm
That solved my problem. Thanks ;)