2D Toolkit Forum
2D Toolkit => Support => Topic started 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:
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?
-
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:
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D rayCastResult = Physics2D.Raycast(mouseWorldPosition, new Vector3(0, 0, 0), 0.0f);
if(rayCastResult.rigidbody)
{
// Do Stuff
}
-
That solved my problem. Thanks ;)