GuiTexture Tracking Enemy on Screen

I'm trying to get a targeting sight to position itself on the screen as long as the enemy is there, but it doesn't work.

It takes the Target's position and uses it to set a GUItexture's position on the screen. The problem is that it doesn't have the target in center. Am I doing something wrong?

Is there some other way?

function Update () {
var screenPos : Vector3 = camera.ViewportToScreenPoint (Target.position);
print ("target is " + screenPos.x + " pixels from the left");
guiTexture.pixelInset = Rect (screenPos.x, screenPos.y, 10, 10 );

}

Hi,

you are calling the wrong function. WorldToViewportPoint is the right call. Here is a working version:

Create a new GuiTexture. drop that javascript, and assign a transform to the Public variable Target in the inspector. Run, and the guiTexture will be properly centered.

You will likely change the texture obviously, then pixelInset values might needs changing. but there is no need to do that within your script.

public var Target:Transform;

function Update () {
    transform.position = Camera.main.WorldToViewportPoint(Target.position);
}

Hope this helps,

Bye,

Jean