Scale guiTexture with respect to camera distance

Id like my GUI Texture that is clamped to the 3d space of a gameobject, to scale with respect to the distance of the camera and that gameObject.

I cant seem to get my math right however. Farther distances should scale it smaller, and vice versa.

Any ideas?

Ok so in that case this seems to work:

// Put this Script on your GUITexture
// assign myCamera and myObject and you're good to go

var myCamera : Transform;
var myObject: Transform;
private var viewPos: Vector3;

private var ratio : float;
private var distance : float;

var distanceFactor : float = 1000;  //factor that needs to be configured 
                                    //to achieve desired result
                                    //with field of view = 60, value of 1000
                                    //seems to be a good number.

function Start () {

ratio = guiTexture.pixelInset.height/guiTexture.pixelInset.width; // fixes the ratio of the texture

}

function Update () {

distance = (myCamera.position - myObject.position).magnitude;
viewPos =  myCamera.camera.WorldToScreenPoint(myObject.position);

guiTexture.pixelInset.width = (1/distance)*distanceFactor;
guiTexture.pixelInset.height = ratio * guiTexture.pixelInset.width;

//to keep the pivot of texture in center
guiTexture.pixelInset.x = -(guiTexture.pixelInset.width /2) + viewPos.x;
guiTexture.pixelInset.y = -(guiTexture.pixelInset.height /2) + viewPos.y;
}

sure you have to manipulate it to make it work better
hope it helps…