asker152
asker152

Reputation: 4887

How to get contact points from a trigger?

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.

Upvotes: 17

Views: 43149

Answers (4)

Daniel Keele
Daniel Keele

Reputation: 57

You can find the intersections of the two colliders using their paths and points.

void OnTriggerStay2D(Collider2D otherCollider)
{
    Vector2[] otherPoints = (EdgeCollider2D)otherCollider.points
    Vector2[] thisPoints = (PolygonCollider2D)thisCollider.GetPath(x)
}

The trick here is handling the intersection math for the various kinds of colliders (Capsule, Circle, etc). I found a great writeup here on line segments.

Upvotes: 0

Max Yari
Max Yari

Reputation: 3697

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!

Upvotes: 13

Nain
Nain

Reputation: 1222

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);
    }
}

Upvotes: 2

Milad Qasemi
Milad Qasemi

Reputation: 3049

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);
        }

Upvotes: 2

Related Questions