14

I'm in a situation where I need a 2d sensor that will not collide but will also give me contact points for a collision.

Triggers don't give me contact points and colliders give me contact points but cause a collision.

I've tried disabling collisions when using colliders in the hopes of getting a collision enter callback but not having the collision actually occur, but no luck.

So how do I get contact points from a trigger? Or how do I get a collision callback with rigidbodies without causing a collision?

Basically I have a circle that I want to act as radar, but I'd like it to be fairly accurate with contact points too.

Nain
  • 1,204
  • 1
  • 12
  • 17
shwick
  • 4,277
  • 6
  • 21
  • 28

3 Answers3

10

Posting due to this being top Google result.

I think there's a simpler (depends on how you look at it of course) and more precise way to do this than raycasting.

private void OnTriggerEnter(Collider collider)
{ 
    var collisionPoint = collider.ClosestPoint(transform.position);
}

collider is a collider that entered trigger in question, transform is a triger's transform. As the function name suggests this fill find a point on collider closest to said trigger. Of course this is not super precise especially for weirdly shaped colliders, but most likely it will be good enough for most of the cases, definitely good enough for a projectile, or say, blade, hitting a rigidbody character.

As a bonus, here's a simple vector math to find collision normal:

var collisionNormal = transform.position - collisionPoint;

Again, not super precise and will not be an actual proper normal (i.e perpendicular) to the impact point, but as with a previous one - most likely will be good enough.

Have fun!

Max Yari
  • 3,617
  • 5
  • 32
  • 56
  • 2
    This doesn't have to be correct all the time though ... in particular if your pivot point is not in the center of your collider this might return completely wrong results ;) – derHugo Sep 09 '21 at 15:42
3

You should use OnCollisionEnter but add a rigidbody to it and set isTrigger to false and set rigidbody isKinematic to true then it will act like a trigger and you can get the contact points like this

 void OnCollisionEnter(Collision collision) {
        foreach (ContactPoint contact in collision.contacts) {
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
  • Thank you, this is the key. I got it working. Also nain thanks I had to use raycasts as well. – shwick Jul 28 '15 at 03:21
  • 1
    This will not work in general; for example, a kinematic Rigidbody will not collide with static colliders, such as most of the environment in most games. – Joe Strout May 12 '23 at 21:06
3

You can get point of contact using OnTriggerEnter function

OnTriggerEnter(Collider other)
{
    RaycastHit hit;
    if (Physics.Raycast(transform.position, transform.forward, out hit))
    {
        Debug.Log("Point of contact: "+hit.point);
    }
}
Nain
  • 1,204
  • 1
  • 12
  • 17
  • 2
    This assumes that the thing you're hitting is somehow in front of this object (since you're casting a ray directly forward from the transform position). That will not, in general, be true — for example, if this object is a cube, and it's fallen down until it hits the ground, this ray will quite likely miss whatever it collided with entirely. – Joe Strout May 12 '23 at 21:08
  • This only works for one very limited case. It really doesn't answer the original question. – s1ni5t3r Aug 28 '23 at 13:22