Skip to content

Instantly share code, notes, and snippets.

@josephjaniga
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephjaniga/e1a015b8448c08caf229 to your computer and use it in GitHub Desktop.
Save josephjaniga/e1a015b8448c08caf229 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (GUIText))]
public class ObjectLabel : MonoBehaviour {
public string npcName;
public Transform target; // the object the label should follow
public Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public bool useMainCamera = true; // Use the camera tagged MainCamera
public Camera cameraToUse; // Only use this if useMainCamera is false
Camera cam;
Transform thisTransform;
// Use this for initialization
void Start () {
guiText.text = npcName;
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
}
// Update is called once per frame
void Update () {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment