0
\$\begingroup\$
    void OnCollisionEnter2D(Collision2D collision)
    {
       foreach(ContactPoint2D contact in collision.contacts) 
       {
           RaycastHit raycastInfo;
           Physics.Raycast(contact.point, Vector3.forward, out raycastInfo, 1000.0f);
           if(raycastInfo.collider)
           {
               Debug.Log("Hit!");
           }
       }
    }

I am using this code to raycast an abject with the contact position of collision. The collision part works fine, when collision happens this function is triggered. But when I raycast to that position, RaycastHit variable return empty.

I couldn't find what I am doing wrong.

\$\endgroup\$
1
\$\begingroup\$

Looks like you're mixing 3D & 2D physics.

You're using OnCollisionEnter2D, which triggers when 2D colliders hit each other.

But then you're using Physics.Raycast, which casts rays against 3D colliders - so it won't be able to detect the two colliders that triggered the event, or any similar colliders in your scene. Maybe you want Physics2D.Raycast?

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, my bad. I didn't think that would cause a problem. Solved it. \$\endgroup\$
    – mcelik
    Jul 5 '15 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.