Get gameobject limits

Im using a raycast to detect if a gameobject is between the target, what I want now is to get the distance between the point of the object hit and the left and right points where the object ends. I attach an image for a better understanding here. Thanks for the help and tell me if you can’t understand the question (I dont know exactly how to explain it but I can attach a more precise screenshot).

You can use local coordinates and mesh.bounds to find these edges. Here is an example script. Note it uses the alignment between the normal and the forward to detect whether you clicked on a back/front side or a left/right side. This will not work for an arbitrary shaped object…just cubes.

To test, start a new scene. And and resize a cube. Add the script to an empty game object. Click on the cube. It positions a couple of marker spheres on the edges and outputs in a Debug.Log(), the distance to the two edges:

#pragma strict

private var marker1 : Transform;
private var marker2 : Transform;
private var localXExtent : float;
private var localYExtent : float;

function Start() {
	marker1 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
	marker2 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
	marker1.localScale = new Vector3(0.15, 0.15, 0.15);
	marker2.localScale = new Vector3(0.15, 0.15, 0.15);
}

function Update() {

	if (Input.GetMouseButtonDown(0)) {
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	
		 if (Physics.Raycast(ray, hit)) {
			var target = hit.transform;	
			var extent : float;
			var mf = target.GetComponent(MeshFilter);
			var localHit = target.InverseTransformPoint(hit.point);
			if (mf != null) {
				if (Mathf.Abs(Vector3.Dot(hit.normal, target.forward)) > 0.9) { 
					extent = mf.mesh.bounds.extents.x;
					marker1.position = target.TransformPoint(Vector3(-extent, localHit.y, localHit.z));
					marker2.position = target.TransformPoint(Vector3(extent, localHit.y, localHit.z));
				}
				else {
					extent = mf.mesh.bounds.extents.z;
					marker1.position = target.TransformPoint(Vector3(localHit.x, localHit.y, -extent));
					marker2.position = target.TransformPoint(Vector3(localHit.x, localHit.y, extent));			
				}
				Debug.Log("Distance 1: "+Vector3.Distance(marker1.position, hit.point));
				Debug.Log("Distance 2: "+Vector3.Distance(marker2.position, hit.point));
			}
		}
	} 
}