How to find a point in a collider farthest from a given position? (reversed ClosestPointOnBounds)

If i have some vector 3 position in the world and i have some collider (box, sphere…) in the world as well, how do i find out, which position in this collider is the most far from my vector 3 position?

So basically reversed ClosestPointOnBounds, ( as in something like FarthestPointOnBounds). Thanks

I don’t think there’s a method provided by Unity, so you have to calculate them yourself.

But why you need this ? :smile:

move your vector 3 to the exact opposite side of the collider, and check the closestpointonbounds from there.

So if your vector is “position”, and the collider is “col”, it’d be:

Vector3 positionToCollider = col.transform.position - position;

Vector3 otherSide = col.transform.position + positionToCollider;

Vector3 farthersPoint = col.ClosestPointOnBounds(otherSide);

If the collider is a primitive you can use the properties of the primitive to ‘project’ the position across. Spheres will be easiest, but it will work with cubes or capsules as well.

If you are working with a complex mesh collider you are stuck iterating through vertices of the mesh and doing distance checking on each one.

Edit: Just realised you are working with bounds. This makes it pretty simple. Just check the corners of the bounding box to determine which point is furthest away. Fun fact, with cubes it will always be one of the corners on the opposite face that is furthest away.

Will this work for anything not a sphere? Its late and my math is a bit sketchy.

Baste: That doesn’t work for box colliders, i tried it, but it returns incorrect values

BoredMormon: I can’t seem to figure out how to get world position of the corners from a collider box.

Since bounds are always square, its bounds.centre + bounds.extents. Separate the extents out to its individual float values.

For a Box Collider when i’m correct, one of the vertex points of the collision box has to be the farest point away.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {


    public List<Vector3> vertexPoints = new List<Vector3>();
    public Vector3 myVector = new Vector3 (10f, 10f, 10f);

    void Start () {

        BoxCollider boxCollider = gameObject.GetComponent<BoxCollider> ();

        vertexPoints.Add (boxCollider.bounds.max);
        vertexPoints.Add (boxCollider.bounds.min);
        vertexPoints.Add (new Vector3 (boxCollider.bounds.max.x, boxCollider.bounds.max.y, boxCollider.bounds.min.z));     
        vertexPoints.Add (new Vector3 (boxCollider.bounds.max.x, boxCollider.bounds.min.y, boxCollider.bounds.min.z));      
        vertexPoints.Add (new Vector3 (boxCollider.bounds.max.x, boxCollider.bounds.min.y, boxCollider.bounds.max.z));     
        vertexPoints.Add (new Vector3 (boxCollider.bounds.min.x, boxCollider.bounds.min.y, boxCollider.bounds.max.z));     
        vertexPoints.Add (new Vector3 (boxCollider.bounds.min.x, boxCollider.bounds.max.y, boxCollider.bounds.max.z)); 
        vertexPoints.Add (new Vector3 (boxCollider.bounds.min.x, boxCollider.bounds.max.y, boxCollider.bounds.min.z));


        int maxDistanceVector = 0;
        float distance = 0;
        Vector3 getCollisionPoint = boxCollider.ClosestPointOnBounds (myVector);

        for (int i = 0; i < vertexPoints.Count; i++){
            if (i == 0) {
                distance = Vector3.Distance(getCollisionPoint, vertexPoints[i]);
                maxDistanceVector = 0;
            } else {
                float newDistance = Vector3.Distance(getCollisionPoint, vertexPoints[i]);
                if(distance < newDistance){
                    distance = newDistance;
                    maxDistanceVector = i;
                }
            }

        }

        Debug.Log ("Farest away point of box collider from vector: " + vertexPoints [maxDistanceVector]);
    }
}
1 Like

That’s what I was suggesting. You missed two vertex points, but the concept is sound.

1 Like

Thanks guys, it works. But i noticed one buglike thing. If i place sphere on the vert coordinates to actually see them visually, the get placed exactly at the collider box vertices if the game object is placed with zero rotation. But if the object is rotated then the verts are in incorrect position it seems.