Distance to GameObject Collider - RayCast Ignore All but 1

My goal is to have a method that gets distance to gameObjects Colliders outside boarder - not GameObject position, so that entity can move to that gameobject and stay appropriate distance from it regardless of target gameObjects size.

physics RayCast seems like a great solution for this however I am having trouble casting a ray that ignores all but target gameObject. I am aware its possible to use layers to ignore certain colliders, but this solution will not work for me because the target gameobject shares layer with objects that may obstruct this ray cast.

Please advise me on how to approach this.

Thanks!

Assuming your collider is the same size as your object’s mesh, use something like:

	void GetDistanceToCollider(){
		Transform a;
		Transform b;
		float f;
		f = Vector3.Distance (a.position, b.position) - b.gameObject.GetComponent<Renderer> ().bounds.extents.x;
	}