Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.