Search Unity

Trigger collision/contact point

Discussion in 'Editor & General Support' started by seon, Jul 9, 2009.

  1. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Is there no way of obtaining the contact point in world space between 2 triggers, like we can with colliders?

    A collider returns a collision reference, but a trigger only returns the collider it hit.

    At the moment I am approximating the hit via the mid point of both triggers, but I would like to be more accurate and get the actual place the trigger contacted.

    Have I missed something?
     
  2. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    No takers?
     
  3. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You can likely get a more accurate position by using some known items, namely the collider.bounds of the trigger, then bounds.IntersectRay from the rear of the actor's collider.bounds along it's local z axis and finally ray.getpoint.

    That should return a vector3 with a closer approximation of where the actor's bounding box intersected the trigger.

    In any case, I don't believe that triggers are meant to give you such collision information. They don't need it to serve their function. And if they did it would be a line segment of the two bounding boxes' intersection and not a point.

    Likely internally it's just using the standard (and very fast) "is one of the bounding box corners within the other box's volume" intersection tests. Only after that returns true does collision code start doing more expensive and accurate tests. They're not needed for a simple trigger, therefore it opts out at that point.
     
  4. shacharoz

    shacharoz

    Joined:
    Jul 11, 2013
    Posts:
    98
    i accomplished this task using a Raycast upon collision (or colliderEnter) event.

    OnTriggerEnter(Collider other){

    GetPointOfContact();
    }

    private Vector3 GetPointOfContact()
    {
    RaycastHit hit;
    if (Physics.Raycast(transform.position, transform.forward, out hit))
    {
    return hit.point;
    }
    }
     
  5. Kerihobo

    Kerihobo

    Joined:
    Jun 26, 2013
    Posts:
    65
    I get this error after adding void to the beginning of that OnTriggerEnter:

    not all code paths return a value
     
  6. tddaniels

    tddaniels

    Joined:
    Mar 19, 2013
    Posts:
    5
    A little late to the party but you're getting that error because the function can execute in such a way that nothing is returned. I guess you could just drop a return vector3.zero to the end and call it good...depending on what you're trying to accomplish.