How to get contact points from a trigger?

跟風遠走 提交于 2019-12-07 01:46:42

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/31641522/how-to-get-contact-points-from-a-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!