I am trying to implement a Lock-targeting texture with the WorldToViewportPoint function but it does not work ( the texture is not on the enemy-target position but sometimes in the screen top corner or most of the times not visible at all).This is the code simplified:
/code/
var target : Transform;
var locktexture : Texture;
var micamera : Camera;
function Update () {
var viewPos = micamera.WorldToViewportPoint (target.position);
WorldToViewportPoint is for getting coordinates in viewport space, which is normalized (i.e., always between 0 and 1). GUITextures use viewport space; in fact you might prefer to use GUITextures for this sort of thing. In which case you’d use
var target : Transform;
var micamera : Camera;
function Update () {
transform.position = micamera.WorldToViewportPoint (target.position);
}
and attach that to the GUITexture object. If you really do want to use Graphics.DrawTexture, that has to be in OnGUI, and you’d use WorldToScreenPoint, because DrawTexture needs screen coordinates…except the Y coordinate needs to be inverted (and this is why I prefer GUITextures for stuff like this, it’s just easier):
var target : Transform;
var locktexture : Texture;
var micamera : Camera;
function OnGUI () {
var viewPos = micamera.WorldToScreenPoint (target.position);
Graphics.DrawTexture(Rect(viewPos.x-16, Screen.height-viewPos.y-16, 32, 32), locktexture);
}