Skip to main content

人物头顶文字

using UnityEngine;
using System.Collections;

public class Text : MonoBehaviour {

public Transform target;
public Camera camera;
public GUITexture guitexture ; //偏移什么的所有的都设为0
public GUIText guitext; //偏移什么的所有的都设为0

public GUIStyle center;

Vector3 screenPos=new Vector3();
public float x;
public float y;
public Vector3 forward;
public Vector3 toOther;
public bool infront;
void Start () {

}

// Update is called once per frame
void Update () {
forward=camera.transform.TransformDirection(Vector3.forward); //前方
toOther=target.transform.position-camera.transform.position; //相机到物体的向量
if (Vector3.Dot(forward, toOther)>0) //点乘
{
infront=true;
}
else
{
infront=false;
}
//~ print(camera.WorldToScreenPoint (target.position));
screenPos = camera.WorldToScreenPoint (target.position+new Vector3(0F,0.1F,0f));
x=screenPos.x;
y=screenPos.y;
guitext.pixelOffset =new Vector2 (x, y);
guitext.material.color=Color.blue;

}

void OnGUI()
{
if (infront)
{
GUI.Label(new Rect (x, Screen.height-y, 200, 200),"renwuname",center);
guitexture.pixelInset = new Rect (x, y, 100, 100);
}
}
}