ObjectLabel

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Added new page)
 
(Improved behavior of label position when clamped to screen and the object moves behind the camera)
Line 9: Line 9:
  
 
==Usage==
 
==Usage==
Attach this script to a GUIText object, and drag the object it should follow into the '''Target''' slot. For best results, the anchor of the GUIText should probably be set to lower center, depending on what you're doing.  '''Offset''' is used to position the label somewhere relative to the actual target's position. The default of (0, 1, 0) is useful for having the label appear above the object, rather than appearing right on top of it. If '''ClampToScreen''' is on, the label will never disappear even if the target is off the screen, but will attempt to follow as best it can (for example, if the target is off to the left of the camera out of sight, the label will still be visible on the left). If '''UseMainCamera''' is checked, the first camera in the scene tagged MainCamera will be used.  If it's not checked, you should drag the desired camera onto the '''CameraToUse''' slot, which is otherwise unused if UseMainCamera is true.
+
Attach this script to a GUIText object, and drag the object it should follow into the '''Target''' slot. For best results, the anchor of the GUIText should probably be set to lower center, depending on what you're doing.  '''Offset''' is used to position the label somewhere relative to the actual target's position. The default of (0, 1, 0) is useful for having the label appear above the object, rather than appearing right on top of it. If '''ClampToScreen''' is on, the label will never disappear even if the target is off the screen, but will attempt to follow as best it can (for example, if the target is off to the left of the camera out of sight, the label will still be visible on the left). '''ClampBorderSize''' sets how much space will be left at the borders if the label is being clamped to the screen, to help ensure that the label is still readable and not partially cut off. This is in viewport space, so the default .05 is 5% of the screen's size. If '''UseMainCamera''' is checked, the first camera in the scene tagged MainCamera will be used.  If it's not checked, you should drag the desired camera onto the '''CameraToUse''' slot, which is otherwise unused if UseMainCamera is true.
  
 
==JavaScript - ObjectLabel.js==
 
==JavaScript - ObjectLabel.js==
Line 15: Line 15:
 
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
 
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 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 useMainCamera = true; // Use the camera tagged MainCamera
 
var cameraToUse : Camera; // Only use this if useMainCamera is false
 
var cameraToUse : Camera; // Only use this if useMainCamera is false
 
private var cam : Camera;
 
private var cam : Camera;
 
private var thisTransform : Transform;
 
private var thisTransform : Transform;
 +
private var camTransform : Transform;
  
 
function Start () {
 
function Start () {
Line 26: Line 28:
 
else
 
else
 
cam = cameraToUse;
 
cam = cameraToUse;
 +
camTransform = cam.transform;
 
}
 
}
  
 
function Update () {
 
function Update () {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
+
if (clampToScreen) {
if (clampToScreen)
+
var relativePosition = camTransform.InverseTransformPoint(target.position);
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, .05, .95),
+
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
Mathf.Clamp(thisTransform.position.y, .05, .95),
+
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);
 
thisTransform.position.z);
 +
}
 +
else {
 +
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
 +
}
 
}
 
}
 +
 +
@script RequireComponent(GUIText)
 
</javascript>
 
</javascript>

Revision as of 15:17, 9 February 2010


Author: Eric Haines (Eric5h5)

Description

Makes a GUIText label follow an object in 3D space. Useful for things like having name tags over players' heads.

Usage

Attach this script to a GUIText object, and drag the object it should follow into the Target slot. For best results, the anchor of the GUIText should probably be set to lower center, depending on what you're doing. Offset is used to position the label somewhere relative to the actual target's position. The default of (0, 1, 0) is useful for having the label appear above the object, rather than appearing right on top of it. If ClampToScreen is on, the label will never disappear even if the target is off the screen, but will attempt to follow as best it can (for example, if the target is off to the left of the camera out of sight, the label will still be visible on the left). ClampBorderSize sets how much space will be left at the borders if the label is being clamped to the screen, to help ensure that the label is still readable and not partially cut off. This is in viewport space, so the default .05 is 5% of the screen's size. If UseMainCamera is checked, the first camera in the scene tagged MainCamera will be used. If it's not checked, you should drag the desired camera onto the CameraToUse slot, which is otherwise unused if UseMainCamera is true.

JavaScript - ObjectLabel.js

<javascript>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) </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox