kasru

Hovering Text

Jan 28th, 2013
11,899
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. var target : Transform;     // Object that this label should follow
  5. var offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
  6. var clampToScreen = false;  // If true, label will be visible even if object is off screen
  7. var clampBorderSize = .05;  // How much viewport space to leave at the borders when a label is being clamped
  8. var useMainCamera = true;   // Use the camera tagged MainCamera
  9. var cameraToUse : Camera;   // Only use this if useMainCamera is false
  10. private var cam : Camera;
  11. private var thisTransform : Transform;
  12. private var camTransform : Transform;
  13.  
  14. function Start () {
  15.     thisTransform = transform;
  16.     if (useMainCamera)
  17.         cam = Camera.main;
  18.     else
  19.         cam = cameraToUse;
  20.     camTransform = cam.transform;
  21. }
  22.  
  23. function Update () {
  24.     if (clampToScreen) {
  25.         var relativePosition = camTransform.InverseTransformPoint(target.position);
  26.         relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
  27.         thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
  28.         thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
  29.                                          Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
  30.                                          thisTransform.position.z);
  31.     }
  32.     else {
  33.         thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
  34.     }
  35. }
  36.  
  37. @script RequireComponent(GUIText)
RAW Paste Data