0
\$\begingroup\$

I have a sprite-based GameObject in Unity to which is attached a RigidBody and a Collider2D. When this GameObject comes into contact with a particular Collider2D Trigger, I want a particle effect to play at the point of contact. This sounds extremely simple to set up and yet I can't seem to find any solution that works, since OnTriggerEnter2D doesn't bother to provide hit coordinates.

Please note that the trigger represents a non-solid space, so adding a solid (non-trigger) collider for hit detection is not a solution.

The sprite is large, so I can't just play the particle effect at its transform position. Raycast is not an appropriate solution for this problem; it doesn't handle nearly enough cases.

\$\endgroup\$
  • \$\begingroup\$ Why the downvote? \$\endgroup\$ – user45623 Mar 3 '16 at 19:10
  • \$\begingroup\$ Depending on the type of colliders you're using, you could try to calculate points of intersection yourself. \$\endgroup\$ – zcabjro Mar 4 '16 at 1:14
  • \$\begingroup\$ Try this otherwise: stackoverflow.com/questions/31641522/… \$\endgroup\$ – zcabjro Mar 4 '16 at 1:16
1
\$\begingroup\$

You could use raycast:

If transform.forward isn't right, use whatever vector that represents that direction the object is going in.

OnTriggerEnter(Collider other){
  RaycastHit hit;
  if (Physics.Raycast(transform.position, transform.forward, out hit))
  {
    // do something with hit.point
  }
}
\$\endgroup\$
  • \$\begingroup\$ This is one of the things I tried, but I guess I didn't do it correctly. I'll give it another shot. I don't feel like it's a great solution because you can't tell the raycast to only look for a specific target, but if I can get it working that should be good enough. \$\endgroup\$ – user45623 Mar 3 '16 at 19:40
  • \$\begingroup\$ This approach doesn't really work. Drawing a ray from the sprite's center doesn't necessarily tell you which part of the sprite is touching the trigger. \$\endgroup\$ – user45623 Mar 3 '16 at 22:18
  • \$\begingroup\$ Hmmmm, sorry to hear that. What if you had a game object on top of the trigger that is the same thing except a collider instead of a trigger, and after you come in contact with it it is deleted so you can so go through the trigger? Not a great solution. \$\endgroup\$ – danglingPointer Mar 4 '16 at 1: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.