Hey, I’m experiencing a bit of a bug with the NPC label script that I’m using to make a name appear above the NPC’s head. Basically what happens is the NPC name will come back onto my screen for some odd reason if I run away from the NPC, even if I’m far away from the NPC. I’ll post a video that more clearly shows what I’m talking about:
The bug can first be seen around 1:37.
Here’s the code:
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 clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
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;
private var camTransform : Transform;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
}
function Update () {
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
@script RequireComponent(GUIText)