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.
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!
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);
}
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With