Hiya
I’m using the below script from the wiki to add a label to an object in 3d space.
I’m using the ClampToScreen variable to keep the label on screen even if it’s out of sight of the player, HOWEVER: the object label returns to the center of the camera view even if the object is BEHIND the camera. I assume because it’s just seeing the thisTransform.position.z bit along the z axis line and doesn’t know if that’s in front or behind the cam…
Can anyone suggest a way to keep the label on the edge of the screen if the object is behind the camera?
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
}
function Update () {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
if (clampToScreen)
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, .05, .95),
Mathf.Clamp(thisTransform.position.y, .05, .95),
thisTransform.position.z);
}